通过SSH运行复杂的sed命令时出现问题 [英] Problems running a complicated sed command over SSH
问题描述
我已经阅读了有关该主题的其他多个答案,但似乎都无法解决我的问题.
I've read multiple other answers on the topic but none seem to work for my problem.
I just got an answer to my other question on how to add prefixes to 2 strings of text in a long, single-line .json file.
命令是
sed -i 's~"libraries"~"pack/\0"~; s~"objects"~"pack/\0"~' /var/www/html/pack/file.json
,其中包含单引号和双引号以及正斜杠和双斜杠,使其他答案失败.
and it contains single and double quotes, as well as forward and double slashes, making other answers fail.
我尝试过
ssh ip<<'EOF'
sed -i 's~"libraries"~"pack/\0"~; s~"objects"~"pack/\0"~' /var/www/html/pack/file.json
EOF
ssh ip 'sed -i '"'"'s~"libraries"~"pack/\0"~; s~"objects"~"pack/\0"~'"'"' /var/www/html/pack/file.json'
和此页面上的所有内容,并且可能还有其他一些答案,没有运气.
and everything on this page, and possibly a couple other things from answers here, with no luck.
第一个带有 EOF
的命令将打开与 ssh ip
-ing相同的屏幕;远程命令提示符.Zaiste.net页面上的其他一些命令也会发生同样的情况.
The first command, with EOF
, opens the same screen I would get when simply ssh ip
-ing; a remote command prompt. The same happens with some other commands from the Zaiste.net page.
请注意, sed
命令是从终端中执行的脚本运行的.因此,多个行的答案都可以.
Please note that the sed
command is being run from a script executed in terminal. Thus, multiple line answers are OK.
还
ssh ip 'sed -i '"'"'s~"libraries"~"pack/\0"~; s~"objects"~"pack/\0"~'"'"' /var/www/html/pack/file.json'
例如,在我要作为前缀的对象"和库"字符串周围返回 EXTRA 引号
returns EXTRA quotes around the "objects" and "libraries" strings I am trying to prefix, for example
"minimumVersion":2,"librariesLocation":"pack/"库"","objectsLocation":"pack/" objects"",
"minimumVersion":2,"librariesLocation":"pack/"libraries"","objectsLocation":"pack/"objects"",
如果此命令引起的多余引号可以解决,那就是我的问题的解决方案.
If the extra quotes issue from this command can be resolved, that's the solution to my problem.
推荐答案
您可以使用:
ssh -t -t ip<<'EOF'
sed -i 's~"libraries"~"pack/&"~; s~"objects"~"pack/&"~' /var/www/html/pack/file.json
exit
EOF
这篇关于通过SSH运行复杂的sed命令时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!