Shell script & csv problem to write in a file
I try to add information in acsv file with a shell script.
For开发者_如何学运维 the moment I add the information, my problem is that the csv file isn't empty at the beginning.
So I would like to put the new information in another column.
But I can't find the way to do this.
here is my code :
sed -i '1i column3' test.csv
sed -i '2i;;4' test.csv
sed -i '3i;;2' test.csv
The output that I have is
column 3
4 2
1 2
3 4
And I would like this:
1 2 column 3
3 4 4
2
Do you think it's possible?
thanks
This should do what you wrote in your example:
sed -i '1s/.*/& column3/' test.csv
sed -i '2s/.*/& 4/' test.csv
with awk (if you want to use space as a separator just leave out the "-vOFS=, -F," part):
awk -vOFS=, -F, 'NR==1 {$3="col3"; print;}' test.csv
awk -vOFS=, -F, 'NR==2 {$3=4; print;}' test.csv
Change the first column:
awk -vOFS=, -F, 'NR==1 {$1="col1"; print;}' test.csv
Insert a column between the 2:
awk -vOFS=, -F, 'NR==1 {print $1,"newcol",$2;}' test.csv
精彩评论