• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Command to add lines to multiple files

 
Matt Kidd
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure it was grep and not sed?
 
Matt Kidd
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 272
C++ Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 272
C++ Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 267
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 267
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 267
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic