LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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.

Notices



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

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


Hi,

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:

Code:
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, 01:38 PM   #2
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian Squeeze
Posts: 5,805

Rep: Reputation: 324Reputation: 324Reputation: 324Reputation: 324
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, www.google.com/linux is the place to search for Linux-specific information.

Last edited by bigrigdriver; 08-28-2007 at 01:39 PM.
 
Old 08-28-2007, 01:39 PM   #3
stickman
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, 01:52 PM   #4
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,028
Blog Entries: 5

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

An example that will LIST the files:

Code:
for FILE in `grep -Rl <string> *`
do echo $FILE
   sed -e s/<string>//g $FILE
   echo ""
done
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).


WARNING: BEFORE PROCEEDING *TEST* ON NON-CRITICAL DIRECTORY/FILES
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:

Code:
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 ""
done
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 file.new. (The cp is done so the file maintains the permissions it had before you started.)
 
  


Reply

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


All times are GMT -5. The time now is 07:01 PM.

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