GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Operating System  >  Shell Scripting
Go To First  |  Previous Question  |  Next Question 
 Shell Scripting  |  Question 34 of 47    Print  
how to delete a word from a file using shell scripting???

  
Total Answers and Comments: 4 Last Update: November 07, 2008     Asked by: vinay.prasad 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: sum
 
sed -e 's/word//g' filename

Above answer was rated as good by the following members:
jrgalaura
July 05, 2007 07:35:32   #1  
sum        

RE: how to delete a word from a file using shell scrip...
sed -e 's/word//g' filename
 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
May 04, 2008 10:48:09   #2  
nishu_naga Member Since: May 2008   Contribution: 2    

RE: how to delete a word from a file using shell scripting???

if we want to delete any particular word from File

for example

to delete apple in {apple is a fruit}

sed -e 's/apple//' sometext

output
is a fruit


 
Is this answer useful? Yes | No
November 07, 2008 04:24:40   #3  
UncaAlby Member Since: October 2008   Contribution: 13    

RE: how to delete a word from a file using shell scripting???
Be careful what you mean by the word "word"

A couple of people have answered with the "sed" command which is good but if you're looking for actually removing a "word" and not a part of a word the instruction needs some modification.

For example to remove the word "the"

echo "Other than the aesthetics there's the synthetics" | sed 's/the//g'

Produces:

Or than aestics re's syntics

To remove the word "the" and not pieces of other words add triangle brackets around the text. These must be quote from the shell e.g.:

sed 's/<the>//g'

So the new result with the brackets will look like this:

Other than aesthetics there's synthetics

The text embedded within other text is not affected.

Of course the next thing to do is pipe your file into this "sed" command and pipe the output into a different file. Inspect the result and if you're satisfied replace the original file with the new one.

 
Is this answer useful? Yes | No
November 07, 2008 23:58:35   #4  
UncaAlby Member Since: October 2008   Contribution: 13    

RE: how to delete a word from a file using shell scripting???
please note that my original answer ate the quote signs which I'm going to try to make sure get through this time:

sed 's/\<the\>//g'

And in case that gets eaten also it's a reverse slash then the less than (<) the text to be replaced ("the") the the reverse slash then the greater than (>)

 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape