Sed add newline before last occurrence of brace?2019 Community Moderator ElectionHow does the “tail” command's “-f” parameter work?sed: replacing newline at a given line (or last line)Need to insert single quotes in text file for use as SQL query using sedAppend something to each list in a fileReplacing matching text after two matching lineshow to subtract first value from the last one within a column across a row in shell?Add newline character to fileEdit /etc/lvm/lvm.conf and add a new global filterappending into middle of a string using sedUsing sed to replace the last occurrence of character

Why didn’t Eve recognize the little cockroach as a living organism?

How to preserve electronics (computers, iPads and phones) for hundreds of years

How to make a list of partial sums using forEach

How to leave product feedback on macOS?

Do you waste sorcery points if you try to apply metamagic to a spell from a scroll but fail to cast it?

Limit max CPU usage SQL SERVER with WSRM

How do I prevent inappropriate ads from appearing in my game?

Anime with legendary swords made from talismans and a man who could change them with a shattered body

Pre-Employment Background Check With Consent For Future Checks

Proving an identity involving cross products and coplanar vectors

Determining multivariate least squares with constraint

Can I cause damage to electrical appliances by unplugging them when they are turned on?

Do I have to know the General Relativity theory to understand the concept of inertial frame?

How can ruler support inventing of useful things?

What should be the ideal length of sentences in a blog post for ease of reading?

Should I assume I have passed probation?

How to make money from a browser who sees 5 seconds into the future of any web page?

Does Doodling or Improvising on the Piano Have Any Benefits?

Overlapping circles covering polygon

If A is dense in Q, then it must be dense in R.

Ways of geometrical multiplication

Check if object is null and return null

If the only attacker is removed from combat, is a creature still counted as having attacked this turn?

Do I have to take mana from my deck or hand when tapping a dual land?



Sed add newline before last occurrence of brace?



2019 Community Moderator ElectionHow does the “tail” command's “-f” parameter work?sed: replacing newline at a given line (or last line)Need to insert single quotes in text file for use as SQL query using sedAppend something to each list in a fileReplacing matching text after two matching lineshow to subtract first value from the last one within a column across a row in shell?Add newline character to fileEdit /etc/lvm/lvm.conf and add a new global filterappending into middle of a string using sedUsing sed to replace the last occurrence of character










1















I want to insert a new line before the last occurrence of brace. My text file looks like that



 "accounts": 
"0x0000000000000000000000000000000000000008": "builtin": "name": "alt_bn128_pairing", "activate_at": "0x0", "pricing": "alt_bn128_pairing": "base": 100000, "pair": 80000 ,
"0x00Ea169ce7e0992960D3BdE6F5D539C955316432": "balance": "1606938044258990275541962092341162602522202993782792835301376"



So what I want to do is adding a new account through sed script.



Please note that the new account will be specified with a variable, something like:



"$ACCOUNT_ADDR": "balance": "1606938044258990275541962092341162602522202993782792835301376" 









share|improve this question


























    1















    I want to insert a new line before the last occurrence of brace. My text file looks like that



     "accounts": 
    "0x0000000000000000000000000000000000000008": "builtin": "name": "alt_bn128_pairing", "activate_at": "0x0", "pricing": "alt_bn128_pairing": "base": 100000, "pair": 80000 ,
    "0x00Ea169ce7e0992960D3BdE6F5D539C955316432": "balance": "1606938044258990275541962092341162602522202993782792835301376"



    So what I want to do is adding a new account through sed script.



    Please note that the new account will be specified with a variable, something like:



    "$ACCOUNT_ADDR": "balance": "1606938044258990275541962092341162602522202993782792835301376" 









    share|improve this question
























      1












      1








      1


      0






      I want to insert a new line before the last occurrence of brace. My text file looks like that



       "accounts": 
      "0x0000000000000000000000000000000000000008": "builtin": "name": "alt_bn128_pairing", "activate_at": "0x0", "pricing": "alt_bn128_pairing": "base": 100000, "pair": 80000 ,
      "0x00Ea169ce7e0992960D3BdE6F5D539C955316432": "balance": "1606938044258990275541962092341162602522202993782792835301376"



      So what I want to do is adding a new account through sed script.



      Please note that the new account will be specified with a variable, something like:



      "$ACCOUNT_ADDR": "balance": "1606938044258990275541962092341162602522202993782792835301376" 









      share|improve this question














      I want to insert a new line before the last occurrence of brace. My text file looks like that



       "accounts": 
      "0x0000000000000000000000000000000000000008": "builtin": "name": "alt_bn128_pairing", "activate_at": "0x0", "pricing": "alt_bn128_pairing": "base": 100000, "pair": 80000 ,
      "0x00Ea169ce7e0992960D3BdE6F5D539C955316432": "balance": "1606938044258990275541962092341162602522202993782792835301376"



      So what I want to do is adding a new account through sed script.



      Please note that the new account will be specified with a variable, something like:



      "$ACCOUNT_ADDR": "balance": "1606938044258990275541962092341162602522202993782792835301376" 






      shell-script shell sed command-line






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 12 at 12:37









      Stefano De AngelisStefano De Angelis

      61




      61




















          3 Answers
          3






          active

          oldest

          votes


















          1














          sed is the wrong tool for this job. One of the right tools is jq.


          % cat wibble.json

          "accounts":
          "0x0000000000000000000000000000000000000008": "builtin": "name": "alt_bn128_pairing", "activate_at": "0x0", "pricing": "alt_bn128_pairing": "base": 100000, "pair": 80000 ,
          "0x00Ea169ce7e0992960D3BdE6F5D539C955316432": "balance": "1606938044258990275541962092341162602522202993782792835301376"


          % ACCOUNT_ADDR="0xdeadbeeffeefdface0badd00dcacad0d0eeeeeeee"
          % jq '."accounts"."'"$ACCOUNT_ADDR"'"."balance"="42"' wibble.json

          "accounts":
          "0x0000000000000000000000000000000000000008":
          "builtin":
          "name": "alt_bn128_pairing",
          "activate_at": "0x0",
          "pricing":
          "alt_bn128_pairing":
          "base": 100000,
          "pair": 80000



          ,
          "0x00Ea169ce7e0992960D3BdE6F5D539C955316432":
          "balance": "1606938044258990275541962092341162602522202993782792835301376"
          ,
          "0xdeadbeeffeefdface0badd00dcacad0d0eeeeeeee":
          "balance": "42"



          %


          It also caught the fact that you had a key+value pair with no enclosing object. ☺






          share|improve this answer























          • I just have a text file, not json.

            – Stefano De Angelis
            Mar 12 at 15:51











          • You contradict yourself.

            – JdeBP
            Mar 14 at 9:45











          • It looks like a json but if you read the question I specify that is a text file :)

            – Stefano De Angelis
            Mar 14 at 10:21


















          0














          You could add a line after the occurence of "accounts",



          sed "/accounts/ a
          "$ACCOUNT_ADDR": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
          " file


          (The " are escaped, to insert variable)



          sed "s/^ }$/ "$ACCOUNT_ADDR": "balance": "1606938044258990275541962092341162602522202993782792835301376i" n }/g" file


          Will replace your } line and print two lines.



          Run sed -i ... for changing the file, in cycle:



          $ cat file
          "accounts":
          "0x0000000000000000000000000000000000000008": "builtin": "name": "alt_bn128_pairing", "activate_at": "0x0", "pricing": "alt_bn128_pairing": "base": 100000, "pair": 80000 ,
          "0x00Ea169ce7e0992960D3BdE6F5D539C955316432": "balance": "1606938044258990275541962092341162602522202993782792835301376"


          $ cat script.sh
          #!/bin/bash

          for i in $(seq 1 5); do
          ACCOUNT_ADDR="account_"$i
          sed -i "/accounts/ a
          "$ACCOUNT_ADDR": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
          " file
          done

          $ ./script.sh
          $ cat file
          "accounts":
          "account_5": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
          "account_4": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
          "account_3": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
          "account_2": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
          "account_1": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
          "0x0000000000000000000000000000000000000008": "builtin": "name": "alt_bn128_pairing", "activate_at": "0x0", "pricing": "alt_bn128_pairing": "base": 100000, "pair": 80000 ,
          "0x00Ea169ce7e0992960D3BdE6F5D539C955316432": "balance": "1606938044258990275541962092341162602522202993782792835301376"







          share|improve this answer

























          • The first solution partially works for me. However, since I need to add more than one address i repeat the command inside a for cycle, in this way I am able to iterate over all the addresses. However, this generates several replicas of the entire json, just updating the first account after the keyword "accounts", and not one unique json with the list of all the accounts.

            – Stefano De Angelis
            Mar 12 at 15:49












          • I see, just run sed -i instead of just sed , see my edited answer.

            – ILikeMatDotH
            Mar 13 at 6:50












          • Thank you, this worked!

            – Stefano De Angelis
            Mar 13 at 11:09


















          0














          Every now and and then I like a sed challenge: using ACCOUNT_ADDR=1234:



          sed -n -e '
          x
          $ i
          "'"$ACCOUNT_ADDR"'": "balance":0,
          2,$ p
          $ x; p
          ' file




           "accounts": 
          "0x0000000000000000000000000000000000000008": "builtin": "name": "alt_bn128_pairing", "activate_at": "0x0", "pricing": "alt_bn128_pairing": "base": 100000, "pair": 80000 ,
          "1234": "balance":0,
          "0x00Ea169ce7e0992960D3BdE6F5D539C955316432": "balance": "1606938044258990275541962092341162602522202993782792835301376"



          This uses the x to stash the current line into the hold space to become the "previous" line in the next cycle.



          As mentioned elsewhere, use sed -i to save the edits in-place




          You can get the same result by reversing the file and using a simpler sed command:



          temp=$(mktemp)
          tac file | sed '2a
          "'"$ACCOUNT_ADDR"'": "balance":0,
          ' | tac > "$temp" && mv "$temp" file





          share|improve this answer






















            Your Answer








            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "106"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f505866%2fsed-add-newline-before-last-occurrence-of-brace%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            sed is the wrong tool for this job. One of the right tools is jq.


            % cat wibble.json

            "accounts":
            "0x0000000000000000000000000000000000000008": "builtin": "name": "alt_bn128_pairing", "activate_at": "0x0", "pricing": "alt_bn128_pairing": "base": 100000, "pair": 80000 ,
            "0x00Ea169ce7e0992960D3BdE6F5D539C955316432": "balance": "1606938044258990275541962092341162602522202993782792835301376"


            % ACCOUNT_ADDR="0xdeadbeeffeefdface0badd00dcacad0d0eeeeeeee"
            % jq '."accounts"."'"$ACCOUNT_ADDR"'"."balance"="42"' wibble.json

            "accounts":
            "0x0000000000000000000000000000000000000008":
            "builtin":
            "name": "alt_bn128_pairing",
            "activate_at": "0x0",
            "pricing":
            "alt_bn128_pairing":
            "base": 100000,
            "pair": 80000



            ,
            "0x00Ea169ce7e0992960D3BdE6F5D539C955316432":
            "balance": "1606938044258990275541962092341162602522202993782792835301376"
            ,
            "0xdeadbeeffeefdface0badd00dcacad0d0eeeeeeee":
            "balance": "42"



            %


            It also caught the fact that you had a key+value pair with no enclosing object. ☺






            share|improve this answer























            • I just have a text file, not json.

              – Stefano De Angelis
              Mar 12 at 15:51











            • You contradict yourself.

              – JdeBP
              Mar 14 at 9:45











            • It looks like a json but if you read the question I specify that is a text file :)

              – Stefano De Angelis
              Mar 14 at 10:21















            1














            sed is the wrong tool for this job. One of the right tools is jq.


            % cat wibble.json

            "accounts":
            "0x0000000000000000000000000000000000000008": "builtin": "name": "alt_bn128_pairing", "activate_at": "0x0", "pricing": "alt_bn128_pairing": "base": 100000, "pair": 80000 ,
            "0x00Ea169ce7e0992960D3BdE6F5D539C955316432": "balance": "1606938044258990275541962092341162602522202993782792835301376"


            % ACCOUNT_ADDR="0xdeadbeeffeefdface0badd00dcacad0d0eeeeeeee"
            % jq '."accounts"."'"$ACCOUNT_ADDR"'"."balance"="42"' wibble.json

            "accounts":
            "0x0000000000000000000000000000000000000008":
            "builtin":
            "name": "alt_bn128_pairing",
            "activate_at": "0x0",
            "pricing":
            "alt_bn128_pairing":
            "base": 100000,
            "pair": 80000



            ,
            "0x00Ea169ce7e0992960D3BdE6F5D539C955316432":
            "balance": "1606938044258990275541962092341162602522202993782792835301376"
            ,
            "0xdeadbeeffeefdface0badd00dcacad0d0eeeeeeee":
            "balance": "42"



            %


            It also caught the fact that you had a key+value pair with no enclosing object. ☺






            share|improve this answer























            • I just have a text file, not json.

              – Stefano De Angelis
              Mar 12 at 15:51











            • You contradict yourself.

              – JdeBP
              Mar 14 at 9:45











            • It looks like a json but if you read the question I specify that is a text file :)

              – Stefano De Angelis
              Mar 14 at 10:21













            1












            1








            1







            sed is the wrong tool for this job. One of the right tools is jq.


            % cat wibble.json

            "accounts":
            "0x0000000000000000000000000000000000000008": "builtin": "name": "alt_bn128_pairing", "activate_at": "0x0", "pricing": "alt_bn128_pairing": "base": 100000, "pair": 80000 ,
            "0x00Ea169ce7e0992960D3BdE6F5D539C955316432": "balance": "1606938044258990275541962092341162602522202993782792835301376"


            % ACCOUNT_ADDR="0xdeadbeeffeefdface0badd00dcacad0d0eeeeeeee"
            % jq '."accounts"."'"$ACCOUNT_ADDR"'"."balance"="42"' wibble.json

            "accounts":
            "0x0000000000000000000000000000000000000008":
            "builtin":
            "name": "alt_bn128_pairing",
            "activate_at": "0x0",
            "pricing":
            "alt_bn128_pairing":
            "base": 100000,
            "pair": 80000



            ,
            "0x00Ea169ce7e0992960D3BdE6F5D539C955316432":
            "balance": "1606938044258990275541962092341162602522202993782792835301376"
            ,
            "0xdeadbeeffeefdface0badd00dcacad0d0eeeeeeee":
            "balance": "42"



            %


            It also caught the fact that you had a key+value pair with no enclosing object. ☺






            share|improve this answer













            sed is the wrong tool for this job. One of the right tools is jq.


            % cat wibble.json

            "accounts":
            "0x0000000000000000000000000000000000000008": "builtin": "name": "alt_bn128_pairing", "activate_at": "0x0", "pricing": "alt_bn128_pairing": "base": 100000, "pair": 80000 ,
            "0x00Ea169ce7e0992960D3BdE6F5D539C955316432": "balance": "1606938044258990275541962092341162602522202993782792835301376"


            % ACCOUNT_ADDR="0xdeadbeeffeefdface0badd00dcacad0d0eeeeeeee"
            % jq '."accounts"."'"$ACCOUNT_ADDR"'"."balance"="42"' wibble.json

            "accounts":
            "0x0000000000000000000000000000000000000008":
            "builtin":
            "name": "alt_bn128_pairing",
            "activate_at": "0x0",
            "pricing":
            "alt_bn128_pairing":
            "base": 100000,
            "pair": 80000



            ,
            "0x00Ea169ce7e0992960D3BdE6F5D539C955316432":
            "balance": "1606938044258990275541962092341162602522202993782792835301376"
            ,
            "0xdeadbeeffeefdface0badd00dcacad0d0eeeeeeee":
            "balance": "42"



            %


            It also caught the fact that you had a key+value pair with no enclosing object. ☺







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 12 at 13:23









            JdeBPJdeBP

            37.2k477178




            37.2k477178












            • I just have a text file, not json.

              – Stefano De Angelis
              Mar 12 at 15:51











            • You contradict yourself.

              – JdeBP
              Mar 14 at 9:45











            • It looks like a json but if you read the question I specify that is a text file :)

              – Stefano De Angelis
              Mar 14 at 10:21

















            • I just have a text file, not json.

              – Stefano De Angelis
              Mar 12 at 15:51











            • You contradict yourself.

              – JdeBP
              Mar 14 at 9:45











            • It looks like a json but if you read the question I specify that is a text file :)

              – Stefano De Angelis
              Mar 14 at 10:21
















            I just have a text file, not json.

            – Stefano De Angelis
            Mar 12 at 15:51





            I just have a text file, not json.

            – Stefano De Angelis
            Mar 12 at 15:51













            You contradict yourself.

            – JdeBP
            Mar 14 at 9:45





            You contradict yourself.

            – JdeBP
            Mar 14 at 9:45













            It looks like a json but if you read the question I specify that is a text file :)

            – Stefano De Angelis
            Mar 14 at 10:21





            It looks like a json but if you read the question I specify that is a text file :)

            – Stefano De Angelis
            Mar 14 at 10:21













            0














            You could add a line after the occurence of "accounts",



            sed "/accounts/ a
            "$ACCOUNT_ADDR": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
            " file


            (The " are escaped, to insert variable)



            sed "s/^ }$/ "$ACCOUNT_ADDR": "balance": "1606938044258990275541962092341162602522202993782792835301376i" n }/g" file


            Will replace your } line and print two lines.



            Run sed -i ... for changing the file, in cycle:



            $ cat file
            "accounts":
            "0x0000000000000000000000000000000000000008": "builtin": "name": "alt_bn128_pairing", "activate_at": "0x0", "pricing": "alt_bn128_pairing": "base": 100000, "pair": 80000 ,
            "0x00Ea169ce7e0992960D3BdE6F5D539C955316432": "balance": "1606938044258990275541962092341162602522202993782792835301376"


            $ cat script.sh
            #!/bin/bash

            for i in $(seq 1 5); do
            ACCOUNT_ADDR="account_"$i
            sed -i "/accounts/ a
            "$ACCOUNT_ADDR": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
            " file
            done

            $ ./script.sh
            $ cat file
            "accounts":
            "account_5": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
            "account_4": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
            "account_3": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
            "account_2": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
            "account_1": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
            "0x0000000000000000000000000000000000000008": "builtin": "name": "alt_bn128_pairing", "activate_at": "0x0", "pricing": "alt_bn128_pairing": "base": 100000, "pair": 80000 ,
            "0x00Ea169ce7e0992960D3BdE6F5D539C955316432": "balance": "1606938044258990275541962092341162602522202993782792835301376"







            share|improve this answer

























            • The first solution partially works for me. However, since I need to add more than one address i repeat the command inside a for cycle, in this way I am able to iterate over all the addresses. However, this generates several replicas of the entire json, just updating the first account after the keyword "accounts", and not one unique json with the list of all the accounts.

              – Stefano De Angelis
              Mar 12 at 15:49












            • I see, just run sed -i instead of just sed , see my edited answer.

              – ILikeMatDotH
              Mar 13 at 6:50












            • Thank you, this worked!

              – Stefano De Angelis
              Mar 13 at 11:09















            0














            You could add a line after the occurence of "accounts",



            sed "/accounts/ a
            "$ACCOUNT_ADDR": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
            " file


            (The " are escaped, to insert variable)



            sed "s/^ }$/ "$ACCOUNT_ADDR": "balance": "1606938044258990275541962092341162602522202993782792835301376i" n }/g" file


            Will replace your } line and print two lines.



            Run sed -i ... for changing the file, in cycle:



            $ cat file
            "accounts":
            "0x0000000000000000000000000000000000000008": "builtin": "name": "alt_bn128_pairing", "activate_at": "0x0", "pricing": "alt_bn128_pairing": "base": 100000, "pair": 80000 ,
            "0x00Ea169ce7e0992960D3BdE6F5D539C955316432": "balance": "1606938044258990275541962092341162602522202993782792835301376"


            $ cat script.sh
            #!/bin/bash

            for i in $(seq 1 5); do
            ACCOUNT_ADDR="account_"$i
            sed -i "/accounts/ a
            "$ACCOUNT_ADDR": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
            " file
            done

            $ ./script.sh
            $ cat file
            "accounts":
            "account_5": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
            "account_4": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
            "account_3": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
            "account_2": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
            "account_1": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
            "0x0000000000000000000000000000000000000008": "builtin": "name": "alt_bn128_pairing", "activate_at": "0x0", "pricing": "alt_bn128_pairing": "base": 100000, "pair": 80000 ,
            "0x00Ea169ce7e0992960D3BdE6F5D539C955316432": "balance": "1606938044258990275541962092341162602522202993782792835301376"







            share|improve this answer

























            • The first solution partially works for me. However, since I need to add more than one address i repeat the command inside a for cycle, in this way I am able to iterate over all the addresses. However, this generates several replicas of the entire json, just updating the first account after the keyword "accounts", and not one unique json with the list of all the accounts.

              – Stefano De Angelis
              Mar 12 at 15:49












            • I see, just run sed -i instead of just sed , see my edited answer.

              – ILikeMatDotH
              Mar 13 at 6:50












            • Thank you, this worked!

              – Stefano De Angelis
              Mar 13 at 11:09













            0












            0








            0







            You could add a line after the occurence of "accounts",



            sed "/accounts/ a
            "$ACCOUNT_ADDR": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
            " file


            (The " are escaped, to insert variable)



            sed "s/^ }$/ "$ACCOUNT_ADDR": "balance": "1606938044258990275541962092341162602522202993782792835301376i" n }/g" file


            Will replace your } line and print two lines.



            Run sed -i ... for changing the file, in cycle:



            $ cat file
            "accounts":
            "0x0000000000000000000000000000000000000008": "builtin": "name": "alt_bn128_pairing", "activate_at": "0x0", "pricing": "alt_bn128_pairing": "base": 100000, "pair": 80000 ,
            "0x00Ea169ce7e0992960D3BdE6F5D539C955316432": "balance": "1606938044258990275541962092341162602522202993782792835301376"


            $ cat script.sh
            #!/bin/bash

            for i in $(seq 1 5); do
            ACCOUNT_ADDR="account_"$i
            sed -i "/accounts/ a
            "$ACCOUNT_ADDR": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
            " file
            done

            $ ./script.sh
            $ cat file
            "accounts":
            "account_5": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
            "account_4": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
            "account_3": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
            "account_2": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
            "account_1": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
            "0x0000000000000000000000000000000000000008": "builtin": "name": "alt_bn128_pairing", "activate_at": "0x0", "pricing": "alt_bn128_pairing": "base": 100000, "pair": 80000 ,
            "0x00Ea169ce7e0992960D3BdE6F5D539C955316432": "balance": "1606938044258990275541962092341162602522202993782792835301376"







            share|improve this answer















            You could add a line after the occurence of "accounts",



            sed "/accounts/ a
            "$ACCOUNT_ADDR": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
            " file


            (The " are escaped, to insert variable)



            sed "s/^ }$/ "$ACCOUNT_ADDR": "balance": "1606938044258990275541962092341162602522202993782792835301376i" n }/g" file


            Will replace your } line and print two lines.



            Run sed -i ... for changing the file, in cycle:



            $ cat file
            "accounts":
            "0x0000000000000000000000000000000000000008": "builtin": "name": "alt_bn128_pairing", "activate_at": "0x0", "pricing": "alt_bn128_pairing": "base": 100000, "pair": 80000 ,
            "0x00Ea169ce7e0992960D3BdE6F5D539C955316432": "balance": "1606938044258990275541962092341162602522202993782792835301376"


            $ cat script.sh
            #!/bin/bash

            for i in $(seq 1 5); do
            ACCOUNT_ADDR="account_"$i
            sed -i "/accounts/ a
            "$ACCOUNT_ADDR": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
            " file
            done

            $ ./script.sh
            $ cat file
            "accounts":
            "account_5": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
            "account_4": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
            "account_3": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
            "account_2": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
            "account_1": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
            "0x0000000000000000000000000000000000000008": "builtin": "name": "alt_bn128_pairing", "activate_at": "0x0", "pricing": "alt_bn128_pairing": "base": 100000, "pair": 80000 ,
            "0x00Ea169ce7e0992960D3BdE6F5D539C955316432": "balance": "1606938044258990275541962092341162602522202993782792835301376"








            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Mar 13 at 6:54

























            answered Mar 12 at 12:55









            ILikeMatDotHILikeMatDotH

            915




            915












            • The first solution partially works for me. However, since I need to add more than one address i repeat the command inside a for cycle, in this way I am able to iterate over all the addresses. However, this generates several replicas of the entire json, just updating the first account after the keyword "accounts", and not one unique json with the list of all the accounts.

              – Stefano De Angelis
              Mar 12 at 15:49












            • I see, just run sed -i instead of just sed , see my edited answer.

              – ILikeMatDotH
              Mar 13 at 6:50












            • Thank you, this worked!

              – Stefano De Angelis
              Mar 13 at 11:09

















            • The first solution partially works for me. However, since I need to add more than one address i repeat the command inside a for cycle, in this way I am able to iterate over all the addresses. However, this generates several replicas of the entire json, just updating the first account after the keyword "accounts", and not one unique json with the list of all the accounts.

              – Stefano De Angelis
              Mar 12 at 15:49












            • I see, just run sed -i instead of just sed , see my edited answer.

              – ILikeMatDotH
              Mar 13 at 6:50












            • Thank you, this worked!

              – Stefano De Angelis
              Mar 13 at 11:09
















            The first solution partially works for me. However, since I need to add more than one address i repeat the command inside a for cycle, in this way I am able to iterate over all the addresses. However, this generates several replicas of the entire json, just updating the first account after the keyword "accounts", and not one unique json with the list of all the accounts.

            – Stefano De Angelis
            Mar 12 at 15:49






            The first solution partially works for me. However, since I need to add more than one address i repeat the command inside a for cycle, in this way I am able to iterate over all the addresses. However, this generates several replicas of the entire json, just updating the first account after the keyword "accounts", and not one unique json with the list of all the accounts.

            – Stefano De Angelis
            Mar 12 at 15:49














            I see, just run sed -i instead of just sed , see my edited answer.

            – ILikeMatDotH
            Mar 13 at 6:50






            I see, just run sed -i instead of just sed , see my edited answer.

            – ILikeMatDotH
            Mar 13 at 6:50














            Thank you, this worked!

            – Stefano De Angelis
            Mar 13 at 11:09





            Thank you, this worked!

            – Stefano De Angelis
            Mar 13 at 11:09











            0














            Every now and and then I like a sed challenge: using ACCOUNT_ADDR=1234:



            sed -n -e '
            x
            $ i
            "'"$ACCOUNT_ADDR"'": "balance":0,
            2,$ p
            $ x; p
            ' file




             "accounts": 
            "0x0000000000000000000000000000000000000008": "builtin": "name": "alt_bn128_pairing", "activate_at": "0x0", "pricing": "alt_bn128_pairing": "base": 100000, "pair": 80000 ,
            "1234": "balance":0,
            "0x00Ea169ce7e0992960D3BdE6F5D539C955316432": "balance": "1606938044258990275541962092341162602522202993782792835301376"



            This uses the x to stash the current line into the hold space to become the "previous" line in the next cycle.



            As mentioned elsewhere, use sed -i to save the edits in-place




            You can get the same result by reversing the file and using a simpler sed command:



            temp=$(mktemp)
            tac file | sed '2a
            "'"$ACCOUNT_ADDR"'": "balance":0,
            ' | tac > "$temp" && mv "$temp" file





            share|improve this answer



























              0














              Every now and and then I like a sed challenge: using ACCOUNT_ADDR=1234:



              sed -n -e '
              x
              $ i
              "'"$ACCOUNT_ADDR"'": "balance":0,
              2,$ p
              $ x; p
              ' file




               "accounts": 
              "0x0000000000000000000000000000000000000008": "builtin": "name": "alt_bn128_pairing", "activate_at": "0x0", "pricing": "alt_bn128_pairing": "base": 100000, "pair": 80000 ,
              "1234": "balance":0,
              "0x00Ea169ce7e0992960D3BdE6F5D539C955316432": "balance": "1606938044258990275541962092341162602522202993782792835301376"



              This uses the x to stash the current line into the hold space to become the "previous" line in the next cycle.



              As mentioned elsewhere, use sed -i to save the edits in-place




              You can get the same result by reversing the file and using a simpler sed command:



              temp=$(mktemp)
              tac file | sed '2a
              "'"$ACCOUNT_ADDR"'": "balance":0,
              ' | tac > "$temp" && mv "$temp" file





              share|improve this answer

























                0












                0








                0







                Every now and and then I like a sed challenge: using ACCOUNT_ADDR=1234:



                sed -n -e '
                x
                $ i
                "'"$ACCOUNT_ADDR"'": "balance":0,
                2,$ p
                $ x; p
                ' file




                 "accounts": 
                "0x0000000000000000000000000000000000000008": "builtin": "name": "alt_bn128_pairing", "activate_at": "0x0", "pricing": "alt_bn128_pairing": "base": 100000, "pair": 80000 ,
                "1234": "balance":0,
                "0x00Ea169ce7e0992960D3BdE6F5D539C955316432": "balance": "1606938044258990275541962092341162602522202993782792835301376"



                This uses the x to stash the current line into the hold space to become the "previous" line in the next cycle.



                As mentioned elsewhere, use sed -i to save the edits in-place




                You can get the same result by reversing the file and using a simpler sed command:



                temp=$(mktemp)
                tac file | sed '2a
                "'"$ACCOUNT_ADDR"'": "balance":0,
                ' | tac > "$temp" && mv "$temp" file





                share|improve this answer













                Every now and and then I like a sed challenge: using ACCOUNT_ADDR=1234:



                sed -n -e '
                x
                $ i
                "'"$ACCOUNT_ADDR"'": "balance":0,
                2,$ p
                $ x; p
                ' file




                 "accounts": 
                "0x0000000000000000000000000000000000000008": "builtin": "name": "alt_bn128_pairing", "activate_at": "0x0", "pricing": "alt_bn128_pairing": "base": 100000, "pair": 80000 ,
                "1234": "balance":0,
                "0x00Ea169ce7e0992960D3BdE6F5D539C955316432": "balance": "1606938044258990275541962092341162602522202993782792835301376"



                This uses the x to stash the current line into the hold space to become the "previous" line in the next cycle.



                As mentioned elsewhere, use sed -i to save the edits in-place




                You can get the same result by reversing the file and using a simpler sed command:



                temp=$(mktemp)
                tac file | sed '2a
                "'"$ACCOUNT_ADDR"'": "balance":0,
                ' | tac > "$temp" && mv "$temp" file






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 23 hours ago









                glenn jackmanglenn jackman

                52.4k573113




                52.4k573113



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Unix & Linux Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f505866%2fsed-add-newline-before-last-occurrence-of-brace%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    getting Checkpoint VPN SSL Network Extender working in the command lineHow to connect to CheckPoint VPN on Ubuntu 18.04LTS?Will the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayVPN SSL Network Extender in FirefoxLinux Checkpoint SNX tool configuration issuesCheck Point - Connect under Linux - snx + OTPSNX VPN Ububuntu 18.XXUsing Checkpoint VPN SSL Network Extender CLI with certificateVPN with network manager (nm-applet) is not workingWill the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayImport VPN config files to NetworkManager from command lineTrouble connecting to VPN using network-manager, while command line worksStart a VPN connection with PPTP protocol on command linestarting a docker service daemon breaks the vpn networkCan't connect to vpn with Network-managerVPN SSL Network Extender in FirefoxUsing Checkpoint VPN SSL Network Extender CLI with certificate

                    Cannot Extend partition with GParted The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election ResultsCan't increase partition size with GParted?GParted doesn't recognize the unallocated space after my current partitionWhat is the best way to add unallocated space located before to Ubuntu 12.04 partition with GParted live?I can't figure out how to extend my Arch home partition into free spaceGparted Linux Mint 18.1 issueTrying to extend but swap partition is showing as Unknown in Gparted, shows proper from fdiskRearrange partitions in gparted to extend a partitionUnable to extend partition even though unallocated space is next to it using GPartedAllocate free space to root partitiongparted: how to merge unallocated space with a partition

                    Marilyn Monroe Ny fiainany manokana | Jereo koa | Meny fitetezanafanitarana azy.