Skip to content
Advertisement

How can I add a line to a file in a shell script?

I want to add a row of headers to an existing CSV file, editing in place. How can I do this?

echo 'one, two, three' > testfile.csv

and I want to end up with

column1, column2, column3
one,     two,     three

Changing the initial CSV output is out of my hands.

Any standard command will do. The important thing is the file is edited in place, and the line is inserted at the beginning of the file.

Advertisement

Answer

This adds custom text at the beginning of your file:

echo 'your_custom_escaped_content' > temp_file.csv
cat testfile.csv >> temp_file.csv
mv temp_file.csv testfile.csv
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement