Fedora Linux Support Community & Resources Center
  #1  
Old 9th April 2012, 11:49 AM
glennzo Offline
Un-Retired Administrator
 
Join Date: Mar 2004
Location: Salem, Mass USA
Posts: 14,538
linuxfirefox
Remove lines of text using a script

I wonder if anyone can help me and at the same time teach me ... how to remove lines containing selected text from a text file using bash scripting. For example, if a text file contains the following lines, at random locations within the file and not necessarily one following the other:

PHP Code:
glenn the_linuxfan
mike the_linuxfan
john the_linuxfan 
How the h-e double hockey sticks is this accomplished? I've examined sed and awk, loops, arrays, etc. but I just-can't-get-it-right !!!!

Your kind suggestions will be immensely appreciated.
__________________
Glenn
The Bassinator © ®

[SIGPIC][/SIGPIC]
Laptop: Toshiba Satellite / Intel Core 2 Duo 1.73 GHz / 2GB / 160GB / Intel Mobile 945GM/GMS/GME/943/940GML Integrated Graphics
Desktop: BioStar MCP6PB M2+ / AMD Phenom 9750 Quad Core / 4GB / 1TB SATA / 500GB SATA / EVGA GeForce 8400 GS 1GB
Reply With Quote
  #2  
Old 9th April 2012, 12:25 PM
sea Offline
"Shells" (of a sub world)
 
Join Date: May 2011
Location: Confoederatio Helvetica (Swissh)
Age: 34
Posts: 3,070
linuxfedorafirefox
Re: Remove lines of text using a script

Found a link to explain this task:
http://en.kioskea.net/faq/1451-sed-d...es-from-a-file
http://www.cyberciti.biz/faq/howto-l...l-blank-lines/
__________________
Notebook: Samsung NC210/NC110 CPU: Intel 2*1600 Mhz RAM: 1024 MB DDR2 OS: Fedora 19 DE: Awesome
Laptop: Toshiba Satellite L670 CPU: Intel(r) 2*1872 Mhz RAM: 3072 MB DDR3 OS: Fedora 17 DE: Awesome
Fedora Manual (RTFM) | The Linux Documentation Project | Script-Tools

Last edited by sea; 9th April 2012 at 01:00 PM.
Reply With Quote
  #3  
Old 9th April 2012, 01:00 PM
DBelton Offline
Administrator
 
Join Date: Aug 2009
Posts: 7,318
linuxfirefox
Re: Remove lines of text using a script

you can delete the lines using grep.

Using your above examples:

Code:
cat oldfilename |  grep -Ev 'glenn the_linuxfan|mike the_linuxfan|john the_linuxfan' > newfilename
You can also have grep read a file with the "criteria" in it as well. Check grep out some more and see if it's what you need.
Reply With Quote
  #4  
Old 9th April 2012, 01:59 PM
jpollard Online
Registered User
 
Join Date: Aug 2009
Location: Waldorf, Maryland
Posts: 6,662
linuxfirefox
Re: Remove lines of text using a script

Note, that delete will also delete any lines that CONTAIN the pattern matched.

This applies to lines that have more than the text, which you might want to keep. Judicious use of matching beginning of lines and end of lines would be needed then.

Oh, and you can shorten the command to "grep -Ev '....'<oldfilename >newfilename"
or even drop the input redirection.
Reply With Quote
  #5  
Old 9th April 2012, 02:07 PM
stevea Offline
Registered User
 
Join Date: Apr 2006
Location: Ohio, USA
Posts: 8,553
linuxfirefox
Re: Remove lines of text using a script

Quote:
Originally Posted by jpollard View Post
Note, that delete will also delete any lines that CONTAIN the pattern matched.
Re-read the original problem statement in post #1 please. That IS what was requested.
You are confabulating exceptions unnecessarily.

Quote:
Originally Posted by glennzo View Post
... how to remove lines containing selected text [...]
Glenn wants to remove lines matching the pattern, not just exact whole line matches.
__________________
None are more hopelessly enslaved than those who falsely believe they are free.
Johann Wolfgang von Goethe

Last edited by stevea; 9th April 2012 at 02:11 PM.
Reply With Quote
  #6  
Old 9th April 2012, 02:16 PM
RupertPupkin Offline
Registered User
 
Join Date: Nov 2006
Location: Detroit
Posts: 5,457
linuxfedorafirefox
Re: Remove lines of text using a script

If you want to edit the file in place (and not save to a different file), you could use sed:
Code:
sed -i -e '/\(glenn the_linuxfan\|mike the_linuxfan\|john the_linuxfan\)/d' myfile.txt
__________________
OS: Fedora 20 x86_64 | Machine: HP Pavilion a6130n | CPU: AMD 64 X2 Dual-Core 5000+ 2.6GHz | RAM: 5GB PC5300 DDR2 | Disk: 400GB SATA | Video: ATI Radeon HD 4350 512MB | Sound: Realtek ALC888S | Ethernet: Realtek RTL8201N

Last edited by RupertPupkin; 9th April 2012 at 04:33 PM.
Reply With Quote
  #7  
Old 9th April 2012, 03:13 PM
jpollard Online
Registered User
 
Join Date: Aug 2009
Location: Waldorf, Maryland
Posts: 6,662
linuxfirefox
Re: Remove lines of text using a script

Quote:
Originally Posted by stevea View Post
Re-read the original problem statement in post #1 please. That IS what was requested.
You are confabulating exceptions unnecessarily.
Yes it was. All I was doing was noting what COULD be desired to ONLY remove those lines.

Not exceptions at all.
Reply With Quote
  #8  
Old 9th April 2012, 05:23 PM
glennzo Offline
Un-Retired Administrator
 
Join Date: Mar 2004
Location: Salem, Mass USA
Posts: 14,538
windows_xp_2003firefox
Re: Remove lines of text using a script

Great solutions! I thank you all for your comments. I'll use Rupert's solution if the rest of you don't mind.

Step up to the virtual bar. I'm buying.
__________________
Glenn
The Bassinator © ®

[SIGPIC][/SIGPIC]
Laptop: Toshiba Satellite / Intel Core 2 Duo 1.73 GHz / 2GB / 160GB / Intel Mobile 945GM/GMS/GME/943/940GML Integrated Graphics
Desktop: BioStar MCP6PB M2+ / AMD Phenom 9750 Quad Core / 4GB / 1TB SATA / 500GB SATA / EVGA GeForce 8400 GS 1GB
Reply With Quote
Reply

Tags
lines, remove, script, text

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Question] How to remove all lines that contain certain text in VI beyonddc Using Fedora 2 22nd August 2007 04:58 PM
Need a script to remove lines beginning with a certain word Jongi Programming & Packaging 2 8th July 2007 05:07 PM
Script to Remove few lines from a file fed.linuxgossip Using Fedora 3 5th March 2007 09:46 PM
Awk for specific lines in text keysorsoze Using Fedora 1 17th January 2007 08:14 PM


Current GMT-time: 03:13 (Sunday, 20-04-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat