-1

Total newbee on this, so here's what is not working:

find . -type f -exec sed -i 's/\$_SESSION['valOne']"/\$valTwo,"/g' {} +

While it should replace recursively, in all the directories it is going through:

$_SESSION['valOne'] into just: $valTwo

I've done this many times, but it seems the quotes are too confusing for me. Any help here is highly appreciated.

Valdeir Psr
  • 702
  • 1
  • 7
  • 10
KJS
  • 1,176
  • 1
  • 13
  • 29

2 Answers2

1

If I understand what you're trying do do (literal replacements) then you need to single quote the string so the shell doesn't try to interpret its contents, then use '\'' for each singe quote within the string and escape any BRE metacharacters (i.e. [ and ] in your case):

find . -type f -exec sed -i 's/$_SESSION\['\''valOne'\''\]"/$valTwo,"/g' {} +
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
0

Please try as follows. Change the ' -> " and escape with the backslash.

Example:

 find . -type f -exec sed -i "s/\$_SESSION\['valOne'\]/\$valTwo/g" {} +
Valdeir Psr
  • 702
  • 1
  • 7
  • 10
  • Thanks, but sorry. The command is ok, but it doesn't change anything – KJS Dec 26 '17 at 22:30
  • 1
    I made it based on your regex. Make sure that `quotes` after `$_SESSION` exists in the php file. Remove the first `\"` and try again. – Valdeir Psr Dec 26 '17 at 22:33
  • Thank you so much! It is actually this one, but it works perfect now: find . -type f -exec sed -i "s/\$_SESSION\['valOne'\]/\$valTwo/g" {} + – KJS Dec 26 '17 at 22:41
  • please change your answer so I can vote for it and people see the right answer – KJS Dec 26 '17 at 22:42
  • No. Always use single quotes unless you NEED double quotes (e.g. to expand a variable) and then use double quotes unless you NEED no quotes (e.g. for globbing). In this case using double quotes around the string is exposing it to the shell for interpretation so you're then having to escape the `$`s so the shell doesn't try to expand the subsequent text as if it was a variable. Just don't do that. – Ed Morton Dec 26 '17 at 23:27