LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-23-2015, 09:47 AM   #1
Haba2015
LQ Newbie
 
Registered: Mar 2015
Posts: 10

Rep: Reputation: Disabled
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.
 
Old 03-23-2015, 10:21 AM   #2
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
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]
 
Old 03-23-2015, 01:49 PM   #3
Haba2015
LQ Newbie
 
Registered: Mar 2015
Posts: 10

Original Poster
Rep: Reputation: Disabled
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
 
Old 03-23-2015, 04:03 PM   #4
Haba2015
LQ Newbie
 
Registered: Mar 2015
Posts: 10

Original Poster
Rep: Reputation: Disabled
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
 
Old 03-24-2015, 05:51 AM   #5
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
I used your code from the earlier post
Code:
cut -d, -f 1
instead of
Code:
cut -d, -f 2-
 
Old 03-24-2015, 06:03 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,945

Rep: Reputation: 7325Reputation: 7325Reputation: 7325Reputation: 7325Reputation: 7325Reputation: 7325Reputation: 7325Reputation: 7325Reputation: 7325Reputation: 7325Reputation: 7325
if awk:
Code:
awk ' BEGIN{FS=OFS=","}NR>1{$1="";sub(",","");print}'
and no sed required
 
Old 03-24-2015, 06:22 AM   #7
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,141

Rep: Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123
Or even
Code:
awk ' BEGIN{FS=OFS=","}NR>1{sub(/[^,]+,/,"");print}'

Last edited by syg00; 03-24-2015 at 06:23 AM. Reason: oops
 
Old 03-24-2015, 07:46 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
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
 
Old 03-24-2015, 07:56 AM   #9
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,141

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


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] only the header line row to column atjurhs Linux - Newbie 5 08-30-2012 09:26 AM
[SOLVED] Delete row of csv file that doesn't meet a criteria arbex5 Programming 9 07-06-2012 02:18 PM
[SOLVED] find a null value in a row/column and delete entire row umix Linux - Newbie 10 10-13-2011 01:26 AM
vi editor - how to delete a character in each row of a column? flipwhy Linux - Newbie 6 01-22-2007 02:12 PM
bash script - remove header row from csv file pljvaldez Programming 5 08-30-2006 11:05 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration