-1

I need help to write bash script, one of its part has a task to insert text between such tokens - first occurring { and last closed ] The real thing as following

{  <-----here start
"pools" : [
        {
                "url" : "stratum+tcp://server:3333",
                "user" : "User",
                "pass" : "x"
        },

        {
                "url" : "stratum+tcp://server1:3333",
                "user" : "User",
                "pass" : "x"

        }
] <-----here end

My script has aim to insert pool config for multiple servers. Bellow last closed ] there are another settings which should stay untouched.

I found a solution it extracts text between defined tokens, expression as follows:

sed -n '/{/{:a;n;/]/b;p;ba}' input.txt

But my task bit complex. I need to replace remote host config

{
"pools" : [
        {
                "url" : "stratum+tcp://server:3333",
                "user" : "User",
                "pass" : "x"
        },

        {
                "url" : "stratum+tcp://server1:3333",
                "user" : "User",
                "pass" : "x"

        }
]
some settings
.......
another seetings
}

With new server settings

{  
"pools" : [
        {
                "url" : "stratum+tcp://anotherserver:3333",
                "user" : "User",
                "pass" : "x"
        },

        {
                "url" : "stratum+tcp://anotherserver1:3333",
                "user" : "User",
                "pass" : "x"

        }
]

I can append/replace new config on remote host as following

echo '{
"pools" : [
        {
                "url" : "stratum+tcp://anotherserver:3333",
                "user" : "User",
                "pass" : "x"
        },

        {
                "url" : "stratum+tcp://anotherserver1:3333",
                "user" : "User",
                "pass" : "x"

        }
]'| ssh root@$HOST -p $PORT 'cat > /remotehost/config.conf'

But the problem is, I need to keep original settings on remote host below ] token and only replace server section.

Demontager
  • 217
  • 1
  • 5
  • 12
  • Maybe there are other hints, like `{` you want to start with is always first bracket in a file, or it starts at first column? It would help you a *lot* – Jakub M. Mar 09 '14 at 13:12
  • 1
    It's very odd that you want to start at a `{` and stop at a `]`; unbalanced operations like that tend to give people the jitters (well, it leaves me wondering what on earth is supposed to be going on). Are you sure you don't want to start at the `"pools" : [` part? – Jonathan Leffler Mar 09 '14 at 15:45
  • 1
    You should use a language with a decent JSON parser instead of `bash`. – chepner Mar 09 '14 at 15:52
  • No, i may also start from "pools" as well. The aim is to replace server config part on remote host. – Demontager Mar 09 '14 at 15:56
  • possible duplicate of [Parsing json with sed and awk](http://stackoverflow.com/questions/1955505/parsing-json-with-sed-and-awk) – tripleee Mar 09 '14 at 16:56
  • I still believe it is possible to get done with bash only. Also, unfortunately i don't familiar with Json too. – Demontager Mar 09 '14 at 17:12
  • The format you are trying to manipulate is called JSON. Having already manipulated it, you are more familiar with it than you thought. – tripleee Mar 09 '14 at 19:36
  • Wise and short answer made by BryanH(3) here http://stackoverflow.com/questions/1955505/parsing-json-with-sed-and-awk Of course it's also interesting for me how to done my task with JSON. But for now after reading entire thread still don't understand where to start. – Demontager Mar 09 '14 at 20:35

1 Answers1

0

I accomplished my task with bash only. Maybe it's not elegant, because used temporary files, but works great.

cat <<'EOF' | ssh root@$HOST -p $PORT 'cat - > /tmp/pool.tmp && sed -n "/]/{:a;n;/}/b;p;ba}" /etc/bamt/cgminer.conf > /tmp/cgminer.conf.tmp \
&& cat /tmp/pool.tmp /tmp/cgminer.conf.tmp > /etc/bamt/cgminer.conf && echo "}" >> /etc/bamt/cgminer.conf \
&& rm /tmp/pool.tmp /tmp/cgminer.conf.tmp'  
{
"pools" : [
        {
                "url" : "stratum+tcp://server:3333",
                "user" : "User",
                "pass" : "x"
        },

        {
                "url" : "stratum+tcp://server1:3333",
                "user" : "User",
                "pass" : "x"

        }
]
EOF

Explanation

Entire server section in script goes to /tmp/pool.tmp (remote sever) then on server side extracted all settings below ] tokens to temporary file cgminer.conf.tmp. After two temporary files concatenated to one single cgminer.conf. When all done temp files removed.

Demontager
  • 217
  • 1
  • 5
  • 12