=====================================
Printing lines that contain a particular word
=====================================
[prabhucloudxlab@cxln4 prabhu]$ cat > filegrep.txt
unix is great os. unix is opensource. unix is free os.
learn operating system.
unix linux which one you choose.
uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
ThIs Is LInux 2000
ThIs Is WIndows 3000
ThIs Is MAC 4000
ThIs Is LInux 2000
ThIs Is WIndows 3000
Printing lines that contain a particular word
=====================================
[prabhucloudxlab@cxln4 prabhu]$ cat > filegrep.txt
unix is great os. unix is opensource. unix is free os.
learn operating system.
unix linux which one you choose.
uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
ThIs Is LInux 2000
ThIs Is WIndows 3000
ThIs Is MAC 4000
ThIs Is LInux 2000
ThIs Is WIndows 3000
SCRIPT
[prabhucloudxlab@cxln4 ~]$ cat>hello.sh
#! /bin/bash
#Printing lines that contain a particular word
echo "enter filename to substitute using sed"
read fileName
echo " "
if [[ -f $fileName ]]
then
echo "Printing lines with numbers"
echo " "
sed '=' $fileName
echo "====================================="
echo "Count the lines word contains WIndows"
echo " "
sed -n '/WIndows/=' $fileName
echo "====================================="
echo "Printing the lines word contains WIndows"
echo " "
sed -n '/WIndows/p' $fileName
echo "====================================="
echo "#Replacing Is with hello "
echo " "
sed 's/Is/hello/pg' $fileName
echo "====================================="
echo "#1st occurrence of all lines"
echo " "
sed -ne 's/Is/hello/pg' $fileName
echo "====================================="
echo "#Multiple words replacing and printing"
echo " "
sed -ne 's/Is/HELLO/pg' -e 's/2000/77777777/pg' $fileName
echo "====================================="
else
echo "$fileName doesn't exist"
fi
OUTPUT
[prabhucloudxlab@cxln4 ~]$ sh hello.sh
enter filename to substitute using sed
filegrep.txt
Printing lines with numbers
1
unix is great os. unix is opensource. unix is free os.
2
learn operating system.
3
unix linux which one you choose.
4
uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
5
ThIs Is LInux 2000
6
ThIs Is WIndows 3000
7
ThIs Is MAC 4000
8
ThIs Is LInux 2000
9
ThIs Is WIndows 3000
=====================================
Count the lines word contains WIndows
6
9
=====================================
Printing the lines word contains WIndows
ThIs Is WIndows 3000
ThIs Is WIndows 3000
=====================================
#Replacing Is with hello
unix is great os. unix is opensource. unix is free os.
learn operating system.
unix linux which one you choose.
uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
Thhello hello LInux 2000
Thhello hello LInux 2000
Thhello hello WIndows 3000
Thhello hello WIndows 3000
Thhello hello MAC 4000
Thhello hello MAC 4000
Thhello hello LInux 2000
Thhello hello LInux 2000
Thhello hello WIndows 3000
Thhello hello WIndows 3000
=====================================
#1st occurrence of all lines
Thhello hello LInux 2000
Thhello hello WIndows 3000
Thhello hello MAC 4000
Thhello hello LInux 2000
Thhello hello WIndows 3000
=====================================
#Multiple words replacing and printing
ThHELLO HELLO LInux 2000
ThHELLO HELLO LInux 77777777
ThHELLO HELLO WIndows 3000
ThHELLO HELLO MAC 4000
ThHELLO HELLO LInux 2000
ThHELLO HELLO LInux 77777777
ThHELLO HELLO WIndows 3000
=====================================