Register
It is currently Sat Dec 20, 2014 8:50 pm

remove string in the file


All times are UTC - 6 hours


Post new topic Reply to topic  [ 5 posts ] 
Author Message
 PostPosted: Sun Sep 21, 2008 11:43 pm   

Joined: Wed Sep 17, 2008 1:02 am
Posts: 1
I want to write a script to remove some string in the line such as below

fileA: fileB:
bat rat
rat dog
cat car

I want to remove "rat" in file A and B how to write the script and delete the "rat" line


Top
 Profile  
 PostPosted: Mon Sep 22, 2008 8:02 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
well it seems that "rat" appears on it's own in a line, therefore the following should work.

Code:
grep -v rat fileA > newfileA


Top
 Profile  
 PostPosted: Mon Sep 22, 2008 9:07 am   
User avatar

Joined: Sun Apr 13, 2008 4:05 am
Posts: 37
Location: /dev/random
if you mean just the rat, not the line with rat

Code:
sed ’s/rat//g’ file > newfile


Top
 Profile  
 PostPosted: Mon Sep 22, 2008 12:43 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Haha, duplicate thread :) I posted this in the other one, but you can also use sed to remove the whole line:

Code:
sed /rat/d test.txt


Edit: Oh! and I mentioned this in the other thread as well, but if you use the "-i" switch with sed, it will edit the file "in place" so you don't have to create a new one:

Code:
sed -i /rat/d test.txt


Edit: I keep forgetting details... Anyway, this will find anything that matches "rat" and delete the whole line. This might not be desirable if you have words in there like "scrat" or "ratatouille". You'll want to play with regular expressions if you need to get that level of detail.

Have fun!
-Jeo


Top
 Profile YIM  
 PostPosted: Sun Sep 28, 2008 9:38 am   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
i removed the other post :)


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 1 guest


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