我怎样才能在循环中grep? [英] How can I grep in a loop?

查看:136
本文介绍了我怎样才能在循环中grep?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含单独文本行的文件。

  text1 
text2
text3
textN

我有一个包含许多文件的目录。我想grep这个特定目录中的每一行。什么是简单的方法来做到这一点?

解决方案

没有必要循环,您可以使用 grep -f 选项从文件中获取模式:



<$ p $ grep -f pattern_file files *

man grep


-f FILE,--file = FILE p>

从FILE获取模式,每行一个。空文件包含零
模式,因此什么都不匹配。 (-f由POSIX指定)。



测试



  $ cat a1 
hello
你好吗?

$猫a2
再见
你好

$猫图案
你好
再见

$ grep -f pattern a *
a1:hello
a2:bye
a2:hello


I have a file containing text in separate lines.

text1
text2
text3
textN

I have a directory with many files. I want to grep for each line in the of this specific directory. What is an easy way to do this?

解决方案

There is no need to loop, you can do use grep with the -f option to get patterns from a file:

grep -f pattern_file files*

From man grep:

-f FILE, --file=FILE

Obtain patterns from FILE, one per line. The empty file contains zero patterns, and therefore matches nothing. (-f is specified by POSIX.)

Test

$ cat a1
hello
how are you?

$ cat a2
bye
hello

$ cat pattern
hello
bye

$ grep -f pattern a*
a1:hello
a2:bye
a2:hello

这篇关于我怎样才能在循环中grep?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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