Linux sed command replace two lines having spaces and special characters using regular expression

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












0















I have one file having following example text.



"list-value": 
"object-value":
"value": "example-value-123"




I want to change value text "example-value-123" by matching pattern from list-value with value text "example-value-changed-567"



Example



 "list-value": 
"object-value":
"value": "example-value-changed-567"




I don't want to match text pattern starting from value. I have to match pattern starting from list value.
I have tried to create regular expression but not able to match pattern after new line found.



Regular expression I have tried so far



 $ sed -i 's/ [[:space:]]"list-value":[[:space:]][[:space:]]N;[[:space:]]"objet-value"[[:space:]]:[[:space:]]"[w+W+]+"[[:space:]] / [[:space:]]"list-value":[[:space:]][[:space:]]N;[[:space:]]"objet-value"[[:space:]]:[[:space:]]"example-value-changed-567"[[:space:]]/g' file.json


I am getting below error for above command



sed: 1: "file. ...": command a expects followed by text


I have created one regular expression online but it is not woking with sed command.



s+"list-value":s+s+"value"s*:s+"[w+W+]+"s+


I need to find better and working regular expression for sed command.










share|improve this question
























  • You never mention what you want to do with this JSON document, or whether this is the complete document (it looks malformed since there's no enclosing ...). You also fail to say in what way your attempt failed.

    – Kusalananda
    Jan 11 at 9:42












  • @Kusalananda I have updated the Question

    – Oomph Fortuity
    Jan 11 at 9:48
















0















I have one file having following example text.



"list-value": 
"object-value":
"value": "example-value-123"




I want to change value text "example-value-123" by matching pattern from list-value with value text "example-value-changed-567"



Example



 "list-value": 
"object-value":
"value": "example-value-changed-567"




I don't want to match text pattern starting from value. I have to match pattern starting from list value.
I have tried to create regular expression but not able to match pattern after new line found.



Regular expression I have tried so far



 $ sed -i 's/ [[:space:]]"list-value":[[:space:]][[:space:]]N;[[:space:]]"objet-value"[[:space:]]:[[:space:]]"[w+W+]+"[[:space:]] / [[:space:]]"list-value":[[:space:]][[:space:]]N;[[:space:]]"objet-value"[[:space:]]:[[:space:]]"example-value-changed-567"[[:space:]]/g' file.json


I am getting below error for above command



sed: 1: "file. ...": command a expects followed by text


I have created one regular expression online but it is not woking with sed command.



s+"list-value":s+s+"value"s*:s+"[w+W+]+"s+


I need to find better and working regular expression for sed command.










share|improve this question
























  • You never mention what you want to do with this JSON document, or whether this is the complete document (it looks malformed since there's no enclosing ...). You also fail to say in what way your attempt failed.

    – Kusalananda
    Jan 11 at 9:42












  • @Kusalananda I have updated the Question

    – Oomph Fortuity
    Jan 11 at 9:48














0












0








0








I have one file having following example text.



"list-value": 
"object-value":
"value": "example-value-123"




I want to change value text "example-value-123" by matching pattern from list-value with value text "example-value-changed-567"



Example



 "list-value": 
"object-value":
"value": "example-value-changed-567"




I don't want to match text pattern starting from value. I have to match pattern starting from list value.
I have tried to create regular expression but not able to match pattern after new line found.



Regular expression I have tried so far



 $ sed -i 's/ [[:space:]]"list-value":[[:space:]][[:space:]]N;[[:space:]]"objet-value"[[:space:]]:[[:space:]]"[w+W+]+"[[:space:]] / [[:space:]]"list-value":[[:space:]][[:space:]]N;[[:space:]]"objet-value"[[:space:]]:[[:space:]]"example-value-changed-567"[[:space:]]/g' file.json


I am getting below error for above command



sed: 1: "file. ...": command a expects followed by text


I have created one regular expression online but it is not woking with sed command.



s+"list-value":s+s+"value"s*:s+"[w+W+]+"s+


I need to find better and working regular expression for sed command.










share|improve this question
















I have one file having following example text.



"list-value": 
"object-value":
"value": "example-value-123"




I want to change value text "example-value-123" by matching pattern from list-value with value text "example-value-changed-567"



Example



 "list-value": 
"object-value":
"value": "example-value-changed-567"




I don't want to match text pattern starting from value. I have to match pattern starting from list value.
I have tried to create regular expression but not able to match pattern after new line found.



Regular expression I have tried so far



 $ sed -i 's/ [[:space:]]"list-value":[[:space:]][[:space:]]N;[[:space:]]"objet-value"[[:space:]]:[[:space:]]"[w+W+]+"[[:space:]] / [[:space:]]"list-value":[[:space:]][[:space:]]N;[[:space:]]"objet-value"[[:space:]]:[[:space:]]"example-value-changed-567"[[:space:]]/g' file.json


I am getting below error for above command



sed: 1: "file. ...": command a expects followed by text


I have created one regular expression online but it is not woking with sed command.



s+"list-value":s+s+"value"s*:s+"[w+W+]+"s+


I need to find better and working regular expression for sed command.







linux sed regular-expression json






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 12 at 1:12









Rui F Ribeiro

39.6k1479132




39.6k1479132










asked Jan 11 at 9:38









Oomph FortuityOomph Fortuity

1012




1012












  • You never mention what you want to do with this JSON document, or whether this is the complete document (it looks malformed since there's no enclosing ...). You also fail to say in what way your attempt failed.

    – Kusalananda
    Jan 11 at 9:42












  • @Kusalananda I have updated the Question

    – Oomph Fortuity
    Jan 11 at 9:48


















  • You never mention what you want to do with this JSON document, or whether this is the complete document (it looks malformed since there's no enclosing ...). You also fail to say in what way your attempt failed.

    – Kusalananda
    Jan 11 at 9:42












  • @Kusalananda I have updated the Question

    – Oomph Fortuity
    Jan 11 at 9:48

















You never mention what you want to do with this JSON document, or whether this is the complete document (it looks malformed since there's no enclosing ...). You also fail to say in what way your attempt failed.

– Kusalananda
Jan 11 at 9:42






You never mention what you want to do with this JSON document, or whether this is the complete document (it looks malformed since there's no enclosing ...). You also fail to say in what way your attempt failed.

– Kusalananda
Jan 11 at 9:42














@Kusalananda I have updated the Question

– Oomph Fortuity
Jan 11 at 9:48






@Kusalananda I have updated the Question

– Oomph Fortuity
Jan 11 at 9:48











3 Answers
3






active

oldest

votes


















2














Assuming that the JSON document is properly formatted, as




"list-value":
"object-value":
"value": "example-value-123"





jq may be used to modify the value:



$ jq '."list-value"."object-value".value="example-value-changed-567"' file.json

"list-value":
"object-value":
"value": "example-value-changed-567"





The result may be redirected to a new file and that file could replace the original file if you wished to change that:



tmpfile=$(mktemp)
cp file.json "$tmpfile"
jq '."list-value"."object-value".value="example-value-changed-567"' "$tmpfile" >file.json
rm "$tmpfile"


I'd avoid parsing a structured document format like JSON (or XML, YAML, HTML etc.) with a line-oriented tool like sed. If there is a parser for the format that you can use, then use that instead. Parsers for JSON are available in all major programming languages. This makes your code more robust as you don't have to care about the actual formatting or character encoding used in the input data. Note, for example, that your document is equivalent to



 "list-value": 
"object-value": "value":
"example-value-123"








share|improve this answer






























    1














    I think you're looking for this as a sed command:



    sed -i.backup '/list-value/N;N;s/example-value-123/example-value-changed-567/' file.json


    Example:



    $ cat ./file.json
    "list-value":
    "object-value":
    "value": "example-value-123"


    "list-value-2":
    "object-value":
    "value": "example-value-123"



    # please note this creates a backup file: file.json.backup
    $ sed -i.backup '/list-value/N;N;s/example-value-123/example-value-changed-567/' file.json

    $ cat ./file.json
    "list-value":
    "object-value":
    "value": "example-value-changed-567"


    "list-value-2":
    "object-value":
    "value": "example-value-123"







    share|improve this answer






























      0














      Nested structures, namely JSON, must be processed by structure aware (here JSON) routines. Whenever you attempt processing it via (multi) line-aware only routines, you'll be always exposed to false-positives.



      Thus, your ask (again, asusminig it's properly formed JSON) is easily achievable via jtc:



      bash $ jtc -w'[list-value]<value>l' -u'"example-value-changed-567"' file.json

      "list-value":
      "object-value":
      "value": "example-value-changed-567"



      bash $





      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%2f493907%2flinux-sed-command-replace-two-lines-having-spaces-and-special-characters-using-r%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









        2














        Assuming that the JSON document is properly formatted, as




        "list-value":
        "object-value":
        "value": "example-value-123"





        jq may be used to modify the value:



        $ jq '."list-value"."object-value".value="example-value-changed-567"' file.json

        "list-value":
        "object-value":
        "value": "example-value-changed-567"





        The result may be redirected to a new file and that file could replace the original file if you wished to change that:



        tmpfile=$(mktemp)
        cp file.json "$tmpfile"
        jq '."list-value"."object-value".value="example-value-changed-567"' "$tmpfile" >file.json
        rm "$tmpfile"


        I'd avoid parsing a structured document format like JSON (or XML, YAML, HTML etc.) with a line-oriented tool like sed. If there is a parser for the format that you can use, then use that instead. Parsers for JSON are available in all major programming languages. This makes your code more robust as you don't have to care about the actual formatting or character encoding used in the input data. Note, for example, that your document is equivalent to



         "list-value": 
        "object-value": "value":
        "example-value-123"








        share|improve this answer



























          2














          Assuming that the JSON document is properly formatted, as




          "list-value":
          "object-value":
          "value": "example-value-123"





          jq may be used to modify the value:



          $ jq '."list-value"."object-value".value="example-value-changed-567"' file.json

          "list-value":
          "object-value":
          "value": "example-value-changed-567"





          The result may be redirected to a new file and that file could replace the original file if you wished to change that:



          tmpfile=$(mktemp)
          cp file.json "$tmpfile"
          jq '."list-value"."object-value".value="example-value-changed-567"' "$tmpfile" >file.json
          rm "$tmpfile"


          I'd avoid parsing a structured document format like JSON (or XML, YAML, HTML etc.) with a line-oriented tool like sed. If there is a parser for the format that you can use, then use that instead. Parsers for JSON are available in all major programming languages. This makes your code more robust as you don't have to care about the actual formatting or character encoding used in the input data. Note, for example, that your document is equivalent to



           "list-value": 
          "object-value": "value":
          "example-value-123"








          share|improve this answer

























            2












            2








            2







            Assuming that the JSON document is properly formatted, as




            "list-value":
            "object-value":
            "value": "example-value-123"





            jq may be used to modify the value:



            $ jq '."list-value"."object-value".value="example-value-changed-567"' file.json

            "list-value":
            "object-value":
            "value": "example-value-changed-567"





            The result may be redirected to a new file and that file could replace the original file if you wished to change that:



            tmpfile=$(mktemp)
            cp file.json "$tmpfile"
            jq '."list-value"."object-value".value="example-value-changed-567"' "$tmpfile" >file.json
            rm "$tmpfile"


            I'd avoid parsing a structured document format like JSON (or XML, YAML, HTML etc.) with a line-oriented tool like sed. If there is a parser for the format that you can use, then use that instead. Parsers for JSON are available in all major programming languages. This makes your code more robust as you don't have to care about the actual formatting or character encoding used in the input data. Note, for example, that your document is equivalent to



             "list-value": 
            "object-value": "value":
            "example-value-123"








            share|improve this answer













            Assuming that the JSON document is properly formatted, as




            "list-value":
            "object-value":
            "value": "example-value-123"





            jq may be used to modify the value:



            $ jq '."list-value"."object-value".value="example-value-changed-567"' file.json

            "list-value":
            "object-value":
            "value": "example-value-changed-567"





            The result may be redirected to a new file and that file could replace the original file if you wished to change that:



            tmpfile=$(mktemp)
            cp file.json "$tmpfile"
            jq '."list-value"."object-value".value="example-value-changed-567"' "$tmpfile" >file.json
            rm "$tmpfile"


            I'd avoid parsing a structured document format like JSON (or XML, YAML, HTML etc.) with a line-oriented tool like sed. If there is a parser for the format that you can use, then use that instead. Parsers for JSON are available in all major programming languages. This makes your code more robust as you don't have to care about the actual formatting or character encoding used in the input data. Note, for example, that your document is equivalent to



             "list-value": 
            "object-value": "value":
            "example-value-123"









            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 11 at 10:07









            KusalanandaKusalananda

            126k16239393




            126k16239393























                1














                I think you're looking for this as a sed command:



                sed -i.backup '/list-value/N;N;s/example-value-123/example-value-changed-567/' file.json


                Example:



                $ cat ./file.json
                "list-value":
                "object-value":
                "value": "example-value-123"


                "list-value-2":
                "object-value":
                "value": "example-value-123"



                # please note this creates a backup file: file.json.backup
                $ sed -i.backup '/list-value/N;N;s/example-value-123/example-value-changed-567/' file.json

                $ cat ./file.json
                "list-value":
                "object-value":
                "value": "example-value-changed-567"


                "list-value-2":
                "object-value":
                "value": "example-value-123"







                share|improve this answer



























                  1














                  I think you're looking for this as a sed command:



                  sed -i.backup '/list-value/N;N;s/example-value-123/example-value-changed-567/' file.json


                  Example:



                  $ cat ./file.json
                  "list-value":
                  "object-value":
                  "value": "example-value-123"


                  "list-value-2":
                  "object-value":
                  "value": "example-value-123"



                  # please note this creates a backup file: file.json.backup
                  $ sed -i.backup '/list-value/N;N;s/example-value-123/example-value-changed-567/' file.json

                  $ cat ./file.json
                  "list-value":
                  "object-value":
                  "value": "example-value-changed-567"


                  "list-value-2":
                  "object-value":
                  "value": "example-value-123"







                  share|improve this answer

























                    1












                    1








                    1







                    I think you're looking for this as a sed command:



                    sed -i.backup '/list-value/N;N;s/example-value-123/example-value-changed-567/' file.json


                    Example:



                    $ cat ./file.json
                    "list-value":
                    "object-value":
                    "value": "example-value-123"


                    "list-value-2":
                    "object-value":
                    "value": "example-value-123"



                    # please note this creates a backup file: file.json.backup
                    $ sed -i.backup '/list-value/N;N;s/example-value-123/example-value-changed-567/' file.json

                    $ cat ./file.json
                    "list-value":
                    "object-value":
                    "value": "example-value-changed-567"


                    "list-value-2":
                    "object-value":
                    "value": "example-value-123"







                    share|improve this answer













                    I think you're looking for this as a sed command:



                    sed -i.backup '/list-value/N;N;s/example-value-123/example-value-changed-567/' file.json


                    Example:



                    $ cat ./file.json
                    "list-value":
                    "object-value":
                    "value": "example-value-123"


                    "list-value-2":
                    "object-value":
                    "value": "example-value-123"



                    # please note this creates a backup file: file.json.backup
                    $ sed -i.backup '/list-value/N;N;s/example-value-123/example-value-changed-567/' file.json

                    $ cat ./file.json
                    "list-value":
                    "object-value":
                    "value": "example-value-changed-567"


                    "list-value-2":
                    "object-value":
                    "value": "example-value-123"








                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 11 at 10:14









                    rowanrowan

                    1607




                    1607





















                        0














                        Nested structures, namely JSON, must be processed by structure aware (here JSON) routines. Whenever you attempt processing it via (multi) line-aware only routines, you'll be always exposed to false-positives.



                        Thus, your ask (again, asusminig it's properly formed JSON) is easily achievable via jtc:



                        bash $ jtc -w'[list-value]<value>l' -u'"example-value-changed-567"' file.json

                        "list-value":
                        "object-value":
                        "value": "example-value-changed-567"



                        bash $





                        share|improve this answer





























                          0














                          Nested structures, namely JSON, must be processed by structure aware (here JSON) routines. Whenever you attempt processing it via (multi) line-aware only routines, you'll be always exposed to false-positives.



                          Thus, your ask (again, asusminig it's properly formed JSON) is easily achievable via jtc:



                          bash $ jtc -w'[list-value]<value>l' -u'"example-value-changed-567"' file.json

                          "list-value":
                          "object-value":
                          "value": "example-value-changed-567"



                          bash $





                          share|improve this answer



























                            0












                            0








                            0







                            Nested structures, namely JSON, must be processed by structure aware (here JSON) routines. Whenever you attempt processing it via (multi) line-aware only routines, you'll be always exposed to false-positives.



                            Thus, your ask (again, asusminig it's properly formed JSON) is easily achievable via jtc:



                            bash $ jtc -w'[list-value]<value>l' -u'"example-value-changed-567"' file.json

                            "list-value":
                            "object-value":
                            "value": "example-value-changed-567"



                            bash $





                            share|improve this answer















                            Nested structures, namely JSON, must be processed by structure aware (here JSON) routines. Whenever you attempt processing it via (multi) line-aware only routines, you'll be always exposed to false-positives.



                            Thus, your ask (again, asusminig it's properly formed JSON) is easily achievable via jtc:



                            bash $ jtc -w'[list-value]<value>l' -u'"example-value-changed-567"' file.json

                            "list-value":
                            "object-value":
                            "value": "example-value-changed-567"



                            bash $






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Jan 11 at 18:44

























                            answered Jan 11 at 17:27









                            Dmitry L.Dmitry L.

                            112




                            112



























                                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%2f493907%2flinux-sed-command-replace-two-lines-having-spaces-and-special-characters-using-r%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?

                                Bahrain

                                Postfix configuration issue with fips on centos 7; mailgun relay