Government Security
Network Security Resources

Jump to content


Simple Shell Program

- - - - - shell
  • Please log in to reply
1 reply to this topic

#1 Little_Dice


    Staff Sergeant

  • Members
  • 284 posts

Posted 20 February 2008 - 07:06 PM

I am trying to write a program in shell to replace strings in a text file. I'm pretty sure that my method is right, I just think I am not declaring , calling my variables correctly.


#calling variables
@ replacements=0
@ i=0
set file
set string[index]
set replacement[index]

echo "This script will substitute a pattern with another one."
echo "How many strings will you be replacing?"
echo "What is the path to the file you want to modify?"
cp $file $file.saved

#The Meat
while ( $i<= $replacements)
echo "Enter $i pattern."
echo "Enter what you want pattern $i replaced with"
sed 's/$string[$i]/$replacement[$x]/g' $file

#2 Kenny


    Former Commander In Chief

  • Retired Admin
  • 6,747 posts

Posted 21 February 2008 - 07:16 AM

not tried your code...but noticed syntax... re this line:

sed 's/$string[$i]/$replacement[$x]/g' $file

try it this way

sed -i "s/oldword/new-word/g" $file

note the double quotes
Kenny aka ComSec

Please read the Forum Rules !!!


Also tagged with one or more of these keywords: shell