Gulshan
Gulshan

Reputation: 131

Removing a word from a previously created file using shell script

Suppose we do have a previously created file ABC

cat ABC      //Checking contents of **ABC**

hello
hi 
how
what
where

Now with the help of shell script i do want to remove the word 'how' from ABC

I am trying this

echo Enter file name 
read a
if[ $a -f ]
then
_____ |grep how ABC
fi

Is there any command which can be used at _____?

All other solution are also welcome.

Upvotes: 2

Views: 55

Answers (1)

anubhava
anubhava

Reputation: 785136

You can use sed:

read -p "Enter file name: " file
[[ -f "$file" ]] && sed -i.bak '/how/d' ABC

/how/d will delete line with the pattern how in file ABC if found and save the changes back into ABC. It also creates a backup of original file with the name ABC.bak in case something goes wrong.

In case you want to replace only a single word (without deleting whole line containing that word) then use:

sed -i.bak 's/how//' ABC

Upvotes: 3

Related Questions