了解awk中的两个文件处理

时间:2019-02-22 07:31:33

标签: awk sed

我试图了解两个文件处理的工作方式。所以这里创建了一个例子。 file1.txt

zzz pq Fruit Apple 10
zzz rs Fruit Car 50
zzz tu Study Book 60

file2.txt

aa bb Book 100
cc dd  Car 200
hj kl XYZ 500
ee ff Apple 300
ff gh ABC 400

我想将file1的第4列与file2的第3列进行比较,如果匹配,则打印file1的第3、4、5列,然后打印{ {1}},其文件1的第5列与文件2的第4列之和。

预期输出:

file2

这是我尝试过的:

Fruit Apple 10 300 310
Fruit Car 50 200 250
Study Book 60 100 160

代码输出;

awk ' FNR==NR{ a[$4]=$5;next} ( $3 in a){ print $3, a[$4],$4}' file1.txt file2.txt

我在打印Book 100 Car 200 Apple 300 列以及如何存储file1时遇到问题。请引导我。

1 个答案:

答案 0 :(得分:1)

请您尝试以下。

awk 'FNR==NR{a[$4]=$3 OFS $4 OFS $5;b[$4]=$NF;next} ($3 in a){print a[$3],$NF,b[$3]+$NF}' file1.txt  file2.txt

输出如下。

Study Book 60 100 160
Fruit Car 50 200 250
Fruit Apple 10 300 310

说明: 现在添加上述代码的说明。

awk '                              ##Starting awk program here.
FNR==NR{                           ##Checking condition FNR==NR which will be TRUE when first Input_file named file1.txt is being read.
  a[$4]=$3 OFS $4 OFS $5           ##Creating an array named a whose index is $4 and value is 3rd, 4th and 5th fields along with spaces(By default OFS value will be space for awk).
  b[$4]=$NF                        ##Creating an array named b whose index is $4 and value if $NF(last field of current line).
  next                             ##next keyword will skip all further lines from here.
}
($3 in a){                         ##Checking if 3rd field of current line(from file2.txt) is present in array a then do following.
  print a[$3],$NF,b[$3]+$NF        ##Printing array a whose index is $3, last column value of current line and then SUM of array b with index $3 and last column value here.
}
' file1.txt  file2.txt             ##Mentioning Input_file names file1.txt and file2.txt