使用grep在文本文件中提取字符串 [英] Extract strings in a text file using grep

查看:2794
本文介绍了使用grep在文本文件中提取字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  ABCB8 
ABCC12
ABCC3
ABCC4
AHR
ALDH4A1
ALDH5A1
....

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



手动我每次做这个

  grepABCB8input.txt> output.txt 

有人可以帮助从input.txt自动grep file.txt中的所有字符串,把它写到output.txt中。

解决方案

  for`cat text.txt` ;做grep $ line input.txt>> output.txt的;完成

text.txt的内容

  ABCB8 
ABCC12
ABCC3
ABCC4
AHR
ALDH4A1
ALDH5A1

编辑

一个更安全的解决方案,可以阅读:

  cat text.txt |而读线;做grep$ lineinput.txt>> output.txt的;完成

编辑2

示例 text.txt

  ABCB8 
ABCB8XY
ABCC12

示例 input.txt $ b

 你被雇佣去做一份工作;我们希望你这样做。 
你被雇佣是因为ABCB8你踢屁股;
我们希望你踢屁股。
ABCB8XY你被聘用是因为你可以承诺一个合理的期限并满足它;
ABCC12我们希望您也能这样做。
你不是一个需要一个中间管理者来跟踪你的鼠标点击的人

如果你不喜欢不关心行的顺序,快速的解决方法是通过 sort | uniq

  cat text.txt |而读线;做grep$ lineinput.txt>> output.txt的;完成; cat output.txt |排序| uniq> output2.txt 

结果在 output.txt中 $ b

编辑3

  cat text.txt |而读线; grep\< $ {line} \> input.txt>> output.txt的;完成

好吗?


I have file.txt with names one per line as shown below:

ABCB8
ABCC12
ABCC3
ABCC4
AHR
ALDH4A1
ALDH5A1
....

I want to grep each of these from an input.txt file.

Manually i do this one at a time as

grep "ABCB8" input.txt > output.txt

Could someone help to automatically grep all the strings in file.txt from input.txt and write it to output.txt.

解决方案

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

Contents of text.txt:

ABCB8
ABCC12
ABCC3
ABCC4
AHR
ALDH4A1
ALDH5A1

Edit:

A safer solution with while read:

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

Edit 2:

Sample text.txt:

ABCB8
ABCB8XY
ABCC12

Sample 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

If You don't care about the order of lines, the quick workaround would be to pipe the solution through a sort | uniq:

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

The result is then in output.txt.

Edit 3:

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

Is that fine?

这篇关于使用grep在文本文件中提取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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