3

I have a command similiar this this one:

find ./ -type f | xargs gsed -i -r 's/[$][A-Za-z_\'"]/testing

I would like put this command to work. Unfortunally the single and double quotes breaks the command. How can I escape this strings and put this command to work?

This question isnt the same as this one because I have a single quote inside the [. But if a littler modification solves the problem works I would like to know.

I had tried several different ways using @, #, \, but no success yet.

Community
  • 1
  • 1
GarouDan
  • 3,743
  • 9
  • 49
  • 75

3 Answers3

6

take a look this:

kent$  cat file
''''''
""""""

kent$  sed 's/\x27/single /g;s/\x22/double /g' file
single single single single single single 
double double double double double double 

so , change your cmd into:

find ./ -type f | xargs gsed -i -r 's/[$][A-Za-z_\x22\x27]/testing'

note I didn't check if your find/xargs part ok, just for the single/double quote part.

Kent
  • 189,393
  • 32
  • 233
  • 301
  • This did not work for me (no error but nothing substituted). I am on a mac. – rrr May 01 '18 at 19:49
  • 1
    @rrr yours is bsd sed. you don't have the `-i` option. read man page for the in-place sub. – Kent May 02 '18 at 13:41
1

You cannot use a single quote in single quotes. You can:

  1. End the single quotes and then use a backslashed quote: '...'\'

  2. End the single quotes and then use a single quote in double quotes: '...'"'"

choroba
  • 231,213
  • 25
  • 204
  • 289
  • 3. Use different quote types for wrapping the expression than the one you are substituting. (i.e. use single quotes in double quotes). If you wrap with double quotes: `sed "s/'/bla/"` the single quote `'` will be replaced with `bla` with no need for escaping. (I'm on a mac) – rrr May 01 '18 at 19:53
  • @rrr: Double quotes might require adding more backslashes as they might start expanding some substrings. – choroba May 01 '18 at 20:03
0

Or you could try putting the sed script in double quotes and escape what needs to be escaped

gsed -i -r "s/[$][A-Za-z_'\"]/testing/" 
Scrutinizer
  • 9,608
  • 1
  • 21
  • 22