Given a CSV file,
h1 h2 h3
a b c
z x b
how to rename the first header to One
and the third header to Three
using command line tools, namely
One h2 Three
a b c
z x b
Given a CSV file,
h1 h2 h3
a b c
z x b
how to rename the first header to One
and the third header to Three
using command line tools, namely
One h2 Three
a b c
z x b
This should do it:
https://stackoverflow.com/questions/11145270/bash-replace-an-entire-line-in-a-text-file
The header should just be the first line in your file, so if you use N = 1, and rewrite the line with the desired headers, so:
sed -i '1s/.*/One, h2, Three/' file.csv
will do it
Sed has the "change" command c
to replace the whole current line:
sed -i '1c\One h2 Three' file.csv
This works with GNU sed. Others seds might need the text to be entered on a separate line:
sed -i '1c\
One h2 Three' file.csv
Line-based solutions (such as sed
-based) won't work for multiline csv headers.
So you need something to understand csv format.
For example with csvtool
(ocaml):
echo 'My,New,Header' > output.csv
csvtool drop 1 input.csv >> output.csv
The below command should work
sed -i '1s/h1/One/;1s/h3/Three/;' file.csv
-i
edits the file in place.
1
executes the operation in the first line.
s/h1/One/
substitutes h1 with One.