Issue
I have file1.txt and file2.txt with the following structure
File1.txt
5511913332222
5511910000023
5511910000029
5511910000034
File2.txt
5511910000029|BLOCKED|7|30/07/2021 02:19:43
5511910000034|AVAIL|7|30/07/2021 03:11:53
5511910000048|AVAIL|7|30/07/2021 04:10:25
5511910000073|BLOCKED|7|30/07/2021 07:20:33
I want to write a file3.txt with the 1st and 2nd columns of the file2 where the 1st column matched the 1st column of file1.txt.
File3.txt
5511910000029|BLOCKED
5511910000034|AVAIL
I have tried some tricks with awk but I couldn’t get the expected result. Could anyone please help me?
awk ‘NR==FNR{a[$0]}NR>FNR && $0 in a{print}’ file1 file2 > file3
Solution
$ awk -F'|' -v OFS='|' 'NR==FNR {a[$1];next} ($1 && $1 in a){print $1,$2}' File1.txt File2.txt
5511910000034|AVAIL
To save output to File3.txt ... {print $1,$2 > "File3.txt"} ...
Answered By – xgadme
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0