0

I want to replace all occurrences of {{password}} or {{ password }} or {{password }} in files with some string using sed i am able to remove with know number of spaces , but could not remove all combinations.

i could replace using , but not all combinations

sed -i -e "s/{{ password }}/$password/g" test.sql
ravthiru
  • 7,648
  • 2
  • 36
  • 48
  • note that you'll have to make sure `password` variable doesn't contain ``\`` or `&` or the delimiter character which `/` in the example used above.. see also: https://stackoverflow.com/questions/29613304/is-it-possible-to-escape-regex-metacharacters-reliably-with-sed – Sundeep May 26 '20 at 08:26
  • 1
    also, as a good practice, I'd use `'s/{{ password }}/'"$password"'/g'` to avoid double quotes conflicting with sed command – Sundeep May 26 '20 at 08:27

1 Answers1

2

If the number of spaces is arbitrary, then you could search for zero or more spaces (*) instead of just one space () with something like {{ *password *}}.

rid
  • 57,636
  • 30
  • 143
  • 185
  • if you are using regex, I often use "\s" instead of " " just to be sure there are not tabs or other whitespace characters "posing" as a regular space. – tst May 26 '20 at 08:11