使用grep提取文本文件中的字符串

时间:2013-06-25 07:30:28

标签: string shell grep string-matching

我的file.txt每行有一个名字,如下所示:

ABCB8
ABCC12
ABCC3
ABCC4
AHR
ALDH4A1
ALDH5A1
....

我想从input.txt文件中grep其中的每一个。

手动我一次这样做

grep "ABCB8" input.txt > output.txt

有人可以帮助从input.txt自动grep file.txt中的所有字符串并将其写入output.txt。

2 个答案:

答案 0 :(得分:2)

您可以使用Bash, Linux, Need to remove lines from one file based on matching content from another file

中所述的-f标记
grep -o -f file.txt input.txt > output.txt

标志

  • -f FILE--file=FILE
  

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

  • -o--only-matching
  

仅打印匹配行的匹配(非空)部分   每个这样的部分都在一个单独的输出线上。

答案 1 :(得分:-1)

for line in `cat text.txt`; do grep $line input.txt >> output.txt; done

text.txt的内容:

ABCB8
ABCC12
ABCC3
ABCC4
AHR
ALDH4A1
ALDH5A1

修改

阅读时更安全的解决方案:

cat text.txt | while read line; do grep "$line" input.txt >> output.txt; done

编辑2

示例text.txt

ABCB8
ABCB8XY
ABCC12

示例input.txt

You were hired to do a job; we expect you to do it.
You were hired because ABCB8 you kick ass;
we expect you to kick ass.
ABCB8XY You were hired because you can commit to a rational deadline and meet it;
ABCC12 we'll expect you to do that too.
You're not someone who needs a middle manager tracking your mouse clicks

如果您不关心行的顺序,快速解决方法是通过sort | uniq管道解决方案:

cat text.txt | while read line; do grep "$line" input.txt >> output.txt; done; cat output.txt | sort | uniq > output2.txt

结果是output.txt

编辑3

 cat text.txt | while read line; do grep "\<${line}\>" input.txt >> output.txt; done

没关系吗?