LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 02-26-2009, 12:57 PM   #1
Rotwang
Member
 
Registered: Jan 2004
Distribution: CentOS
Posts: 281

Rep: Reputation: 30
A shell script to include a message on line 2 of every text file in a director tree?


So, I'm releasing something open source (woo!). And I want to include a message at the top of every source file in a directory tree. Actually, I want to add it on line 2 of every file (or "after line 1").

I could do this in perl or php, but I was wondering if there's some nifty shell script I can use with cat and find or something.

thanks
 
Old 02-26-2009, 01:32 PM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,492

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
You can try sed using the i command to insert one or more lines of text before line 2:
Code:
sed -i.bck '2i new line' list_of_files
this will do a backup copy of the original files, adding extension .bck. You can put the whole list of files as argument or eventually you can use this command in conjunction with find to look for files based on some criteria.
 
Old 02-26-2009, 02:18 PM   #3
Rotwang
Member
 
Registered: Jan 2004
Distribution: CentOS
Posts: 281

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by colucix View Post
You can try sed using the i command to insert one or more lines of text before line 2:
Code:
sed -i.bck '2i new line' list_of_files
this will do a backup copy of the original files, adding extension .bck. You can put the whole list of files as argument or eventually you can use this command in conjunction with find to look for files based on some criteria.
Ok almost perfect. The message I want to insert is actually multiple lines- can I stick the text from another file on line 2 rather than input from the quotes there?

I also don't want the backup, but I think I can figure out how to remove that by piping find "*.bck" and piping that to rm -f or something.
 
Old 02-26-2009, 03:09 PM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,492

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
Quote:
Originally Posted by Rotwang View Post
Ok almost perfect. The message I want to insert is actually multiple lines- can I stick the text from another file on line 2 rather than input from the quotes there?
Yes. To insert the content of another file use the command r. Suppose you have the text to insert in a file named insert.txt
Code:
sed -i '1r insert.txt' file1 file2 file3
This will insert the content of insert.txt after the first line in file1, file2 and file3.
Quote:
Originally Posted by Rotwang View Post
I also don't want the backup, but I think I can figure out how to remove that by piping find "*.bck" and piping that to rm -f or something.
Nope. Just remove the .bck from the -i option, as in my example above
Code:
sed -i blah blah blah
will edit files in place without doing the backup. Dangerous if you don't test the command before actually editing your original files!

Last edited by colucix; 02-26-2009 at 03:10 PM.
 
Old 02-26-2009, 05:01 PM   #5
Rotwang
Member
 
Registered: Jan 2004
Distribution: CentOS
Posts: 281

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by colucix View Post
Yes. To insert the content of another file use the command r. Suppose you have the text to insert in a file named insert.txt
Code:
sed -i '1r insert.txt' file1 file2 file3
This will insert the content of insert.txt after the first line in file1, file2 and file3.

Nope. Just remove the .bck from the -i option, as in my example above
Code:
sed -i blah blah blah
will edit files in place without doing the backup. Dangerous if you don't test the command before actually editing your original files!
Thanks, here's what I ended up using, based on what you gave me:

find ./ -name "*.php" -exec sed -i '1r ./eachfile.txt' {} \;

thanks.
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script loop though text file:-1 line at a time. knockout_artist Linux - Newbie 2 05-04-2008 06:58 PM
How to read a single line from a text file into a shell script. SkipHuffman Linux - Software 2 08-16-2006 02:10 PM
SSI include line from text-file? @ngelot Programming 4 05-11-2005 06:43 AM
Shell script - how to show a specific line of a text file davi_cabral Linux - Software 3 09-28-2004 01:39 PM
How to delete a line from a text file with shell script programming Bassam General 1 01-28-2004 08:51 PM


All times are GMT -5. The time now is 02:30 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration