You're viewing a comment by Haba and its responses.

Haba Permalink
March 23, 2015, 18:11

I have several of csv files, I want to remove the first row (header) and first column as well. This is my code, but it overwrites the original files with no content inside:

for x in *.csv;
do
sed '1d' $x | cut -d, -f 1 > "$x";

done

Help would be highly appreciated.

Comment Responses

March 23, 2015, 19:11

You don't need to loop over files. You can use sed's -i argument. This argument replaces data in-place and rewrites the file, and creates a backup .bak version of the file.

Here's how you do it:

sed -i.bak '1d; s/^[^,]\+,//; s/^\s\+//' *.csv

Explanation:

1d delete the first line and goes to next line.

s/^[^,]\+,// delete everything from the start to the first comma.

s/^\s\+// cleanups any whitespace at the beginning.

Reply To This Comment

(why do I need your e-mail?)

(Your twitter handle, if you have one.)

Type the word "unix_122": (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.