LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 05-18-2007, 05:59 AM   #1
jaggy00
LQ Newbie
 
Registered: Jan 2007
Distribution: CentOS & Fedora Core
Posts: 25

Rep: Reputation: 15
howto remove a particular phrase from a text file?


i need to remove a little piece of text from a bigger text file. i can find the phrase with grep, but have no idea howto remove it from the file.

any advice appreciated.
 
Old 05-18-2007, 06:28 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

Sed can do that:

sed -i 's/to be removed//' infile

The above will remove to be removed from the infile and saves it in place (the i- option). Try without the -i option first, output is printed to screen instead.

Hope this helps.
 
Old 05-18-2007, 06:35 AM   #3
Indiestory
Member
 
Registered: Aug 2006
Location: Aberdeen, Scotland
Distribution: OpenBSD
Posts: 164
Blog Entries: 1

Rep: Reputation: 30
could be done with grep,
Code:
cat infile | grep -v [phrase] > outfile
 
Old 05-18-2007, 06:39 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

True, grep can also remove, but it takes out the whole line, not a part of that line. Depending on what you want, both can be used.

BTW: Use: grep -v "[phrase]" infile > outfile if the grep statement is preferable, it's more resource friendly (only one command instead of 2).
 
Old 05-18-2007, 07:27 AM   #5
jaggy00
LQ Newbie
 
Registered: Jan 2007
Distribution: CentOS & Fedora Core
Posts: 25

Original Poster
Rep: Reputation: 15
This solves the issue. Thank you very much
 
  


Reply

Tags
editing, edition, text


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Remove text from a file jviola Programming 23 03-21-2007 01:23 PM
how to remove lf from a text file gfem Linux - Software 3 10-19-2006 07:41 PM
remove \r and \n from a text file powah Programming 9 10-02-2006 07:02 AM
remove some text from a file hfawzy Programming 7 07-10-2006 10:22 PM
Which command to search text/phrase in file? b:z Linux - General 2 03-30-2005 10:11 PM


All times are GMT -5. The time now is 12:03 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