I have a shell script like:
for fl in /home/dr/*.txt; do
mv $fl $fl.old
sed 's#$1#$2#g' $fl.old > $fl
rm -f $fl.old
done
and I run it like ./script.sh find replace, yet nothing happens and there is no output. Why is this?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The problem is that you’re using single quotes instead of double quotes. With single quotes,
sedinterprets the string literally (i.e. it will search for the string$1, not the first argument).Below is a functioning version of what you were trying to do. Note that I’ve replaced temporary file usage with sed’s “in-place” editing.
However, you can one-line everything!