Vonton
Vonton

Reputation: 3372

merging file - whats wrong?

Where is a problem in my script I would like to compare $1 (first file) with $21 (second file). everything by awk and tab delimited.

INPUT:
1st file:
1         soup
3         bread
5         roll

2nd file:
a....$20   1
b....$20   2
c....$20   3
d....$20   4
e....$20   5

OUTPUT
a....$20   1   soup
b....$20   2   
c....$20   3   bread
..etc..

MY SCRIPT


awk -F'\t' -v OFS="\t" 'NR==FNR{a[$1]=$2; next} {if($21 in a) print $0a,[$2]; else print $0}' file2 file1

Thank you.

Upvotes: 1

Views: 40

Answers (1)

Kent
Kent

Reputation: 195169

in your codes, change:

if($21 in a) print $0a,[$2]

into

if($21 in a) print $0, a[$21]

also, change the parameter to your awk command

from:

file2, file1

into:

file1, file2

Upvotes: 2

Related Questions