Compare two files and find match and print the header of the second file

Hi,

I have two input files; file1 and file2. I compare them based on matched values in 1 column and print selected columns of the second file (file2). I got the result but the header was not printed. i want the header of file2 to be printed together with the result. Then i did below codes:-

awk -F "\t" 'FNR==NR{F2[$1]; next}FNR==1{print} ($1 in F2) {print $1 "\t" $6 "\t" $7 "\t" $10 "\t" $12 "\t" $13 "\t" $24}' file1 file2 > matched_output

but it gives me the headers for all columns, whereas i just want the header for the selected columns only. Any help is so appreciated. Thanks

Try:

awk -F "\t" 'FNR==NR{F2[$1]; next} FNR==1 || $1 in F2 { print $1, $6, $7, $10, $12, $13, $24}' OFS='\t' file1 file2...
1 Like

Maybe?

awk -F "\t" 'FNR==NR{F2[$1]; next}FNR==1{print $1 "\t" $6 "\t" $7 "\t" $10 "\t" $12 "\t" $13 "\t" $24} ($1 in F2) {print $1 "\t" $6 "\t" $7 "\t" $10 "\t" $12 "\t" $13 "\t" $24}' file1 file2 > matched_output
1 Like

It worked perfectly! Thanks :slight_smile:

---------- Post updated at 02:17 PM ---------- Previous update was at 02:13 PM ----------

It works great too. thanks :slight_smile: