LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to delete header (first row) and first column from multiple CSV files (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-delete-header-first-row-and-first-column-from-multiple-csv-files-4175537606/)

Haba2015 03-23-2015 09:47 AM

How to delete header (first row) and first column from multiple CSV files
 
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.

linosaurusroot 03-23-2015 10:21 AM

You could use a temporary filename if you can think of one that's definitely not in use.

[code]
sed '1d' $x | cut -d, -f 1 > replace_$x && mv replace_$x $x
[//code]

Haba2015 03-23-2015 01:49 PM

Hi linosaurusroot,

the code: for x in *.csv; do sed '1d' $x | cut -d, -f 1 > replace_$x && mv replace_$x $x ; done is deleting the files and only retain the column 1 , I am intending to cut

Haba2015 03-23-2015 04:03 PM

Thank you: This works:

for x in *.csv; do sed '1d' $x| awk 'BEGIN{FS=OFS=","}{$1="";sub(",","")}1' > rippe_$x && mv rippe_$x $x ; done

linosaurusroot 03-24-2015 05:51 AM

I used your code from the earlier post
Code:

cut -d, -f 1
instead of
Code:

cut -d, -f 2-

pan64 03-24-2015 06:03 AM

if awk:
Code:

awk ' BEGIN{FS=OFS=","}NR>1{$1="";sub(",","");print}'
and no sed required

syg00 03-24-2015 06:22 AM

Or even
Code:

awk ' BEGIN{FS=OFS=","}NR>1{sub(/[^,]+,/,"");print}'

grail 03-24-2015 07:46 AM

I see no issue with sed either:
Code:

sed -i -e '1d' -e 's/[^,]*,//' *.csv
If you append something to 'i' option it will also make a backup, eg -i.bak

syg00 03-24-2015 07:56 AM

Not like you grail to use two "-e" when one would suffice .... :p


All times are GMT -5. The time now is 04:26 AM.