script to search only or search and replace exact pattern

Bellow is a script to search only or  search and replace exact pattern .If you have any sugggestion to this script that would help in ease  system administration task ... be free to suggest.
 
#!/bin/bash
clear

search_pattern ()
{
read -p " Enter the path where your text file is loscated: " p
read -p "Enter the pattern you want to serch: " pat
pattern=`sed -n "\|$pat|p" $p`

if [ -n "$pattern"  ]; then
echo -e "$pattern"
else
echo -e "######### The pattern cannot be found #########"
fi
}

replace_pattern ()
{
read -p " Enter the path where your text file is loscated: " p
read -p "Enter the pattern you want to serch: " pat
read -p "Enter the pattern you want to replace: " reppat
pattern=`sed -n "\|$pat|p" $p`
if [ -f "$p" ]; then

   if [ -n "$pattern"  ]; then

sed -i  "s|$pat|$reppat|" $p
  else
echo -e "######### the pattern is not found ############# "
   fi
else
echo -e "############ the file is not found #############"
fi
}

read -p " What do you ant to do: [ search or replace ]: " t

case $t in
search ) search_pattern
;;
replace ) replace_pattern
;;
* ) echo -e "please enter search or replace"

esac
 

--

No comments:

Other Articles

Enter your email address: