0

I want to write a shell script which will delete all lines containing a specified word in one or more files supplied as arguments to it.How can i do it?

I am a beginner in shell script.So if you give answer with details about how it works, it will be very helpful.

Tanvir Rahman
  • 651
  • 1
  • 11
  • 31
  • This has to be a duplicate of something. You'll want to use `sed`. Something like `sed '/magicword/d' file`. – Dan Dec 04 '15 at 22:26
  • Saying something is a dupe without checking doesn't help. – kayleeFrye_onDeck Dec 04 '15 at 22:27
  • Thanks for tracking that guy down. Added a flag as well. Tanvir Rahman, this site has a lot of answers all over the place, especially for actions that you suspect are not rare. Try using the Search box, or alternately, your question + stackoverflow in a Google search. – kayleeFrye_onDeck Dec 04 '15 at 22:31

1 Answers1

2

The sed delete command will do this easily:

$ cat foo.txt 
apple
pear
peach
banana
orange

$sed -ie '/pea/d' foo.txt

$ cat foo.txt 
apple
banana
orange

This just searches for pea and deletes it with the d (delete) command.

miken32
  • 42,008
  • 16
  • 111
  • 154