1

My KSH-Script should replace a String in a txt file from the same directory.

sed -i 's/"$original"/"$reversed"/' inputtext.txt

is what I'm using currently, but it doesn't work. There is no error in the code or things like that. It just doesn't work.

Here is my whole code:

#!/bin/ksh
original=$1
reversed=""
counter=0
echo $original | awk -v ORS="" '{ gsub(/./,"&\n") ; print  }' | \
while read char
do
   letters[$counter]+="$char"
   ((counter=counter+1))
done
length=${#original}
((length=length-1))
echo $original | awk -v ORS="" '{ gsub(/./,"&\n") ; print  }' | \
while read char
do
   reversed+=${letters[$length]}
   ((length=length-1))
done
echo $reversed
sed -i 's/"$original"/"$reversed"/' inputtext.txt
exit 0

I want, that in the file "inputtext.txt" (same dir as the .sh file) every word that equals "$original" gets changed to "$reversed".

What am I doing wrong?

  • Possible duplicate of [Sed replacement not working when using variables](http://stackoverflow.com/questions/5156293/sed-replacement-not-working-when-using-variables) – tripleee Feb 05 '16 at 12:42

1 Answers1

1

I think single quotes prevent variable expansion. You can try this:

sed -i "s/$original/$reversed/" inputtext.txt
mauro
  • 5,730
  • 2
  • 26
  • 25