Add Column to end of CSV file using 'awk' in BASH script
You may add a comma to OFS
(Output Field Separator):
awk -F"," 'BEGIN { OFS = "," } {$6="2012-02-29 16:13:00"; print}' input.csv > output.csv
Output:
2012-02-29,01:00:00,Manhatten,New York,234,2012-02-29 16:13:00
2012-02-29,01:00:00,Manhatten,New York,843,2012-02-29 16:13:00
2012-02-29,01:00:00,Manhatten,New York,472,2012-02-29 16:13:00
2012-02-29,01:00:00,Manhatten,New York,516,2012-02-29 16:13:00
EDIT to answer the comment of SirOracle
:
From awk
man page:
-v var=val
--assign var=val
Assign the value val to the variable var, before execution of the program begins. Such
variable values are available to the BEGIN block of an AWK program.
So assign your date to a shell variable and use it inside awk
:
mydate=$(date)
awk -v d="$mydate" -F"," 'BEGIN { OFS = "," } {$6=d; print}' input.csv > output.csv
I'd do:
awk '{ printf("%s,2012-02-29 16:13:00\n", $0); }' input.csv > output.csv
This hard codes the value, but so does your code.
Or you can use sed
:
sed 's/$/,2012-02-29 16:13:00/' input.csv > output.csv