如何grep查询两个字符串的所有文件 [英] How to grep query all files for two strings

查看:295
本文介绍了如何grep查询两个字符串的所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里我们去:

我需要查询两个都有TODO语句和我的名字的php文件。
这两个字符串都可以在文档的任意位置(即行),并且可以放在0-无限行(位置0-n)的任何地方。



如何grep查询我的名字:
$ b

  find -name'*。 php'-exec grep -infincken{} + 

输出:

  ./ some / file.php:51:ramon fincken 
./somefile.php:2:rfincken

如何为TODO查询grep

  find -name'* .php'-exec grep -nTODO{} + 

输出:

  ./ some / file.php:53:// TODO:foobar! 
./some/otherfile.php:53:// TODO:foobar?

我需要结合两个grep查询(或其结果)我期待这样的结果:

$ p $ ./ some / file.php

我曾经在一个grep中尝试过运算符,但他们预计两个字符串在同一行上,并以特定的顺序或想出所有结果(OR .. OR)而不是(AND)

解决方案

这行看起来很难看,但它应该给你想要的东西: b
$ b

  find whatever ... | xargs grep -il'fincken'
| xargs grep -il'todo'
| xargs grep -in -e'todo'-e'fincken'

输出如下所示:

  / foo / bar / file:100:TODO 
/ foo / bar / file:101:fincken

只有 TODO fincken 将被列出。


Here we go:

I need to query php files which both have a TODO statement as well as my name. Both strings could be anywhere in the document (ie. line) and be positioned anywhere on 0-infinite lines (position 0-n).

How to grep query for my name:

find -name '*.php' -exec grep -in "fincken" {} +

output:

./some/file.php:51: ramon fincken
./somefile.php:2: rfincken

How to grep query for the TODOs

find -name '*.php' -exec grep -n "TODO" {} +

output:

./some/file.php:53: // TODO: foobar!
./some/otherfile.php:53: // TODO: foobar?

I need to combine both grep queries (or their results) so I am expecting this as result:

./some/file.php

I have tried operators in one grep, but they expected both strings on the same line and in a particular order or .. came up with all results (OR .. OR) instead of ( AND )

解决方案

this line looks ugly, but it should give what you want:

find whatever...|xargs grep -il 'fincken'
                |xargs grep -il 'todo'
                |xargs grep -in -e'todo' -e'fincken'  

The output would look like:

/foo/bar/file : 100:TODO
/foo/bar/file : 101:fincken

only files with both TODO and fincken would be listed.

这篇关于如何grep查询两个字符串的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆