0

I have one file with 11 columns with first column as primary id - P1 second csv with three columns with first column as same primary id - P1, though not at same level in both files, I am merging both files using below command:

awk 'NR==FNR {h[$2] = $3; next} {print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,h[$2]}' first.csv second.csv > final.csv

however, getting only three columns in new csv

2 Answers 2

1

You should see if join wouldn't be an easier solution. Type man join for that:

join - join lines of two files on a common field
Sign up to request clarification or add additional context in comments.

1 Comment

i have used join -t, <(sort -t, -k1 second.csv) <(sort -t, -k1 first.csv) > third.csv, however it is giving illegal byte sequence error
0

If first.csv has 11 columns and second.csv has three, then you have your files are in the wrong order. Try like this:

awk 'NR==FNR {h[$2] = $3; next} {print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,h[$2]}' second.csv first.csv > final.csv

You are also not using the first column as keys in this example, but the second one.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.