0

How can I match a column from a file to next file and append the columns if present using awk.

File 1:

T36 T200 T77 T99 T100 T101 T110 

File2:

T36 aa 123 T36 aa 456 T200 cc 789 T99 aa 1011 T77 bb 1213 T77 bb 1415 T100 xx 1617 

Desired output:

T36 aa 123 T36 aa 456 T200 cc 789 T77 bb 1213 T77 bb 1415 T99 aa 1011 T100 xx 1617 T101 T110 

In my output file I want to get the order of file 1 and also print the lines that are not present in file 2.

So far I have done this but it is printing only the matched columns not all.

awk 'NR == FNR { x[$1]=$1; next} { print x[$1], $0 }' file1 file2 

Please Help!

1
  • 1
    (1) you have to parse file2 first and later file1 (2) You have to save multiline string for every key, that means appending to exisitng value, (depending on a conditional expression) and not overwriting (3) when printing, again a ternary is needed because you want to print the key if there is no value. Commented Jun 10, 2022 at 22:15

1 Answer 1

2
awk 'f==2 {map[$1] = ($1 in map) ? map[$1] ORS $0 : $0} f==1 {print (($1 in map)? map[$1] : $1)} ' f=2 file2 f=1 file1 

or the same using the FNR==NR idiom:

awk 'FNR==NR {map[$1] = ($1 in map) ? map[$1] ORS $0 : $0; next} {print (($1 in map)? map[$1] : $1)} ' file2 file1 

Output:

T36 aa 123 T36 aa 456 T200 cc 789 T77 bb 1213 T77 bb 1415 T99 aa 1011 T100 xx 1617 T101 T110 
3
  • Thanasisp, thank you, this worked but in some cases it is repeating lines Commented Jun 10, 2022 at 22:25
  • Are lines into file1 unique? In any case, you have to update with a reproducible case where "it is repeating lines" so I can check it. Now I can only see the test example into your question. Commented Jun 10, 2022 at 22:31
  • Thank you yes they are unique, I can change them manually. Its not too many. Thank you so much for your help. Commented Jun 10, 2022 at 22:34

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.