-1

I am trying to pass in assets/css/images to replace images in multiple files.

So I wrote this script

$list
$word
$neword
$i

echo "select files: "
read list
echo "input word to search for: "
read word
echo "input word to replace with: "
read neword

for i in $list
do
sed -i "s/${word}/${neword}/g" $i

done

It works for words without a /, and I can see why, but I don't know how to get around it. Anyone?

  • i mean to replace the string "images" with the string "assets/css/images" – Jenna Murphy May 17 '17 at 04:24
  • the error I am getting is: sed -e expression #1, char 17: unknown option to 's' – Jenna Murphy May 17 '17 at 04:25
  • Would this help: http://stackoverflow.com/questions/27787536/how-to-pass-a-variable-containing-slashes-to-sed? – grundic May 17 '17 at 04:26
  • Is the list of variable interpolations at the beginning of the script really part of your code? It would do nothing useful if the variables are unset, and potentially something bad if not. – tripleee May 17 '17 at 04:42
  • grundic, thank you for pointing that out. I couldn't find that for the life of me. Thank you! – Jenna Murphy May 17 '17 at 16:55
  • triplee, Sorry as I am new to bash scripting. So the list at the top was not neccessary. I deleted it and it worked the same. Good to know! thank you! Out of curiosity, what do you mean b something bad could happen? – Jenna Murphy May 17 '17 at 16:58

1 Answers1

0

You should do like this to escape / in sed

Try Following,

sed -i "s#${word}#${neword}#g" "$i"

or

sed -i "s~${word}~${neword}~g" "$i"

Edit: Added Quotes to Filename as Suggested by @tripleee for avoiding condition like filename contains in $i have spaces.

toxic_boi_8041
  • 1,424
  • 16
  • 26