Sed add newline before last occurrence of brace?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








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.9k478183




            37.9k478183












            • 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 Mar 20 at 1:42









                glenn jackmanglenn jackman

                53.1k573114




                53.1k573114



























                    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

                    How to check contact read email or not when send email to Individual?

                    Displaying single band from multi-band raster using QGIS

                    How many registers does an x86_64 CPU actually have?