简体   繁体   English

将sed输出通过xargs输出到grep的问题

[英]Problems piping sed output to grep through xargs

I am having problems piping sed output to be used with grep through xargs . 我有问题管道sed输出与grep通过xargs一起使用。 I think it is a problem of variable expansion, but I am not sure. 我认为这是一个可变扩展的问题,但我不确定。

The scenario is as follows. 方案如下。 Current directory has these files: 当前目录包含以下文件:

file1.log
file1.log.67654
file2.log
file2.log.66876
file3.log
file3.log.56768
...

From these files, only some of the ones ending in .log contain a certain string so that: 从这些文件中,只有一些以.log结尾的文件包含某个字符串,以便:

$ grep -l "certainString"
file1.log
file3.log

What I want is to find how many of these files OR the ones that start like these but have additional .numbers as extension (files file1.log , file1.log.67654 , file3.log and file3.log.56768 in this example) contain another string. 我想要的是找到这些文件中有多少或者那些以这些文件开头但有额外的.numbers作为扩展名的文件(本例中为文件file1.logfile1.log.67654file3.logfile3.log.56768 )包含另一个字符串

I thought that this command would do it: 我以为这个命令会这样做:

grep -l "certainString" | sed "s/log/log*/g" | xargs grep -l "anotherString"

But after I replace log with log* with sed , xargs does not expand the * but inteprets it literally, therefore: 但经过我顶替loglog*sedxargs不扩大* ,但字面上inteprets它,因此:

$ grep -l "certainString" | sed "s/log/log*/g" | xargs grep -l "anotherString"
grep: ./file1.log*: No such file or directory
grep: ./file3.log*: No such file or directory

I have read about this and people suggest using something like: 我已经读过这个,人们建议使用类似的东西:

grep -l "certainString" | sed "s/log/log*/g" | xargs -I@ sh -c 'grep -l "anotherString"'

But I can´t make it work (I do not fully understand it, to be honest). 但是我不能让它发挥作用(说实话,我并不完全理解它)。 I feel I am just there but I am missing a detail. 我觉得我就在那里,但我错过了一个细节。 Any help? 有帮助吗?

in case file names doesn't contain spaces or globbing metacharater ( * [ ] ) 以防文件名不包含空格或globbing metacharater( * [ ]

Just add @ , because -I@ means that @ is a token to be replaced by argument 只需添加@ ,因为-I@表示@是要被参数替换的标记

grep -l "certainString" | sed "s/log/log*/g" | xargs -I@ sh -c 'grep -l "anotherString" @'

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

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