使用grep在文本文件中提取字符串 [英] Extract strings in a text file using 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
结果在 编辑3 : 好吗? I have file.txt with names one per line as shown below: I want to grep each of these from an input.txt file. Manually i do this one at a time as Could someone help to automatically grep all the strings in file.txt from input.txt and write it to output.txt. Contents of Edit: A safer solution with while read: Edit 2: Sample Sample If You don't care about the order of lines, the quick workaround would be to pipe the solution through a The result is then in Edit 3: Is that fine? 这篇关于使用grep在文本文件中提取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! output.txt中$ c
$ b
cat text.txt |而读线; grep\< $ {line} \> input.txt>> output.txt的;完成
ABCB8
ABCC12
ABCC3
ABCC4
AHR
ALDH4A1
ALDH5A1
....
grep "ABCB8" input.txt > output.txt
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
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
. cat text.txt | while read line; do grep "\<${line}\>" input.txt >> output.txt; done