Shell Script to Delete All the Line Containing the Given Word from a File

shell script is a computer program designed to be run by the Unix shell, a command-line interpreter. The various dialects of shell scripts are considered to be scripting languages. Typical operations performed by shell scripts include file manipulation, program execution, and printing text. A script that sets up the environment, runs the program, and does any necessary cleanup, logging, etc. is called a wrapper.

In this post, we will write a shell script to Delete All the Line Containing the Given Word from a File.

INPUT:
line 1: filename
line2: word

OUTPUT:
Print the output accordingly

The following is the shell script to delete All the Line Containing the Given Word from a File:

echo enter file name
read file
echo enter word
read word
echo file before removing $word:
cat $file
grep -v -i $word $file > test
mv test $file
echo file after removing $word:
cat $file

OUTPUT:

$ enter file name
$ a
$ enter word
$ linux
$ file before removing linux:
$ linux
$ Hello World 
$ Hello
$ linux
$ Hello
$ file after removing linux:
$ Hello World 
$ Hello
$ Hello

Let us know in the comments if you are having any questions regarding this shell script.

And if you found this post helpful, then please help us by sharing this post with your friends. Thank You

Leave a Reply