grep "output of cat command - every line"在不同的文件中



对不起,这个问题的标题有点令人困惑,但我想不出其他任何东西。我正在尝试做这样的事情

cat fileA.txt | grep `awk '{print $1}'` fileB.txt

fila包含100行,而filb包含1亿行。

我想要的是从fila中获取id,在另一个文件- filb中grep该id并打印该行。

e.g fileA.txt
1234
1233
e.g.fileB.txt
1234|asdf|2012-12-12
5555|asdd|2012-11-12
1233|fvdf|2012-12-11

期望输出

1234|asdf|2012-12-12
1233|fvdf|2012-12-11

同时去掉catawk:

grep -f fileA.txt fileB.txt

一个人就能做好那件事:

awk -F'|' 'NR==FNR{a[$0];next;}$1 in a' fileA fileB

参见test:

kent$  head a b
==> a <==
1234
1233
==> b <==
1234|asdf|2012-12-12
5555|asdd|2012-11-12
1233|fvdf|2012-12-11
kent$  awk -F'|' 'NR==FNR{a[$0];next;}$1 in a' a b
1234|asdf|2012-12-12
1233|fvdf|2012-12-11

编辑

添加的解释:

-F'|'  #| as field separator (fileA)
'NR==FNR{a[$0];next;} #save lines in fileA in array a
 $1 in a  #if $1(the 1st field) in fileB in array a, print the current line from FileB
我无法在这里解释更多的细节,对不起。例如awk如何处理两个文件,什么是NR,什么是FNR..如果接受的答案不适合你,我建议你试试这句话。如果您想深入了解,请阅读一些awk教程。

如果id在不同的行上,您可以在grep中使用-f选项:

cut -d "|" -f1 < fileB.txt | grep -F -f fileA.txt

cut命令将确保在使用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.)

相关内容

最新更新