1

I want to write a script that will add new columns daily to a csv sheet.

The script will run daily and will append to csv file. Now how to edit csv file ? Edit will be via awk or sed commands. I will use this csv file to send mail to users and it will be used in excelsheets.

I have not found any relavent answers from all questions i have been through so please try to answer it .

5
  • 1
    Let's get clear on the meaning of edit. Do you want to change the file using an actual editor (like vi or ed) programmatically, or change the file using tools like sed or awk? The former is more involved. Commented Jun 22, 2013 at 10:48
  • via sed or awk. the script will run daily . It will capture some output from some other script and update the sheet according . Commented Jun 22, 2013 at 11:03
  • Doesn't adding columns ruin the structure of a CSV? Or do you mean adding rows? Commented Jun 22, 2013 at 11:05
  • No adding columns . I will store some test results. So initially the file will have only testcase names. Then everytime the script will run it will capture new results and update accordingly. Commented Jun 22, 2013 at 11:09
  • @rsplak - no, assuming some user will re-import the new file into excel or open office as a spreadsheet. Commented Jun 22, 2013 at 19:42

2 Answers 2

1

You can try something with awk:

awk 'BEGIN{FS=OFS=","}{$NF=$NF",new_stuff"}1' csv

$ cat csv 
1,shoes,red
2,apple,black
3,fog,blue
4,elephant,gray
5,monkey,green

$ awk 'BEGIN{FS=OFS=","}{$NF=$NF",new_stuff"}1' csv 
1,shoes,red,new_stuff
2,apple,black,new_stuff
3,fog,blue,new_stuff
4,elephant,gray,new_stuff
5,monkey,green,new_stuff
Sign up to request clarification or add additional context in comments.

2 Comments

partially correct , i think have to implement some sort of loop in new_stuff .
@AmarpreetSinghSaini If you can post some sample data in your question then may be we can work out a better solution.
0

so you want something like that :

sed 's/\(john mayers.*\)$/\1\,hallo/' file

file:

john mayers ,hi   ,hello
jack bauer  ,yo   ,wasup

output:

john mayers ,hi   ,hello,hallo
jack bauer  ,yo   ,wasup

1 Comment

something like this but multiple rows will be present. say 50 rows and 3 columns everytime

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.