LinuxQuestions.org
Review your favorite Linux distribution.
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, 10: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, 11:21 AM   #2
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 979
Blog Entries: 2

Rep: Reputation: 235Reputation: 235Reputation: 235
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, 02: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, 05: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, 06:51 AM   #5
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 979
Blog Entries: 2

Rep: Reputation: 235Reputation: 235Reputation: 235
I used your code from the earlier post
Code:
cut -d, -f 1
instead of
Code:
cut -d, -f 2-
 
Old 03-24-2015, 07:03 AM   #6
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

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

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

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

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
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, 08:56 AM   #9
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,833

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 10:26 AM
[SOLVED] Delete row of csv file that doesn't meet a criteria arbex5 Programming 9 07-06-2012 03:18 PM
[SOLVED] find a null value in a row/column and delete entire row umix Linux - Newbie 10 10-13-2011 02:26 AM
vi editor - how to delete a character in each row of a column? flipwhy Linux - Newbie 6 01-22-2007 03:12 PM
bash script - remove header row from csv file pljvaldez Programming 5 08-30-2006 12:05 PM


All times are GMT -5. The time now is 10:03 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration