Compare 2 files using awk but exclude header record from each file

Hi Forum.

I have 2 files (a.txt, b.txt) that I need to match on the first column but need to exclude the header records during comparison from each of the 2 files. This is what I have so far.

a.txt
# header record
1,1,1,1,
2,3,4,5
3,4,5,6

b.txt
# header record
2,two,three,foor

Code:
awk -F","  'FNR==NR{a[$1]=$2 FS $3","$4","$5","$6","$7","$8;next} $1 in a {print $0, a[$1]}' OFS='|' b.txt a.txt

Output using current code:
#|,,,,,,
2,3,4,5|two,three,foor,,,,

Desired Output:
2,3,4,5|two,three,foor,,,,

Thank you for all your help.

wouldn't FNR>1 do the job?

Where would I put that additional condition in my code?

I tried the following but not getting the desired output.

awk -F","  'FNR > 1; FNR==NR{a[$1]=$2 FS $3","$4","$5","$6","$7","$8;next} $1 in a {print $0, a[$1]}' OFS='|' b.txt a.txtawk -F","  'FNR==NR{a[$1]=$2 FS $3","$4","$5","$6","$7","$8;next} $1 in a {print $0, a[$1]}' OFS='|' b.txt a.txt

Output:
#|,,,,,,
2,3,4,5|two,three,foor,,,,

awk -F"," 'FNR>1' 'FNR==NR{a[$1]=$2 FS $3","$4","$5","$6","$7","$8;next} $1 in a {print $0, a[$1]}' OFS='|' b.txt a.txt
2,two,three,foor

Output:
1,1,1,1,
2,3,4,5
3,4,5,6

Thanks.

awk -F","  'FNR==1{next}; ....'
2 Likes

This is the final code after some help from vgersh99:


awk -F"," 'FNR==1{next};FNR==NR{a[$1]=$2 FS $3","$4","$5","$6","$7","$8;next} $1 in a {print $0, a[$1]}' OFS='|' a.txt b.txt