我正在尝试使用公共列从两个文件中提取数据,但我无法获取所需的数据。
File1
A B C D E F G
Dec 3 abc 10 2B 21 OK
Dec 1 %xyZ 09 3F 09 NOK
Dec 5 mnp 89 R5 11 OK
File2
H I
abc 10
xyz 00
pqr 45
我能够获得输出A B C D E F G但是无法在C& C之间添加I E栏。
Trail 1:
awk 'FNR==1{next}
NR==FNR{a[$1]=$2; next}
{k=$3; sub(/^\%/,"",k)} k in a{print $1,$2,$3,$4,a[$2],$5,$6,$7; delete a[k]}
END{for(k in a) print k,a[k] > "unmatched"}' File2 File1 > matched
必需的输出:
matched:
A B C D I E F G
Dec 3 abc 10 10 2B 21 OK
Dec 1 %xyZ 09 00 3F 09 NOK
unmatched :
H I
pqr 45
Could you please help me for getting this output please ? Thank you.
答案 0 :(得分:0)
小心你在file1中有一个大写的Z.我把它放在我的测试中的小写 - 如果它不是一个错字,那是另一个要处理的小细节。
$ awk 'FNR==1 {next}
NR==FNR {a[$1]=$2; next}
{k=$3; sub(/^\%/,"",k)}
k in a {print $1,$2,$3,$4,a[k],$5,$6,$7; delete a[k]}
END {for(k in a) print k,a[k] > "unmatched"}' File2 File1 > matched
$ cat matched
Dec 3 abc 10 10 2B 21 OK
Dec 1 %xyz 09 00 3F 09 NOK
$ cat unmatched
pqr 45
包含四列的文件2 :
$ cat a
A B C D E F G
Dec 3 abc 10 2B 21 OK
Dec 1 %xyz 09 3F 09 NOK
Dec 5 mnp 89 R5 11 OK
$ cat b
H I J K
abc 10 j1 k1
xyz 00 j2 k2
pqr 45 j3 k3
$ cat x.awk
FNR==1 {next}
NR==FNR {a[$1]=$0; next}
{k=$3; sub(/^\%/,"",k)}
k in a {
split(a[k], b)
print $1,$2,b[2],$3,b[3],b[4],$4,$5,$6,$7; delete a[k]
}
END {for(k in a) print a[k] > "unmatched"}
$ awk -f x.awk b a
Dec 3 10 abc j1 k1 10 2B 21 OK
Dec 1 00 %xyz j2 k2 09 3F 09 NOK
$ cat unmatched
pqr 45 j3 k3