Friday, May 3, 2024
 Popular · Latest · Hot · Upcoming
2
rated 0 times [  2] [ 0]  / answers: 1 / hits: 5709  / 2 Years ago, sun, february 27, 2022, 9:54:11

I have file1 and file2 as following. I would like to first find matches between two files based on first column and then print the matching lines from file1 and file2. I have written the desired output below



$ cat file1
1, 456, abcd, ...
23, 567, rstc, ...
45, 678, rsto, ...




$ cat file2
23, 5607, abcstc, ...
45, 28, zfgsto, ...


Desired output:



23,     567,    rstc,    ...
23, 5607, abcstc, ...
45, 678, rsto, ...
45, 28, zfgsto, ...

More From » awk

 Answers
6

How about



awk -F, 'NR==FNR {a[$1]=$0;next}; $1 in a {print a[$1]; print}' file1 file2

[#21514] Monday, February 28, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
irtuallyefu

Total Points: 429
Total Questions: 97
Total Answers: 119

Location: Hong Kong
Member since Tue, Oct 19, 2021
3 Years ago
irtuallyefu questions
;