AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 437118
Accepted
Akand
Akand
Asked: 2018-04-12 10:59:49 +0800 CST2018-04-12 10:59:49 +0800 CST 2018-04-12 10:59:49 +0800 CST

通过搜索特定的文件扩展名对目录进行排序

  • 772

假设我在目录 A 中,在 A 下有许多文件夹(B、C、D 等),每个文件夹中都有一个文件“*.out”和子文件夹。我想从 A 运行一个脚本,它将在 *.out 文件中查找文本“index123”并打印出所有相应的文件夹名称。

这是我的脚本:

#!/bin/sh  
FILES=home/A  
grep --include=\*.out -rnw $FILES -e "index123" | while read file; do  
str1="FILES/$(basename $file)"  
echo $str1
done

这显示错误。

NB 这可以通过在一行代码中“查找”来完成,但为什么显示的 while 循环显示错误?

directory sort
  • 2 2 个回答
  • 97 Views

2 个回答

  • Voted
  1. Kusalananda
    2018-04-12T12:20:09+08:002018-04-12T12:20:09+08:00

    假设目录结构如下:

    A
    |-- B
    |   |-- file1.out
    |   |-- file2.out
    |   `-- file3.out
    |-- C
    |   |-- file1.out
    |   |-- file2.out
    |   `-- file3.out
    |-- D
    |   |-- file1.out
    |   |-- file2.out
    |   `-- file3.out
    `-- E
        |-- file1.out
        |-- file2.out
        `-- file3.out
    

    您的代码的问题是您grep将产生看起来像

    ./B/file1.out:2:some data which includes the word index123
    ./B/file2.out:2:some data which includes the word index123
    ./B/file3.out:2:some data which includes the word index123
    ./C/file1.out:2:some data which includes the word index123
    ./C/file2.out:2:some data which includes the word index123
    ./C/file3.out:2:some data which includes the word index123
    ./D/file1.out:2:some data which includes the word index123
    ./D/file2.out:2:some data which includes the word index123
    ./D/file3.out:2:some data which includes the word index123
    ./E/file1.out:2:some data which includes the word index123
    ./E/file2.out:2:some data which includes the word index123
    ./E/file3.out:2:some data which includes the word index123
    

    那是的输出

    grep --include=\*.out -rnw . -e "index123"
    

    与A作为当前目录。

    然后,您将尝试basename在这些单独的行上运行,但由于basename最多需要两个参数(路径名和从该路径名中删除的后缀)而失败。GNUbasename会抱怨“额外的操作数”,而 BSDbasename会抱怨不正确的使用。


    grep-l当您将其与标志一起使用时,将向您显示文件的名称(仅,即不是匹配的完整行) 。

    这意味着您的脚本可能会被单个命令替换

    grep -w -l "index123" */*.out
    

    这将在表单上给出输出

    B/file1.out
    B/file2.out
    B/file3.out
    C/file1.out
    C/file2.out
    C/file3.out
    D/file1.out
    D/file2.out
    D/file3.out
    E/file1.out
    E/file2.out
    E/file3.out
    

    我添加-w了您在grep命令行中使用的内容。-n(用于编号行,您也在使用)不能与 . 一起使用-l。

    从你的代码来看,这就是你想要的。

    如果您只需要文件夹名称,请执行

    $ grep -w -l "index123" */*.out | sed 's#/[^/]*##' | sort -u
    B
    C
    D
    E
    

    所有这些都假定这A是当前的工作目录,但是您说问题中就是这种情况,所以这应该不是问题。

    • 2
  2. Best Answer
    Akand
    2018-07-21T07:45:20+08:002018-07-21T07:45:20+08:00

    根据帖子通过特定搜索在while循环中查找文件,其中一种解决方案可以使用while循环如下:

    #!/bin/bash
    while IFS= read -r d;
    grep -q "index123" "$d" && dirname "$d"|awk -F'/' '{print $2}'
    done < <(find . -maxdepth 2 -type f -name "*.out")

    • 1

相关问题

  • 如何对文件中每个日期的行数进行排序?

  • 如何使用bash删除目录中的所有文件夹?

  • 确定自上次以来目录内容是否已更改的最快方法

  • 列出根据特定内容行排序的文件

  • 读取带有单词的文本文件及其出现次数和排序的打印输出

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    ssh 无法协商:“找不到匹配的密码”,正在拒绝 cbc

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    如何卸载内核模块“nvidia-drm”?

    • 13 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Wong Jia Hau ssh-add 返回:“连接代理时出错:没有这样的文件或目录” 2018-08-24 23:28:13 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST
  • Martin Hope
    Bagas Sanjaya 为什么 Linux 使用 LF 作为换行符? 2017-12-20 05:48:21 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve