LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-14-2010, 03:31 PM   #1
skeates
LQ Newbie
 
Registered: Oct 2010
Posts: 3

Rep: Reputation: 0
Search and remove


Good Day all.

I want to be able to check the contents of a text file for a specific string and remove it from the file from the command prompt.

I would basically be searching through a number of files and if a specific string is found I would like it removed automatically.

pretty much a find and replace, were the replace is nothing. any one got any ideas on how you would do this. I already have the search part sorted just need to be able to remove the string I don't want from the multiple files.

Thanks
 
Old 10-14-2010, 03:39 PM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
sed
 
Old 10-14-2010, 04:27 PM   #3
skeates
LQ Newbie
 
Registered: Oct 2010
Posts: 3

Original Poster
Rep: Reputation: 0
Hi.

Thanks for that. It helped a lot.

Just if I could get a bit more help. I put this together

find / -name "filename" -exec sed -i '' 's/old/new/g' {} \;

Any way for me to make this replace an entire line with new if old is only part of the line?

for example the line may contain old 123 or old 456, but I would just use the search term old to replace the entire line with new.

Thanks
 
Old 10-14-2010, 04:53 PM   #4
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
Probably:
Code:
find / -name "filename" -exec sed -i '' 's/old.*/new/g' '{}' \;
(I haven't tested)

That will replace "old anything here" with "new".

That's using regular expressions.

And you want to quote your {} in case of spaces or funny characters in file names.
 
1 members found this post helpful.
Old 10-15-2010, 06:59 AM   #5
skeates
LQ Newbie
 
Registered: Oct 2010
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks alot.

That did the trick.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Search for ip address in file and remove it coolplanet Programming 9 09-28-2009 03:05 AM
Search tools (Affinity, Tracker Search Tool, etc.) not working - don't find any files Adamantus Linux - Newbie 1 03-29-2009 11:21 PM
Is there a way to search for and remove dead symbolic links? HGeneAnthony Linux - General 5 01-19-2007 02:09 AM
SUSE 9.3 YAST Install Remove search problem riba43 SUSE / openSUSE 2 07-17-2005 12:47 PM
remove color after word search in vi editor dylan912 Linux - Newbie 1 12-10-2004 04:15 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:31 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
Open Source Consulting | Domain Registration