简体   繁体   English

搜索文件中带有特定文本的行并输出文件名(文件命令和grep)

[英]Search for lines with specific text in files and output file names (file command and grep)

I know this question may be simple to some of you, but I have tried several combinations and googled a lot, without success. 我知道这个问题对某些人来说可能很简单,但是我尝试了几种组合并用Google搜索很多,但没有成功。

Problem: I have a bunch of files with a given file name, but in different directories. 问题:我有一堆具有给定文件名的文件,但是位于不同的目录中。

For example, I have a file called 'THEFILE.txt' in directories a, b, c, d. 例如,我在目录a,b,c,d中有一个名为“ THEFILE.txt”的文件。 I am in a directory that has these as subdirectories. 我在具有这些作为子目录的目录中。 In each of 'THEFILE.txts' I am looking for lines with the following pattern :'Has this property blah blah blah _apple'. 在每个“ THEFILE.txts”中,我正在寻找具有以下模式的行:“具有此属性等等吗?_apple”。 So what I know for sure about the line is that it starts with 'Has this property ' and ends with '_apple'. 因此,我对此行的确切了解是,它以“具有此属性”开头,以“ _apple”结尾。

I tried: 我试过了:

  1. find . 找 。 -name 'THEFILE.txt' -exec grep -l 'Has this property' {} \\; -name'THEFILE.txt'-exec grep -l'具有此属性'{} \\; This works, but I get each and every line with 'Has this property'. 这可行,但是我得到的每一行都带有“具有此属性”。 I only want ones with _apple at the end 我只想要结尾为_apple的

  2. So I tried: find . 所以我尝试了:查找。 -name 'THEFILE.txt' -exec grep -l 'Has this property*_apple' {} \\; -name'THEFILE.txt'-exec grep -l'具有此属性* _apple'{} \\; //Does not work, and from my google searches, I don't expect it to. //不起作用,从我的Google搜索中,我不希望如此。

  3. So, next I tried: find . 因此,接下来我尝试:查找。 -name 'THEFILE.txt' -exec grep -l 'Has this property[!-~]*_apple' {} \\; -name'THEFILE.txt'-exec grep -l'具有此属性[!-〜] * _ apple'{} \\; //DOES NOT WORK //不起作用

  4. find . 找 。 -name 'THEFILE.txt' -exec grep 'Has this property' {} \\; -name'THEFILE.txt'-exec grep'具有此属性'{} \\; | | grep '_apple$' //This outputs all matching lines, but not the file names grep'_apple $'//输出所有匹配的行,但不输出文件名

  5. find . 找 。 -name 'THEFILE.txt' -exec grep 'Has this property' {} \\; -name'THEFILE.txt'-exec grep'具有此属性'{} \\; | | grep -l '_apple$' //Says file is stdin grep -l'_apple $'//说的文件是标准输入

Expected output: (say files a and c have desired lines) 预期的输出:(例如文件a和c具有所需的行)

./a/THEFILE.txt
./c/THEFILE.txt

Your attempt 2. was almost there. 您的尝试2.快到了。 With a little adjustment: 稍作调整:

find . -name THEFILE.txt -exec grep -q '^Has this property.*_apple$' '{}' ';' -print

it is more precise than recursive grepping and simpler (no pipeline). 它比递归grepping更精确且更简单(无管道)。

The reason why the above works (as opposed to grep -l ), is that the -exec action evaluates to whatever the exit status of its command was. 上面的方法起作用的原因(与grep -l相反)是因为-exec动作的计算结果是其命令的退出状态。 grep will exit with 0 status (which is true ) if it finds what it looked for, that will make -exec yield true , and that in turn will cause the next action ( -print ) to be taken too. grep找到了所要查找的内容,它将以0状态退出(这是true ),这将使-exec yield true ,从而使下一个动作( -print )也被执行。

您可以只使用grep -r,如果只需要文件名,则将其通过管道传输到awk中。

grep -r "_apple$" . | awk -F: '{print $1}'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM