Government Security
Network Security Resources

Jump to content

Photo

Simple Shell Program

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

#1 Little_Dice

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.

#!/bin/tcsh


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


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

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

#2 Kenny

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