Add Column to end of CSV file using 'awk' in BASH script

Related searches

How do you add a column to the end of a CSV file with using a string in a variable?

2012-02-29,01:00:00,Manhattan,New York,234
2012-02-29,01:00:00,Manhattan,New York,843
2012-02-29,01:00:00,Manhattan,New York,472
2012-02-29,01:00:00,Manhattan,New York,516
2012-02-29,01:00:00,Manhattan,New York,234,2012-02-29 16:13:00
2012-02-29,01:00:00,Manhattan,New York,843,2012-02-29 16:13:00
2012-02-29,01:00:00,Manhattan,New York,472,2012-02-29 16:13:00
2012-02-29,01:00:00,Manhattan,New York,516,2012-02-29 16:13:00

awk -F"," '{$6="2012-02-29 16:13:00" OFS $6; print}' input.csv > output.csv

My attempt above in added the string to the end but stripped all the comma separators. result
2012-02-29 01:00:00 Manhattan New York 234 2012-02-29 16:13:00
2012-02-29 01:00:00 Manhattan New York 843 2012-02-29 16:13:00
2012-02-29 01:00:00 Manhattan New York 472 2012-02-29 16:13:00
2012-02-29 01:00:00 Manhattan New York 516 2012-02-29 16:13:00

Appreciate any help!


TIMESTAMP=$(date +"%F %T")

#Append 'Create' DateTimeStamp to CSV for MySQL logging
$GAWK -v d="$TIMESTAMP" -F"," 'BEGIN {OFS = ","} {$6=d; print}' $ORIG_FILE > $NEW_FILE
rm -f $ORIG_FILE

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


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:

awk -v d="$mydate" -F"," 'BEGIN { OFS = "," } {$6=d; print}' input.csv > output.csv

Python: Add a column to an existing CSV file –, Insert a column as second column with same values into an existing csv. In all the above examples we added a column in the end of csv file i.e.� Add a column with same values to an existing CSV file. Open ‘input.csv’ file in read mode and create csv.reader object for this csv file. Open ‘output.csv’ file in write mode and create csv.writer object for this csv file. Using reader object, read the ‘input.csv’ file line by line. For each row

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

Need to add column at the end of csv, So, actually you want to append a , and the double-quoted contents of $ formatted_date to the end of every line? You can do this with sed "s/$/� That is my sample code i want to append a whole column to my existing HOG.CSV file which contain features of images. i am tryi but each time when i read the column that i want to append to HOG.CSV, it overwrite all the columns of HOG.CSV file.

You can set the OFS (output field seperator):

awk -F"," 'BEGIN { OFS = "," } ; {$6="2012-02-29 16:13:00" OFS $6; print}' input.csv >output.csv

which gives me:

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,

AWK : Add columns in the end of csv file, AWK : Add columns in the end of csv file. Original Post by villebonnais. villebonnais. 07:24 AM 03-28-2011. Hi everybody, I need some help please. I have a csv� To insert a new column after the last column $ awk -F, ' {$ (NF+1)=++i;}1' OFS=, file Unix,10,A,1 Linux,30,B,2 Solaris,40,C,3 Fedora,20,D,4 Ubuntu,50,E,5 $NF indicates the value of last column. Hence,by assigning something to $ (NF+1), a new field is inserted at the end automatically.

If anyone wants to create csv file through shell with column names: where first input stored in variables from_time, to_time.

example:insert two timestamps with from_time and to_time as column names with respective values -


{ echo -e "$FROM_TIME,$TO_TIME";}>input1.csv
{ echo -e "from_time,to_time"; cat input1.csv;} > input.csv

first line storing the values second line responsible for adding column name

UNIX, The issue was solved in comments without proper explanation: Surrounding sed script with single quotes doesn't work? 's/$/;"newc"/'. 1. I have an existing CSV file with 12 columns in it containing financial information. This CSV is sorted. I need to ADD two columns: 'product item number' (a 7 digit int) and 'product item number ID' (a 6 digit int). 2.

Adding a new column to an existing csv file via Matlab., The final output i want to have is a csv file with 23 columns and 400 rows. the " num2cell(d1(1:end,7))" is the new column that i want to add. Assign the csv file to some temporary variable(df). df = pandas.read_csv("____.csv") define the data you want to add color=[‘red’ , ’blue’ , ’green

Keys in this dictionary matches with the column names in csv file. To add the contents of this dictionary as a new row in the csv file, we need to follow these steps, import csv module’s DictWriter class, Open our csv file in append mode and create a file object, Pass the file object & a list of csv column names to the csv.DictWriter(), we

› How to insert filename into all txt files in a given folder › prefix 2nd column in csv file in unix box › Batch to add Filename as First Column › [Solved] Append filename to end of each row in .csv file › [Solved] Batch processing- Convert row to column in text files › Csv file editing using batch › bash script to add additional

  • Isn't it 'ManhattAn' rather than 'ManhattEn'?
  • Probably is, I was just quickly typing it for an example.
  • Thanks @Birei. I wasn't sure what OFS was but it makes sense now. Tried your code and it worked.
  • Do you know how I can add the date through a variable instead of the hard coded timestamp? I'll add my revised code above to see it better.
  • @SirOracle: Updated the answer.
  • @Birei, I found you code is useful , I have packaged your script and uploaded as #sparrow plugin so that everyone could reuse it -
  • @Birei, nice one thanx. How can anyone include the quotes in the string to be inserted? I wanted to add the string "2.4.0" (inluding quotes) in the second column of a .csv file, but the code below does not work: awk -F"," 'BEGIN { OFS = "," } {$2="\"2.4.0"\"; print}' test.csv > output.csv
  • or: awk -v date="2012-02-29 16:13:00" -v OFS=, '{print $0, date}'
  • Thanks Jörg Beyer. I removed the OFS $6 using @Birei example below to get rid of the trailing comma. I'm just trying to figure out how to add a variable in place of the hard coded date. I updated the question above with an example.
  • You should edit your answer, it's not clear and it contains errors (second echo has a unmatched double quote, for example)