0

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

ABCB8
ABCC12
ABCC3
ABCC4
AHR
ALDH4A1
ALDH5A1
....

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

手动我一次做一个

grep "ABCB8" input.txt > output.txt

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

4

2 回答 2

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:

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

于 2013-06-25T08:01:52.233 回答
-1
for line in `cat text.txt`; do grep $line input.txt >> output.txt; done

内容text.txt

ABCB8
ABCC12
ABCC3
ABCC4
AHR
ALDH4A1
ALDH5A1

编辑

while read 的更安全的解决方案:

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

可以吗?

于 2013-06-25T07:34:42.970 回答