How to delete a word from a file using shell scripting???

Questions by vinay.prasad

Showing Answers 1 - 11 of 11 Answers

sum

  • Jul 5th, 2007
 

sed -e 's/word//g' filename

UncaAlby

  • Nov 7th, 2008
 

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.

  Was this answer useful?  Yes

UncaAlby

  • Nov 7th, 2008
 

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 (>)

  Was this answer useful?  Yes

ajay goel

  • Jul 7th, 2014
 

Code
  1. vi file

  2. escape

  3.  1,$ s/word//g

  4. :wq

  Was this answer useful?  Yes

abdul

  • Nov 7th, 2014
 

#!/bin/bash/
set +x

echo -e "enter the file name:c"
read fname

echo -e "enter the word to be deleted:c"
read pattern
echo $pattern
sed s/$pattern//g $fname > temp.txt

  Was this answer useful?  Yes

Sanjeev

  • Apr 3rd, 2015
 

sed s/pattern//g filename

  Was this answer useful?  Yes

swapna

  • May 14th, 2015
 

Here in the script instead of redirecting the o/p into temp file we can just do as follows

Code
  1. sed -I s/$pattern//g <<filename>>

  Was this answer useful?  Yes

venkat

  • Jan 10th, 2017
 

tr -d pattern < filename

  Was this answer useful?  Yes

sambit

  • Mar 28th, 2017
 

sed command with -i option will make the changes persistent in the file. You dont need the redirection operator to submit the changes to a new file. The answer is:
sed -i s/word_to_delete/word_to_replace_with/g file_name

  Was this answer useful?  Yes

Manisha Kumari

  • Jun 15th, 2017
 

In Esc mode, dw

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions