Fedora Linux Support Community & Resources Center
  #1  
Old 9th April 2012, 11:49 AM
glennzo Online
Un-Retired Administrator
 
Join Date: Mar 2004
Location: Salem, Mass USA
Age: 57
Posts: 14,869
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: Just a couple of old single core units
Desktop: BioStar MCP6PB M2+ / AMD Phenom 9750 Quad Core / 4GB / Kingston HyperX 3K SSD 240GB SATA 3.0 / 1TB 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,696
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/
__________________
Laptop: Toshiba satellite p50-a-11 CPU: Intel i7 8*2400 MHz GPU: GeForce GT 745M RAM: 8192 MB Distro: Fedora (Rawhide) DE: Awesome
Guide: Windows 8+ & Fedora 20+ Dualboot
Repo (sea-devel) :: Text User Interface (TUI), Video Handler Script (VHS), efi-helper, grub2-circled-nasa-themes, Essentials (blame, p, rmtf, rnd, siggen, screenshot...), (BETA) Dev-Scripts, (BETA) Fedora Account Scripts (FAS)

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,334
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,898
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: 9,041
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,769
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 21 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,898
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 Online
Un-Retired Administrator
 
Join Date: Mar 2004
Location: Salem, Mass USA
Age: 57
Posts: 14,869
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: Just a couple of old single core units
Desktop: BioStar MCP6PB M2+ / AMD Phenom 9750 Quad Core / 4GB / Kingston HyperX 3K SSD 240GB SATA 3.0 / 1TB 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 10:46 PM
Awk for specific lines in text keysorsoze Using Fedora 1 17th January 2007 09:14 PM


Current GMT-time: 11:22 (Monday, 22-12-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
...Baleal - Jatisura Village Travel Photos on Instagram - Reedy River Falls Historic Park and Greenway Photos - Aqualuz Troia Resort Travel Photos - Turkey - Antalya - Grand Haber Hotel