Merging Multiple Columns between two files

Hello guys,
I have 2 CSV files which goes like this:
CSV1:

Breaking.csv:
UTF-8
"Name","Description","Occupation","Email"
"Walter White","","Chemistry Teacher","w.w@bb.com"
"Jessie Pinkman","","Junkie","j.p@bb.com"
"Hank Schrader","","DEA Agent","h.s@bb.com"

CSV2:

Bad.csv
"Name","Description"
"Walter White","Is Heisenberg"
"Jessie Pinkman","Is Captain Cook"
"Hank Schrader","Is Dead"

Basically I want to Merge the the two files but I want to get only the description for the other one. Output should look like this:

BreakingBad.csv:
UTF-8
"Name","Description","Occupation","Email"
"Walter White","Is Heisenberg","Chemistry Teacher","w.w@bb.com"
"Jessie Pinkman","Is Captain Cook","Junkie","j.p@bb.com"
"Hank Schrader","Is Dead","DEA Agent","h.s@bb.com"

Is this possible by using awk? Or is there any other method.

Thanks for the help. Always stay awesome :slight_smile:

Try:

awk -F"," 'NR==FNR{a[$1]=$2;next}{$2=a[$1]}1' OFS="," Bad.csv Breaking.csv > BreakingBad.csv
1 Like

I have the same problem just like this, problem is there is a first column preceding look below:

"Number","Name","Description","Occupation","Email"
"1","Walter White","","Chemistry Teacher","w.w@bb.com"
"2","Jessie Pinkman","","Junkie","j.p@bb.com"
"3","Hank Schrader","","DEA Agent","h.s@bb.com"

I want the same results but I am getting this:

$ awk -F"," 'NR==FNR{a[$1]=$2;next}{$3=a[$1]}1' OFS="," Bad.csv Breaking.csv
"Number","Name",,"Occupation","Email"
"1","Walter White",,"Chemistry Teacher","w.w@bb.com"
"2","Jessie Pinkman",,"Junkie","j.p@bb.com"
"3","Hank Schrader",,"DEA Agent","h.s@bb.com"

Can it be possible to not include headers too? :confused:

awk -F"," 'FNR==1{next}NR==FNR{a[$1]=$2;next}{$3=a[$1]}1' OFS="," Bad.csv Breaking.csv

Hi vgersh

I am getting hte same results:

$ awk -F"," 'FNR==1{next}NR==FNR{a[$1]=$2;next}{$3=a[$1]}1' OFS="," Bad.csv Breaking.csv
"Number","Name",,"Occupation","Email"
"1","Walter White",,"Chemistry Teacher","w.w@bb.com"
"2","Jessie Pinkman",,"Junkie","j.p@bb.com"
"3","Hank Schrader",,"DEA Agent","h.s@bb.com"

pls post both Bad and Breaking files and the desired result.

Same as the above, I just copied it, but nonetheless:

Breaking.csv
"Number","Name","Description","Occupation","Email"
"1","Walter White","","Chemistry Teacher","w.w@bb.com"
"2","Jessie Pinkman","","Junkie","j.p@bb.com"
"3","Hank Schrader","","DEA Agent","h.s@bb.com"
Bad.csv
"Name","Description"
"Walter White","Is Heisenberg"
"Jessie Pinkman","Is Captain Cook"
"Hank Schrader","Is Dead"

Desired results:

"Number","Name","Description","Occupation","Email"
"1","Walter White","Is Heisenberg","Chemistry Teacher","w.w@bb.com"
"2","Jessie Pinkman","Is Captain Cook","Junkie","j.p@bb.com"
"3","Hank Schrader","Is Dead","DEA Agent","h.s@bb.com"

Thanks for your help

Well, if you're looking for the name, that's in field 2 now:

awk -F","       'FNR==NR        {a[$1]=$2;next}
                 FNR>1          {$3=a[$2]}
                 1
                ' OFS="," Bad.csv Breaking.csv 
"Number","Name","Description","Occupation","Email"
"1","Walter White","Is Heisenberg","Chemistry Teacher","w.w@bb.com"
"2","Jessie Pinkman","Is Captain Cook","Junkie","j.p@bb.com"
"3","Hank Schrader","Is Dead","DEA Agent","h.s@bb.com"

And, don't do the replacement for line1 in breaking.csv...

BTW - don't hijack threads - open your own and refer back to the original one!