Shell – replace column of a csv file with column from another file

awkcsvperlsedshell-script

I have two sample files like this:

$ cat file1
abc,sachin
cat,kumar 

$ cat file2
xyz
pressure

$ cat file3 
xyz,sachin
pressure, kumar 

I want the first column of file1 to be replaced with file2.

I tried doing something like this:

$ awk 'FNR==NR{a[NR]=$3;next}{$2=a[FNR]}1' file1 file2

I'm using Solaris 10 and it didn't appear to support this. Any other suggestions?

Best Answer

How about using cut and paste? if your shell supports process substitutions, then

$ paste -d, file2.txt <(cut -d, -f2 file1.txt)
xyz,sachin
pressure,kumar 
Related Question