Problems running a complicated sed command over SS

2019-08-15 19:25发布

问题:

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.

The command is

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.

I've tried

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.

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.

Please note that the sed command is being run from a script executed in terminal. Thus, multiple line answers are OK.

Also,

ssh ip 'sed -i '"'"'s~"libraries"~"pack/\0"~; s~"objects"~"pack/\0"~'"'"' /var/www/html/pack/file.json'

returns EXTRA quotes around the "objects" and "libraries" strings I am trying to prefix, for example

"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.

回答1:

You can use:

ssh -t -t ip<<'EOF'
sed -i 's~"libraries"~"pack/&"~; s~"objects"~"pack/&"~' /var/www/html/pack/file.json
exit
EOF