grep使用grep结果 [英] grep using grep results
问题描述
比方说,我有两个包含属性名称和属性的文件,其中不同的数据存储在两个文件中:
Let's say I have two files containing attribute names and properties, where different data is stored in two files:
1.txt:
abc12 3@ 50
edf13 4@ 50
dde8 3@ 50
2.txt
abc12 3@ 65
edf13 4@ 50
dde8 3@ 70
dde7 3@ 70
我要grep如下:first- grep"3 @" 1.txt |awk'{print $ 1}'
,然后 grep * 2.txt
,其中star(*)代表第一个grep的输出.
I want to grep as follows:
first- grep "3@" 1.txt | awk '{print $1}'
, then grep * 2.txt
where star(*) represent output of first grep.
所有操作都应在提示符后的一行中完成.
all should be done in a single line from the prompt.
预期输出:
abc12 3@ 65
dde8 3@ 70
谢谢
推荐答案
通过一个 awk
进程:
awk 'FNR==NR{if ($2 ~ "3@") a[$1]++; next} a[$1]' 1.txt 2.txt
第一个 condition {action}
意味着从第一个文件读取时,以第一个字段为哈希值增加数组元素.当此元素为一个或多个时,第二个条件 a [$ 1]
为true,将检查第二个文件,默认操作是打印该行.
the first condition{action}
means when reading from first file, increase the array element with this first field as hash. The second condition a[$1]
is true when this element is one or more, this is checked for the second file and the default action is to print the line.
这篇关于grep使用grep结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!