Solution for replacement of 4th column with 3rd column in a file using awk/sed preserving delimters

input

"A","B","C,D","E","F"
"S","T","U,V","W","X"
"AA","BB","CC,DD","EEEE","FFF"

required output:

"A","B","C,D","C,D","F"
"S", T","U,V","U,V","X"
"AA","BB","CC,DD","CC,DD","FFF"

tried using awk but double quotes not preserving for every field. any help to solve this is much appreciated.

cat input | awk -v FS="\",\"" '{ $4=$3;print }'
"A B C,D C,D F"
"S T U,V U,V X"
"AA BB CC,DD CC,DD FFF"

You were very close, but you need to specify the output field separator as well as input field separator. (And there is no reason to read and write the contents of your input file twice using a pipeline instead of letting awk read the input file directly.) Try:

awk 'BEGIN{FS=OFS="\",\""}{ $4=$3;print }' input

or:

awk 'BEGIN{FS=OFS="\",\""}{ $4=$3 }1' input
2 Likes

Thanks for providing the solution.

Have another thing that I'm trying to solve for 4th column if empty replace with "novalue" what is the best way to get this in awk

col1,col2,col3,col4,col5,col6
"1","2","3","4","5","6"
"1","2","3","","5","6"
"a","b","c","","e","f"
col1,col2,col3,col4,col5,col6
"1","2","3","4","5","6"
"1","2","3","novalue","5","6"
"a","b","c","novalue","e","f"

Hello khblts,

Could you please try following and let me know if this helps you.

awk -F, 'NR==1{print;next} ($4 ~ /^\"\"$/){$4="\"novalue\""} 1' OFS=","   Input_file

Output will be as follows.

col1,col2,col3,col4,col5,col6
"1","2","3","4","5","6"
"1","2","3","novalue","5","6"
"a","b","c","novalue","e","f"

Thanks,
R. Singh

awk 'BEGIN{FS=OFS="\",\""}{$4=$3; gsub("\"\"", "\"novalue\"")}1' input

In addition to what RavinderSingh13 suggested, you could also try the slightly simpler:

awk 'BEGIN{FS=OFS=","} {sub(/""/, "\"novalue\"", $4)} 1' file