Thursday, December 19, 2013

Unix/Linux - Shell script to find and replace a text from the list of files

Below is the automated shell script which will find a text and replace it with the given string. It will also keep a backup of existing file. It can also be used for global search and replace string from a text file. Please see the self-explanatory shell script below -


#find string

#replace with

echo "searching "$searchString" ..."
grep -l $searchString * >foundIn.txt
chmod 777 foundIn.txt

echo "replacing "$replaceWith" ..."
while read x
  if [ $x != ]
          (cat  $x) >$x"_bkp"
          sed "s/${searchString}/${replaceWith}/g" $x"_bkp" >  $x
          echo "Found in... "$x
done <foundIn.txt

echo $count "occurences have been replaced."

Linux/Unix - Shell script to read a text file

Method 1 - By iterating each line using while loop


#Name of the text file

echo "Reading text file "$txtFile" ..."

while read x
echo $x
done <$txtFile

Method 2 - By using cat (concatenate and display files) command


#Name of the text file

echo "Reading text file "$txtFile" ..."

cat $txtFile

Wednesday, December 18, 2013

keytool error: cacerts (Permission denied)

I got the error when trying to install a certificate into my keystore

Command -
keytool -import -alias aliasName -file fileName.cer -keystore cacerts
keytool -list -keystore cacerts


Windows -
This could happen if you are not running the command prompt in administrator mode. Login with the user who has administrator privilege.

For Windows 7 and above - Go to run, type cmd and hit Ctrl+Shift+enter. This will open the command prompt in administrator mode.

For others windows -  Go to start -> all programs -> accessories -> right click command prompt and say run as administrator.

Linux -
First check the write permissions on the keystore. Also if needed login with root user and try the command.