this question has answer here:
- sed substitution bash variables 4 answers
i have bash shell script written below:
#!/bin/bash #arg1: pattern found #arg2: pattern replaced #arg3: filename pattern echo replace $1 $2 $3 files in `find . -type f -name "$3"` sed 's/$1/$2/g' $i done
i use script (say, called subs) as:
./subs bbb ddd aa
where file aa has:
aaaa bbbb cccc
but, while expect see bbbb replaced dddb, shows same content i.e. bbbb. clue wrong here? also, make same file getting modified after replacement. how can that? qs little similar sed substitution bash variables, in qs, have used pattern find , replace argument shell variable.
variables won't expand when put inside single quotes; need use double quotes:
sed "s/$1/$2/g" "$i"
to modify file in place:
sed -i.bak "s/$1/$2/g" "$i"
after operation original file backed .bak
extension; if don't want keep backup:
sed -i "s/$1/$2/g" "$i"
Comments
Post a Comment