aspose file tools*
The moose likes Linux / UNIX and the fly likes Command to add lines to multiple files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "Command to add lines to multiple files" Watch "Command to add lines to multiple files" New topic
Author

Command to add lines to multiple files

Matt Kidd
Ranch Hand

Joined: Jul 17, 2002
Posts: 261
I have 3 lines associated to svn that I want to add to all the files in the repository. We have a text editor that integrates grep and he told me that I can use that to add the lines en masse but I don't know of a way to insert via grep through this tool. As it stands now i can only replace lines and I can't find a common string through out the code base that I can replace.

is there a way to tell grep insert the following lines at the beginning of the file?
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Sure it was grep and not sed?


http://home.arcor.de/hirnstrom/bewerbung
Matt Kidd
Ranch Hand

Joined: Jul 17, 2002
Posts: 261
Originally posted by Stefan Wagner:
Sure it was grep and not sed?


thanks...I'll have to do some work but that will do it.
Anand Hariharan
Rancher

Joined: Aug 22, 2006
Posts: 257

You could use sed like one poster suggested, or you could have those 3 lines saved as a file. Let's say you call this file svn_header.

Now all you have to do is -

cat svn_header source_file > source_file_with_header
mv source_file_with_header source_file

You have to do this "redirection dance" with sed anyway, and since you are always adding the lines at the very beginning, you should be okay with just using cat.

If you decide to go with Perl, you can do an in-place edit.

HTH,
- Anand


"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- Antoine de Saint-Exupery
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923


You have to do this "redirection dance" with sed anyway, and since you are always adding the lines at the very beginning, you should be okay with just using cat.


No.

will change the file in place.
Anand Hariharan
Rancher

Joined: Aug 22, 2006
Posts: 257

Originally posted by Stefan Wagner:


No.

will change the file in place.


That's a GNU extension, not in POSIX sed.
Matt Kidd
Ranch Hand

Joined: Jul 17, 2002
Posts: 261
interesting. So sed can do this. I like the idea of cat since I have different types of files (java, api, wo, wod, html, sql, etc) I have to add the svn stuff too which will require different comment styles. I can call sed at the command line via a java file or I could write a shell script.
Matt Kidd
Ranch Hand

Joined: Jul 17, 2002
Posts: 261
got it. java program to call a different sed command at the command line depending on the file type. Have the svn headers in the directory already before I run it. Sound good?
Matt Kidd
Ranch Hand

Joined: Jul 17, 2002
Posts: 261
Originally posted by Stefan Wagner:


No.

will change the file in place.


okay I'm just going to write a shell script. What would I do for the SEDCOMMAND that would be applied to FILE to insert various comments into the beginning of the file?


I've been googling and it seems I need to use a shell script of some sort and if that's the case a simple .+ should cover it. Am I right in this regard?
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923


will insert a

comment at line 1 (i1:insert at Line 1).

To apply this for multiple files in directory and all subdirectories:


[ April 11, 2007: Message edited by: Stefan Wagner ]
 
wood burning stoves
 
subject: Command to add lines to multiple files