LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-05-2009, 02:36 PM   #1
guest
Member
 
Registered: May 2003
Distribution: CentOS 5 64 bit
Posts: 255

Rep: Reputation: 30
Bash: How to add a line on top of text files


I have a bunch of CSVs (250 of them) that doesn't have a header, so I need to add a header

Does anyone know a command that will add a header to all files like so?
NAME, AGE, CITY, STATE


Thanks!
 
Old 02-05-2009, 02:39 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Using sed?
Code:
sed -i '1iNAME, AGE, CITY, STATE' file.csv
include this command in a for loop if you want to repeat for every CSV file.
 
Old 02-05-2009, 02:49 PM   #3
guest
Member
 
Registered: May 2003
Distribution: CentOS 5 64 bit
Posts: 255

Original Poster
Rep: Reputation: 30
Is there a command that just edits all of them? like *.csv?
 
Old 02-05-2009, 03:03 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by guest View Post
Is there a command that just edits all of them? like *.csv?
That works, but I have heard people argue that the loop is more robust. Why not try it??

So--2 choices:

sed -i '1iSTUFF' *.csv

for fil in *.csv; do sed -i 'i1STUFF' $fil; done
 
Old 02-05-2009, 03:35 PM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
No need for sed here, bash has everything you need.
Code:
echo "NAME, AGE, CITY, STATE" > headerfile
for csv in *.csv; do cat headerfile $csv > tmpfile2; mv tmpfile2 $csv; done
rm headerfile
--- rod.
 
Old 02-05-2009, 04:43 PM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by theNbomr View Post
No need for sed here, bash has everything you need.
Code:
echo "NAME, AGE, CITY, STATE" > headerfile
for csv in *.csv; do cat headerfile $csv > tmpfile2; mv tmpfile2 $csv; done
rm headerfile
--- rod.
Not really. sed -i is one external command, while cat + mv + rm are three!
 
Old 02-05-2009, 05:14 PM   #7
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
Originally Posted by colucix View Post
Not really. sed -i is one external command, while cat + mv + rm are three!
Doh! Yes, quite true. I guess I always think of these tiny tools as part of bash, and sometimes, they are as builtins. Good catch.
--- rod.
 
Old 02-05-2009, 05:39 PM   #8
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 47
Moin,

Quote:
Originally Posted by colucix View Post
Not really. sed -i is one external command, while cat + mv + rm are three!
I wonder about the method sed uses to do the inplace editing. I think that it can't be done in-memory, some temporary files should have to be created. That's why I'd prefer this method (not tested):
Code:
mkdir new_dir
cd old_dir
for f in *.csv; do
  (echo "NAME, AGE, CITY, STATE"; cat $f) >../new_dir/$f
done
cd ..
rm -r old_dir
mv new_dir old_dir
This method uses only one simple cat instead of the sed monster and additionally allows you to break every time you receive an error and keep the original files as long as all new files are created, but temporary needs a free space on harddisk similar to the source folder's space.

Jan
 
Old 02-05-2009, 05:59 PM   #9
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Nice, jan61. Your method removes the need for one 'rm'.
Code:
for csv in *.csv; do (echo "NAME, AGE, CITY, STATE"; cat $csv) > tmp; mv tmp $csv; done
I knew someone would come up with a method for that. Thanks.
--- rod.
 
Old 02-05-2009, 11:20 PM   #10
guest
Member
 
Registered: May 2003
Distribution: CentOS 5 64 bit
Posts: 255

Original Poster
Rep: Reputation: 30
Wow thanks guys! You guys are such a life saver!!
 
Old 02-06-2009, 12:29 AM   #11
guest
Member
 
Registered: May 2003
Distribution: CentOS 5 64 bit
Posts: 255

Original Poster
Rep: Reputation: 30
but wait.. what happens if there's spaces in the files? how do you handle that? the script doesn't work with filenames with spaces..
 
Old 02-06-2009, 12:30 AM   #12
guest
Member
 
Registered: May 2003
Distribution: CentOS 5 64 bit
Posts: 255

Original Poster
Rep: Reputation: 30
nevermind.. this fixed it all! lol

sed -i '1iSTUFF' *.csv

'nuf sed
 
Old 02-07-2009, 06:23 AM   #13
shyamkumar1986
LQ Newbie
 
Registered: Feb 2009
Posts: 19

Rep: Reputation: 1
Cool

Quote:
Originally Posted by theNbomr View Post
No need for sed here, bash has everything you need.
Code:
echo "NAME, AGE, CITY, STATE" > headerfile
for csv in *.csv; do cat headerfile $csv > tmpfile2; mv tmpfile2 $csv; done
rm headerfile
--- rod.
@theNbomr - Pretty neat script ... I was able to use this for another similar requirement I was working on today.

Thanks!!!

Last edited by shyamkumar1986; 02-09-2009 at 08:42 AM.
 
  


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
BASH: read every line in the files and use the line as parameters as another program tam3c36 Programming 10 12-07-2010 01:42 PM
How to add a text to first line of a text file? oskeewow Linux - Newbie 6 04-23-2008 12:40 PM
How to add a new line in bash divya_linux Programming 4 10-17-2007 09:07 AM
how to change some text of a certain line of a text file with bash and *nix scripting alred Programming 6 07-10-2006 11:55 AM
Bash script text line into an array toolshed Programming 1 06-13-2005 05:49 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06: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
Open Source Consulting | Domain Registration