shell script to add a new column at last
14,639
Solution 1
You can just use this awk:
awk 'BEGIN { OFS = "," } {print $0, "z"}' input.csv > output.csv
Or even simpler to use sed
:
sed 's/$/,z/' input.csv > output.csv
Solution 2
Your input file contains control-Ms, provided absolutely free by some Windows program. To get rid of them run dos2unix or similar on your file first and then run your awk script.
Author by
Ananya_Chandraker
Updated on June 04, 2022Comments
-
Ananya_Chandraker almost 2 years
Input file test.csv ================= 1,abc, 12,12-19 2,aslk,16,13-143 3,kjsd,45,11-137 4,ajsd,144,10-190 required output: ======================== 1,abc, 12,12-19,z 2,aslk,16,13-143,z 3,kjsd,45,11-137,z 4,ajsd,144,10-190,z
I am using the following command.
awk -F"," 'BEGIN { OFS = "," } {$5="z"; print}' input.csv > output.csv
but I am getting:
======================== 1,abc, 12,12-19 ,z 2,aslk,16,13-143 ,z 3,kjsd,45,11-137 ,z 4,ajsd,144,10-190 ,z
Please help me to get the required output.
-
Ananya_Chandraker over 9 yearsIf am using this then I am getting same result. and if I opening this using 'vi' editor then I am getting '^M' symbol before ',z' :(
-
anubhava over 9 yearsThat means you have
\r
like Windows file. Use this awk:awk -v RS='\r\n' 'BEGIN { OFS = "," } {print $0, "z"}' input.csv > output.csv
-
Ed Morton over 9 yearsDepending on your environment you might need to use gawk with
-v BINMODE=3
if you want to include the control-Ms in the RS. -
Ananya_Chandraker over 9 yearsHey Ed, Thanks, Yes our file is .csv made through SAS, and we are using it in Linux. But Anubhava's one line code worked for out requirement. Thanks a lot for your effort.