![linux awk merge files](https://www.linuxcommands.site/wp-content/uploads/2019/09/image-35.png)
Today, I have a problem that requires two files to be merged based on keywords.
This problem was dealt with before, but I forgot it today. . .
Process in previous article: awk NR FNR difference
Here is a detailed record of the process.
Today’s question has two files:
test-4.txt
➜ cat test-4.txt
123 34131231
321 13123135
128 34673222
457 34522111
101 23455009
456 90909087
111 23456711
test-5.txt
➜ cat test-5.txt
123
456
321
111
The content of the file test-5.txt is part of the first column of the file test-4.txt.
Need to get this result:
123 34131231
456 90909087
321 13123135
111 23456711
Here you need to use the awk variables NR and FNR.
➜ awk '{print "NR:"NR, " FNR:"FNR, " " $0}' test-4.txt
When it is a single file, the values of the variables NR and FNR are the same.
➜ awk '{print "NR:"NR, " FNR:"FNR, " " $0}' test-4.txt test-5.txt
When it is two files, the value of NR is incremented, and the FNR still starts from 1.
According to this feature: when FNR==NR, the first file is read, FNR! =NR reads the second file.
➜ awk -F' ' 'NR==FNR{a[$1]=$2;}NR!=FNR{print $0, a[$1]}' test-4.txt test-5.txt
#awk -F' ' '{if(NR==FNR) {a[$1]=$2;} else if(NR!=FNR) {print $0, a[$1];}}' test-4.txt test-5.txt
In this way, you can get the desired result.
The following commands are explained in detail below:
0. -F’ ‘
Cut two file contents based on spaces.
1. NR==FNR{a[$1]=$2;}
When it is the first file, the file content is converted into an array.
The array key is the first column content, and the array value is the corresponding second column content.
a[123] = 34131231
a[321] = 13123135
a[128] = 34673222
a[457] = 34522111
a[101] = 23455009
a[456] = 90909087
a[111] = 23456711
2. NR!=FNR{print $0, a[$1]}
When it is the second file, it outputs the contents of its file. At the same time, the content of the array a with the second file content as the key is output.
123 a[123]
456 a[456]
321 a[321]
111 a[111]
##########
a[123] = 34131231
a[456] = 90909087
a[321] = 13123135
a[111] = 23456711
So the result is:
123 34131231
456 90909087
321 13123135
111 23456711
OK.