Register
It is currently Fri Nov 28, 2014 4:33 pm

Delete a grepped line


All times are UTC - 6 hours


Post new topic Reply to topic  [ 6 posts ] 
Author Message
 PostPosted: Fri Jan 27, 2012 8:12 pm   

Joined: Fri Jan 27, 2012 8:08 pm
Posts: 8
Hi does anyone know how to grep into a .txt file and delete the line you grepped. Let's say that i had a txt file containing this
Quote:
hi whats your name
i am lagoon
ok cool

how would i edit the file so it deletes the 2nd line?

Thanks in advance!


Top
 Profile  
 PostPosted: Sat Jan 28, 2012 1:59 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 574
hi,

you don't use `grep' to delete, but only to match; to delete you'd use `sed'


Top
 Profile  
 PostPosted: Sat Jan 28, 2012 10:53 am   

Joined: Fri Jan 27, 2012 8:08 pm
Posts: 8
Can you show me how? Thanks


Top
 Profile  
 PostPosted: Sat Jan 28, 2012 11:31 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 574
Code:
man sed
lol

Code:
sed 2d yourFile
if your happy with the result, add an -i option.


Top
 Profile  
 PostPosted: Sat Jan 28, 2012 12:06 pm   

Joined: Fri Jan 27, 2012 8:08 pm
Posts: 8
Nevermind i figured it out


Top
 Profile  
 PostPosted: Mon Jan 30, 2012 2:38 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
If you want to modify a stream:
Code:
sed '/i am lagoon/d' file.txt


If you want to modify the file itself:
Code:
ed -s file.txt <<< $'/i am lagoon/d\nw'


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP