-1
sed -i 's/$/\'/g' 
sed -i "s/$/\'/g"

How to escape both $ and ' by 1 command?

Cyrus
  • 77,979
  • 13
  • 71
  • 125
snakehand
  • 31
  • 4

4 Answers4

0

This might work for you (GNU sed):

sed 's/$/'\''/' file

Adds a single quote to the end of a line.

sed 's/\$/'\''/' file

Replaces a $ by a single quote.

sed 's/\$$/'\''/' file

Replaces a $ at the end of line by a single quote.

N.B. Surrounding sed commands by double quotes is fine for some interpolation but may return unexpected results.

potong
  • 51,370
  • 6
  • 49
  • 80
0

To add a quote at the end of a line use

sed -i "s/$/'/g" file
sed -i 's/$/'"'"'/g' file

See proof.

If there are already single quotes, and you want to make sure there is single occurrence at the end of string use

sed -i "s/'*$/'/g" file
sed -i 's/'"'*"'$/'"'"'/g' file

See this proof.

To escape $ and ' chars use

sed -i "s/[\$']/\\\\&/g" file

See proof

  • [\$'] - matches $ (escaped as in double quotes it can be treated as a variable interpolation char) or '
  • \\\\& - a backslash (need 4, that is literal 2 backslashes, it is special in the replacement), and & is the whole match.
Ryszard Czech
  • 16,363
  • 2
  • 17
  • 35
0

Use octal values

sed 's/$/\o47/'

Care to use backslash + letter o minus + octal number 1 to 3 digit

F. Hauri
  • 58,205
  • 15
  • 105
  • 122
0

Just don't use single quotes to start the sed script?

sed "s/$/'/"

The /g at the end means to apply everywhere it's found on each stream (line) - you don't need this since $ is a special character indicating end of stream.