LinuxAnswers - the LQ Linux tutorial section.
Go Back > Forums > Linux Forums > Linux - General
User Name
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.


  Search this Thread
Old 08-28-2007, 12:25 PM   #1
LQ Newbie
Registered: Aug 2007
Posts: 1

Rep: Reputation: 0
Question Erase string from a batch of files


I've been trying to do this for several days. I need to erase a string from a huge amount of files, and I was wondering if a combination of shell commands would do the trick.

I search for the string like this:

grep -R "the_string_I_want_to_erase" *
I get a huge listing of files from which I want to remove this specific string. Is there a way to do this?

Thank you so much for the attention.
Old 08-28-2007, 12:38 PM   #2
LQ Addict
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian Squeeze
Posts: 5,840

Rep: Reputation: 342Reputation: 342Reputation: 342Reputation: 342
Pipe the result of the grep search through sed. Something along the lines of:
grep -R "the_string_I_want_to_erase" * | sed '-s/the_string_I_want_to_erase//g'

I don't use sed enough to be certain of the correct syntax. You should be able to google up a few examples/tutorials.

In case you aren't aware, is the place to search for Linux-specific information.

Last edited by bigrigdriver; 08-28-2007 at 12:39 PM.
Old 08-28-2007, 12:39 PM   #3
Senior Member
Registered: Sep 2002
Location: Nashville, TN
Posts: 1,552

Rep: Reputation: 53
A combination of find and perl should do the trick. Something similar to:
find /path/to/file -type f -exec perl -pi -e 's/the_string_I_want_to_erase//g' {} \;
Old 08-28-2007, 12:52 PM   #4
LQ Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,222
Blog Entries: 9

Rep: Reputation: 835Reputation: 835Reputation: 835Reputation: 835Reputation: 835Reputation: 835Reputation: 835
You should use the sed command for this kind of substitution:

An example that will LIST the files:

for FILE in `grep -Rl <string> *`
do echo $FILE
   sed -e s/<string>//g $FILE
   echo ""
The -Rl says to do recursive and list only the files (not the lines) that match.

The for loop uses each file found. (Of course you put your string rather than "<string>" in the line that starts with sed.

The sed line says to substitute what is between the first two slashes with what is between the last two slashes - since there is nothing between the last two slashes it essentially erases the original string. The g at the end tells it to do this globally (otherwise it would only do first occurrence).

The below has NOT been tested by me so if you run it you do so
at your own risk-it is provided for EXAMPLE purpose only.

The above does not change the actual files themselves - it just shows you what would be changed. To modify the files you'd have to expand the script somewhat:

for FILE in `grep -Rl <string> *`
do echo $FILE
   sed -e s/<string>//g $FILE >${FILE}.new
   cp ${FILE}.new $FILE
   rm ${FILE}.new 
   echo ""
The sed line was change to redirect output into a new file suffixed as .new. The next line copies the revised file over the original and then the rm removes the (The cp is done so the file maintains the permissions it had before you started.)


erase, files, string

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
Need script for cron to erase MT files DavidHB Linux - Software 4 04-30-2007 06:51 AM
Erase files madmax1143 Linux - Software 5 08-05-2006 05:23 PM
software to erase corrupted files? karl_marx Linux - Software 1 05-22-2006 08:16 PM
batch append string to the end of a determined line in text files osio Programming 6 06-30-2005 09:28 AM
How do you erase files in Terminal? darkarcon2015 Linux - Software 1 06-09-2004 05:56 PM

All times are GMT -5. The time now is 10:27 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration