[英]Ruby Escape Backslash For Sed Command
I have a sed
command:我有一个sed
命令:
sed "\#/,#d" /tmp/dest_sorted
I need to run this command in my Ruby script.我需要在我的 Ruby 脚本中运行这个命令。 I've tried this:我试过这个:
sed_command = "sed '\#/,#d' /tmp/dest_sorted"
p sed_command
I get the following result:我得到以下结果:
"sed '#/,#d' /tmp/dest_sorted"
I've tried adding a second backslash:我试过添加第二个反斜杠:
sed_command = "sed '\\#/,#d' /tmp/dest_sorted"
but I get this:但我明白了:
"sed '\\#/,#d' /tmp/dest_sorted"
How can I preserve the single backslash at the beginning of my sed
command?如何在sed
命令开头保留单个反斜杠?
You can use single-quoted (verbatim) strings and escape single quotes inside them:您可以使用单引号(逐字)字符串并在其中转义单引号:
puts 'sed \'\#/,#d\' /tmp/dest_sorted'
#> sed '\#/,#d' /tmp/dest_sorted
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.