1

Hi I have two csv files having same columns like,

x.csv

column1,column2
A,2 
B,1

y.csv

column1,column2
A,1
C,2

I want the output like:

z.csv

column1,column2
A,2
B,1
C,2

i.e. for the matching data in first column, I want to keep the x.csv record and for a new field in y.csv (like A,2) i just want to append it (like C,2).

Thanks

1 Answer 1

3
$ awk -F, 'NR==FNR{a[$1]; print; next} ! ($1 in a)' x.csv y.csv
column1,column2
A,2 
B,1
C,2

How it works

  • -F,

    This tells awk to use a comma as the field separator

  • NR==FNR{a[$1]; print; next}

    While reading the first file (NR==FNR), this tells awk to (a) to add $1 as a key to the associative array a, (b) print the line, and (c) skip the remaining commands and jump to the next line in a file.

  • ! ($1 in a)

    If we get here, that means we are working on the second file. In that case, we print the line if the first field is not a key of array a (in other words, if the first field did not appear in the first file).

Sign up to request clarification or add additional context in comments.

3 Comments

@EdMorton I know it works that way for awks I use. Do you know if it is guaranteed to work that way? I have not found this issue documented in POSIX.
Is the next statement really neccessary since the ! ($1 in a) will never match the first file?
@EdMorton Very good. Thanks! Answer updated to remove =1.

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.