Hello, I am creating a shell script and I am almost done. I have certain things I want done in the shell but don't know how to finish it properly.
Here is what I am aiming to do. I am aiming to have a shell that takes at least 3 parameters. The first two are the two words to replace (the first being the word replaced and the second being the word to replace it with) and then the third (and beyond) parameter, are all the files to make the changes to.
Here is what I have so far but its not working correctly and giving me incorrect output when using special characters such as changing 'Unix' to 'U N I X'
#!/bin/sh
replace=$1
shift
with=$1
shift
for file in $*
do
echo $file
newreplace=`echo "$replace" | sed -e "s/$replace/&/g"`
echo $newreplace
sed "s/$newreplace/$with/g" "$file" > "$file.updated"
mv "$file.updated" "$file"
done
Given your 'Unix' to 'U N I X' example, it works for me. I'm a bit unsure what you are trying to accomplish with this statement:
newreplace=`echo "$replace" | sed -e "s/$replace/&/g"`
Maybe you're just debugging with it, but you're replacing the string with itself which doesn't (to me) seem useful.
Please post a sample of your input, what you expect the result to be, and what you are actually getting. That might help someone here figure out what is going on. Also, the version of your O/S, sed, and shell might be useful to know too.