user3767953
user3767953

Reputation: 141

merge two csv files, command wont print first line

I have two csv files and a command to merge them but it wont print the first line.

File1.csv

1003085,1
1003144,2
1003149,2
1003208,1
101203,1

File2.csv

131567,489.564273848
2759,489.564273848
33630,58.5245463474
554915,63.4537671111
1003085,74.6
1003144,20.365
1003149,486.39431

Wanted output.csv

1003085,1,74.6
1003144,2,20.365
1003149,2,486.39431
1003208,1
101203,1

The command I am using:

awk -F, -v OFS="," 'NR==FNR&&NR>1{a[$1]=$2 FS $3;next} FNR>1{printf "%s%s\n",$0,($1 in a?FS a[$1]:"")}' file2.csv file1.csv > out.csv

Upvotes: 1

Views: 35

Answers (1)

shellter
shellter

Reputation: 37278

NR>1 means "Number (of) Record" Greater than (>) 1. Remove that and the FNR>1 (current File Number (of) Record) and you should be OK.

If you inherited this code, it is probably designed to eliminate printing the header row.

IHTH

Upvotes: 2

Related Questions