Adding header to an existing csv and populate it with values

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Adding header to an existing csv and populate it with values

Sahil
Hi,
I have 2 queries here-
1-I am trying to add a header to an existing file having header and data and correspondly wants to populate it with some data...for eg

Name, Subject, Code
ABC,Physics,1001
XYZ,chem,1002
....
upto 10000 records.

Now suppose i want to add a column "Stream"  here at the end with data as "Science". so output should be like this-
Name, Subject, Code, Stream
ABC,Physics,1001,Science
XYZ,chem,1002,Science
.....
upto 10000 records.
I have read so many awk tricks to do but still not able to achieve it.


2- I want to delete blank rows from a csv file. From net i got this code to achieve it-

sed -i '/^$/d' file.csv

however it is not deleting blank rows.

Can anyone plz help me with it. I am new to shell scripting.

thanks in advance
Reply | Threaded
Open this post in threaded view
|

Re: Adding header to an existing csv and populate it with values

Guru
Administrator
1. For the fist one:

awk 'NR==1{$(NF+1)="Stream"}NR!=1{$(NF+1)="Science"}1' OFS=, file


2. Refer to the link, example 11.
http://www.theunixschool.com/2012/06/sed-25-examples-to-delete-line-or.html