1

Have to make a header for a csv file from unix shell script. It should be like below.

"Name","Place","Pincode","officename"

Tried like below. But it is not working

s_header= echo "Name","Place","Pincode","officename"

echo s_header>>filename.csv

Thanks you

2 Answers 2

7

You can use tool like sed. 1i means insert at first line.

 sed -i -e '1i"Name","Place","Pincode","officename"' filename.csv
Sign up to request clarification or add additional context in comments.

1 Comment

Note that this doesn't work on Macs - stackoverflow.com/questions/4247068/…
1

Suppose your csv file contents are in details.csv file. You create header.csv file by printing headers into it.You also can probably try below.

echo "Name","Place","Pincode","officename" > header.csv && cat details.csv >> header.csv && mv header.csv details.csv

Steps:

1)Print the headers into file name header.csv

2)Append the contents of the csv file contents(details.csv) into header.csv

3)Rename the header.csv file back to your original file details.csv

Comments

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.