如何通过 SED 在行尾添加引号 [英] How to add quote at the end of line by SED

查看:151
本文介绍了如何通过 SED 在行尾添加引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

如何通过 1 个命令同时转义 $'?

How to escape both $ and ' by 1 command?

推荐答案

要在行尾添加引号,请使用

To add a quote at the end of a line use

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

请参阅证明.

如果已经有单引号,并且你想确保在字符串末尾有单引号使用

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

参见证明

  • [\$'] - 匹配 $(转义为双引号,它可以被视为变量插值字符)或 '
  • \\\\& - 一个反斜杠(需要 4 个,也就是字面意义上的 2 个反斜杠,在替换中比较特殊),而 & 是整个匹配.
  • [\$'] - 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.

这篇关于如何通过 SED 在行尾添加引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆