3

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

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

fileA 包含 100 行,而 fileB 包含 1 亿行。

我想要的是从fileA中获取id,在不同的file-fileB中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
4

3 回答 3

11

cat完全摆脱awk

grep -f fileA.txt fileB.txt
于 2013-01-10T22:28:33.553 回答
4

单独的 awk 可以很好地完成这项工作:

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

看测试:

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

for further details I cannot explain here, sorry. for example how awk handle two files, what is NR and what is FNR.. I suggest that try this awk line in case the accepted answer didn't work for you. If you want to dig a little bit deeper, read some awk tutorials.

于 2013-01-10T22:48:07.160 回答
1

如果 id 位于不同的行上,您可以使用以下-f选项grep

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.)
于 2013-01-10T22:28:04.363 回答