如何在关键字段匹配后打印所有列

时间:2017-02-21 20:06:23

标签: awk

在匹配关键字段后,如何从两个文件中加入每行的所有字段?如果f2中的字段数未知,如何推广这个单行?

f2:    
a 1 2    
b 3 4    
c 5 6   

f3:    
10 a x y z    
11 g x y z    
12 j x y z    

observed:    
a 10 x y z
a1 10 x y z

Desired:    
a 1 2 10 x y z

这些是我最好的尝试,但不正确:

  

awk'FNR == NR {a [$ 1] = $ 2; next}($ 2 in a){print a [$ 2],$ 0}'f2.txt f3.txt> f4.txt

     

awk'FNR == NR {a [$ 1] = $ 2 $ 3; next}($ 2 in a){print a [$ 2],$ 0}'f2.txt f3.txt> f4.txt

3 个答案:

答案 0 :(得分:0)

for (keys %myHash) {...}

将整个值保存为值,将column1保存为键,当读取第二个文件时,检查数组 awk 'NR==FNR{a[$1]=$0;next} ($2 in a){print a[$2],$1,$3,$4,$5}' f2.txt f3.txt > f4.txt 中的column2与否,如果是,则打印a,其余列

更短的方法(这个命令的缺点是在10和x之间有一个额外的空间):

a[$2]

用空字符串替换$ 2的第二个文件,并打印整行awk 'NR==FNR{a[$1]=$0;next} ($2 in a){second=$2; $2="";print a[second],$0}' f2.txt f3.txt > f4.txt

答案 1 :(得分:0)

@ mxttgen31:试试:

awk 'FNR==NR{Q=$2;$2="";A[Q]=$0;next} ($1 in A){print $0,A[$1]}'  f3  f2

以上命令的说明如下:

awk 'FNR==NR{      ##### Checking condition FNR==NR here, where FNR and NR both denotes the number of line, 
                         only difference between FNR and NR is as we could read mutiple files from awk, 
                         value of FNR will be RESET on next file's start, where NR's value will be keep on increasing till 
                         it completes reading all the file. so this condition will be TRUE only when first Input_file(which is f3 here) will be TRUE.
Q=$2;              ##### Assigning second field's value to variable Q.
$2="";             ##### making second field's value to NULL now.
A[$2]=$0;          ##### Create an array named A whose index is $2 and value is current line.
next}              ##### putting next(awk's in-built keyword) which skips all next further statements and take the cursor again to starting.
($1 in A)          ##### Cursor will come here whenever second Input_file is being read, so here checking $1(first field) is present in array A then do following.
{print $0,A[$1]}   ##### print the current line($0) of current file named f2 and print array A's value whose index is $1 of current file f2.
' f3  f2           ##### Mentioning Input_files here.

答案 2 :(得分:0)

如果您的文件按照示例中的键排序,join是此任务的工具

join -11 -22 f2.txt f3,txt