Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
递归打印匹配模式的文件行,排除多个目录(BASH)_Bash_Grep_Find - Fatal编程技术网

递归打印匹配模式的文件行,排除多个目录(BASH)

递归打印匹配模式的文件行,排除多个目录(BASH),bash,grep,find,Bash,Grep,Find,我想写一个bash脚本: 接受要匹配的正则表达式。查找当前目录中的所有文件,并最终查找包含该单词的子目录(如果指定了-r)。打印-匹配正则表达式后的n行。(可选):按扩展名排除多个目录或文件 例如: ./dir |- file_1 |- file_2 |- subDir1 |- file_3 |- node_modules (subDir) |- bin (subDir) 运行 myscript -w 'TO

我想写一个bash脚本:

接受要匹配的正则表达式。查找当前目录中的所有文件,并最终查找包含该单词的子目录(如果指定了-r)。打印-匹配正则表达式后的n行。(可选):按扩展名排除多个目录或文件

例如:

./dir
    |- file_1
    |- file_2
    |- subDir1 
          |- file_3
          |- node_modules (subDir)
          |- bin (subDir)
运行

myscript -w 'TODO' -r -d '<list of excluded dir>' -x <list of excluded extensions file>' -l '<lines inside file after where i found word to be printed>
问题是我不知道如何在bash中传递要被grep排除的dir和文件列表。我试过用正则表达式

dirs="(node_modules|bin)"
但它不是格雷普读的

另一个要求是打印上次修改文件的数据

目前我得到的只是:

./file1_path: line1
./file1_path: line2
...
我想得到:

./file_path | last_modification_date | line_string
...
排除的目录和文件扩展名未打印出来

我试图通过find+grep来完成这项工作,但我发现很难连接这些命令。 现在我被困在这个问题上:

find . -maxdepth $MAXDEPTH -type f -exec ls -lah {} | gawk '{print "<here the infos to extract (filename data )>"}'\; -exec grep "$WORD" -A 4 {} \;
第二个
-exec
命令没有输出:

虽然没有呆呆地看,但效果很好:

-rwxr-xr-x 1 linux linux 2,4K mar 19 10:30 ./todo_checker  <=== THIS LINE
# Following program check search for TODO's in file your working on. 
# It is useful when you're a working on a large software and spread 
# TODO's over the files. 
#
# It check for TODO's and print: 
# file_name | file_location | file_last_modification
# n lines after todo
#
# parameters: 

-rwxr-xr-x 1 linux 2,4K mar 19 10:30./todo_checker如果你写这篇文章是为了练习什么的,那就不同了。但是,如果你只是需要这个功能,它真的很擅长,并且很容易安装在几乎任何操作系统上。如果你用它来做的话,
--ignore dir
--ignore
会处理好这一切。这听起来像是
查找
的工作。它和你在这里列出的差不多。添加一对
| grep-v…
以删除一些结果(您的排除项),您会没事的。好的,谢谢大家。当然@Mihir我会去看看银色搜索者。我试图通过find+grep来完成这项工作,但我发现很难连接这些命令。现在我被困在这个问题上:
find-maxdepth$maxdepth-type f-exec ls-lah{}|gawk'{print'}\-执行grep“$WORD”-a4{}\没有
awk
,它可以正常工作,但我需要它来美化命令行中打印的关于文件的信息
gawk: row com.:1: ./file_1
gawk: row com.:1: ^ syntax error
gawk: row com.:1: ./file_1
gawk: row com.:1:   ^ regular expression not complete
-rwxr-xr-x 1 linux linux 2,4K mar 19 10:30 ./todo_checker  <=== THIS LINE
# Following program check search for TODO's in file your working on. 
# It is useful when you're a working on a large software and spread 
# TODO's over the files. 
#
# It check for TODO's and print: 
# file_name | file_location | file_last_modification
# n lines after todo
#
# parameters: