aspose file tools*
The moose likes Linux / UNIX and the fly likes Script to Find & Remove text in 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 "Script to Find & Remove text in multiple files." Watch "Script to Find & Remove text in multiple files." New topic
Author

Script to Find & Remove text in multiple files.

Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140
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


Mani
Quaerendo Invenietis
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

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:


http://home.arcor.de/hirnstrom/bewerbung
Peter Rooke
Ranch Hand

Joined: Oct 21, 2004
Posts: 805

And if the above is not enough, you could do something like:


[ May 03, 2006: Message edited by: Peter Rooke ]

Regards Pete
Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140
Great. That helped a lot.
Many thanks to Stefan & Peter.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Script to Find & Remove text in multiple files.