• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Script to Find & Remove text in multiple files.

 
Mani Ram
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm new to Unix shell scripting. Will be thankful if someone can help me out.

My requirement is to traverse through a directory structure (including sub-directories), search for a particular string in all Java files (eg. System.out.print), and delete those lines.
Can anyone can help me to write such a script.

Note: There are lot of other activities involved. For example, if a file has that particular line, I need to check-it out first before performing a delete. Similarly, I also have to check-in the file once the update is done. So, it will be better if the script is divided into multiple functions, so that I can call them as and when required.

Thanks
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To search for a file, you can use find:

To delete every line which contains System.out.print, you could use sed:


find and sed are very mighty commands; the man-page will show you their options.

You may combine both:


For more complicated stuff, like checking out files, I would start with grep, to gather all filenames where an expression is found:


and operate with a script on that list of files:
 
Peter Rooke
Ranch Hand
Posts: 848
1
Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if the above is not enough, you could do something like:


[ May 03, 2006: Message edited by: Peter Rooke ]
 
Mani Ram
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great. That helped a lot.
Many thanks to Stefan & Peter.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic