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

How to escape both $ and ' by 1 command?

Cyrus
  • 84,225
  • 14
  • 89
  • 153
snakehand
  • 39
  • 6

4 Answers4

1

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
  • 18,032
  • 4
  • 24
  • 37
1

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.

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
  • 55,640
  • 6
  • 51
  • 83
0

Use octal values

sed 's/$/\o47/'

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

F. Hauri - Give Up GitHub
  • 64,122
  • 17
  • 116
  • 137