我正在寻找一种可以快速转义字符串的工具。这项任务非常有用,但我找不到它。bash - 快速转义字符串
比方说一个例子:
hisrmline 'h | g -E "^ [0-9]* exit$"'
如果我想手动逃避它,这是可以做到:
'hisrmline '\''h | g -E "^ [0-9]* exit$"'\'''
但它的耗时和效率不高。所以我发现的printf%Q:
[[email protected] note]$ printf "%q" hisrmline 'h | g -E "^ [0-9]* exit$"'
hisrmlineh\ \|\ g\ -E\ \"\^\ \[0-9\]\*\ \ exit\$\"[[email protected] note]$
[[email protected] note]$
输出是错误的,因为hisrmlineh是concate一起,所以我缩小字符串:
[[email protected] note]$ printf "%q" hisrmline 'h'
hisrmlineh[[email protected] note]$
[[email protected] note]$
我想要的是什么hisrmline \'h \'
这对grep特别有用:
[[email protected] note]$ HISTTIMEFORMAT=""; history|grep -a --color=auto hisrmline\ \'h
7856 hisrmline 'hisrmline'
7857 hisrmline 'hisrmline'
7882 hisrmline 'h | g -E "^ [0-9]* exit[ ]*$"'
7883 hisrmline 'h | g -E "^ [0-9]* exit[ ]*$"'
7884 hisrmline 'h | g -E "'
7885 hisrmline 'h | g '
7886 hisrmline 'h | g'
7887 hisrmline 'h |'
7890 hisrmline 'h | g -E "^ [0-9]* exit$"'
7891 hisrmline 'h | g -E "^ [0-9]* exit$"'
7905 h|g 'hisrmline 'h | g -E "^ [0-9]* exit$"''
和grep -F不会让我的生活更轻松,当处理嵌套的单引号,我仍然需要手工逃脱单引号“\”':
[[email protected] note]$ HISTTIMEFORMAT=""; history|grep -a --color=auto -F '[0-9]* exit$"'\'''
7889 h|g -aF 'h | g -E "^ [0-9]* exit$"'
7890 hisrmline 'h | g -E "^ [0-9]* exit$"'
7891 hisrmline 'h | g -E "^ [0-9]* exit$"'
7905 h|g 'hisrmline 'h | g -E "^ [0-9]* exit$"''
7911 h|g 'hisrmline 'h | g -E "^ [0-9]* exit$"''
7912 h|g 'hisrmline '"'"'h | g -E "^ [0-9]* exit$"'"'"'
有没有更简单的方法或任何现有的实用程序来逃避任何字符串的列表?
感谢您的回答,但我的问题是寻找一种更简单/快速/更快的方式,而无需导航字符串并手动添加\“我很惊讶,因为我甚至找不到这种类型只需复制字符串并粘贴即可将其转换,而不必手动添加斜线\以便每次出现双引号“ – 2015-04-05 12:53:43
否,您不必通过'\”将每个''''转义出来'手动,看到我更新的答案。你可以在变量'str'中读取整个字符串,然后简单地使用:'printf'%q \ n“”$ str“' – anubhava 2015-04-05 13:13:30