sed pattern replace “ to ” and to \ except json string





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







1















I've got problem to replace " to " and to \ and except " of json



test.txt input file



"a"     "b"     
{"1":"female","2":"197312","3":"359","4":"201109","5":"mail"}
uff08u524du5bfeu5fdc


I want to output like



"a"     "b"     
{"1":"female","2":"197312","3":"359","4":"201109","5":"mail"}
\uff08\u524d\u5bfe\u5fdc









share|improve this question









New contributor




JOIN MAX is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1





    How complex is your file? Will the JSON string always be in a single line? Will the JSON string never contain { or }? Will there be multiple lines of data? Will the JSON data always be the only line(s) in the file that start with a {?

    – terdon
    1 hour ago


















1















I've got problem to replace " to " and to \ and except " of json



test.txt input file



"a"     "b"     
{"1":"female","2":"197312","3":"359","4":"201109","5":"mail"}
uff08u524du5bfeu5fdc


I want to output like



"a"     "b"     
{"1":"female","2":"197312","3":"359","4":"201109","5":"mail"}
\uff08\u524d\u5bfe\u5fdc









share|improve this question









New contributor




JOIN MAX is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1





    How complex is your file? Will the JSON string always be in a single line? Will the JSON string never contain { or }? Will there be multiple lines of data? Will the JSON data always be the only line(s) in the file that start with a {?

    – terdon
    1 hour ago














1












1








1








I've got problem to replace " to " and to \ and except " of json



test.txt input file



"a"     "b"     
{"1":"female","2":"197312","3":"359","4":"201109","5":"mail"}
uff08u524du5bfeu5fdc


I want to output like



"a"     "b"     
{"1":"female","2":"197312","3":"359","4":"201109","5":"mail"}
\uff08\u524d\u5bfe\u5fdc









share|improve this question









New contributor




JOIN MAX is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












I've got problem to replace " to " and to \ and except " of json



test.txt input file



"a"     "b"     
{"1":"female","2":"197312","3":"359","4":"201109","5":"mail"}
uff08u524du5bfeu5fdc


I want to output like



"a"     "b"     
{"1":"female","2":"197312","3":"359","4":"201109","5":"mail"}
\uff08\u524d\u5bfe\u5fdc






text-processing sed






share|improve this question









New contributor




JOIN MAX is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




JOIN MAX is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 3 hours ago









fra-san

2,0271721




2,0271721






New contributor




JOIN MAX is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 3 hours ago









JOIN MAXJOIN MAX

61




61




New contributor




JOIN MAX is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





JOIN MAX is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






JOIN MAX is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 1





    How complex is your file? Will the JSON string always be in a single line? Will the JSON string never contain { or }? Will there be multiple lines of data? Will the JSON data always be the only line(s) in the file that start with a {?

    – terdon
    1 hour ago














  • 1





    How complex is your file? Will the JSON string always be in a single line? Will the JSON string never contain { or }? Will there be multiple lines of data? Will the JSON data always be the only line(s) in the file that start with a {?

    – terdon
    1 hour ago








1




1





How complex is your file? Will the JSON string always be in a single line? Will the JSON string never contain { or }? Will there be multiple lines of data? Will the JSON data always be the only line(s) in the file that start with a {?

– terdon
1 hour ago





How complex is your file? Will the JSON string always be in a single line? Will the JSON string never contain { or }? Will there be multiple lines of data? Will the JSON data always be the only line(s) in the file that start with a {?

– terdon
1 hour ago










3 Answers
3






active

oldest

votes


















2














To be more robust, you could do a full json parsing:



perl -0777 -pe '
s@(".*?"|\)|({(?:"(?:\.|[^"])*+"|(?2)|[^"{}]++)*+})|[^{}\"]+@
$1 ? $1 =~ s/["\]/\$&/gr : $&@gse'


Which on an input like



"a"     "b"     "c{d"
{"1":"female","2":"197312","3":"359","4":"201109","5":"mail"}
{
"1": {"x": "y"}
"2": "}}}"
"3": ["{"x", "}"]
}
uff08u524du5bfeu5fdc


gives



"a"     "b"     "c{d"
{"1":"female","2":"197312","3":"359","4":"201109","5":"mail"}
{
"1": {"x": "y"}
"2": "}}}"
"3": ["{"x", "}"]
}
\uff08\u524d\u5bfe\u5fdc


You may want to clarify what you want to do if the input contains "foo"bar" or "foonbar" outside of json objects.






share|improve this answer































    2














    In the simple example you show, this is easy. Just escape the characters only on lines that don't start with a {:



    $ sed -E '/^[^{]/s|(["])|\1|g' file 
    "a" "b"
    {"1":"female","2":"197312","3":"359","4":"201109","5":"mail"}
    \uff08\u524d\u5bfe\u5fdc


    However, things get considerably more complicated if your JSON can span several lines. For such cases, you can write a little script that counts the number of opening { and closing } and only applies the replacement while those numbers are equal (so when we aren't in a JSON string). Something like:



    perl -F'' -ne 'for (@F){$op++ if /{/; $cl++ if /}/; if($cl==$op){s|["\]|\$&|g;}print}' file 


    However, this will also break if the JSON string itself can contain { or } which don't signify a JSON section (e.g. {"1":"b-{c}"} or whatever). For such cases, use Stéphane's approach instead.






    share|improve this answer

































      0














      $ sed 's/(\|^"|"$|"[ t])/\1/g' test.txt | sed 's/([ t])"/1\"/g'



      • Replace or " at start of line or end of line or before space/tab character:
        's/(\|^"|"$|"[ t])/\1/g'

      • or " after space/tab character: sed 's/([ t])"/1\"/g'


      So this only works if your json string doesn't contain any space/tab characters and is on one line.






      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
        });


        }
        });






        JOIN MAX is a new contributor. Be nice, and check out our Code of Conduct.










        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f511415%2fsed-pattern-replace-to-and-to-except-json-string%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














        To be more robust, you could do a full json parsing:



        perl -0777 -pe '
        s@(".*?"|\)|({(?:"(?:\.|[^"])*+"|(?2)|[^"{}]++)*+})|[^{}\"]+@
        $1 ? $1 =~ s/["\]/\$&/gr : $&@gse'


        Which on an input like



        "a"     "b"     "c{d"
        {"1":"female","2":"197312","3":"359","4":"201109","5":"mail"}
        {
        "1": {"x": "y"}
        "2": "}}}"
        "3": ["{"x", "}"]
        }
        uff08u524du5bfeu5fdc


        gives



        "a"     "b"     "c{d"
        {"1":"female","2":"197312","3":"359","4":"201109","5":"mail"}
        {
        "1": {"x": "y"}
        "2": "}}}"
        "3": ["{"x", "}"]
        }
        \uff08\u524d\u5bfe\u5fdc


        You may want to clarify what you want to do if the input contains "foo"bar" or "foonbar" outside of json objects.






        share|improve this answer




























          2














          To be more robust, you could do a full json parsing:



          perl -0777 -pe '
          s@(".*?"|\)|({(?:"(?:\.|[^"])*+"|(?2)|[^"{}]++)*+})|[^{}\"]+@
          $1 ? $1 =~ s/["\]/\$&/gr : $&@gse'


          Which on an input like



          "a"     "b"     "c{d"
          {"1":"female","2":"197312","3":"359","4":"201109","5":"mail"}
          {
          "1": {"x": "y"}
          "2": "}}}"
          "3": ["{"x", "}"]
          }
          uff08u524du5bfeu5fdc


          gives



          "a"     "b"     "c{d"
          {"1":"female","2":"197312","3":"359","4":"201109","5":"mail"}
          {
          "1": {"x": "y"}
          "2": "}}}"
          "3": ["{"x", "}"]
          }
          \uff08\u524d\u5bfe\u5fdc


          You may want to clarify what you want to do if the input contains "foo"bar" or "foonbar" outside of json objects.






          share|improve this answer


























            2












            2








            2







            To be more robust, you could do a full json parsing:



            perl -0777 -pe '
            s@(".*?"|\)|({(?:"(?:\.|[^"])*+"|(?2)|[^"{}]++)*+})|[^{}\"]+@
            $1 ? $1 =~ s/["\]/\$&/gr : $&@gse'


            Which on an input like



            "a"     "b"     "c{d"
            {"1":"female","2":"197312","3":"359","4":"201109","5":"mail"}
            {
            "1": {"x": "y"}
            "2": "}}}"
            "3": ["{"x", "}"]
            }
            uff08u524du5bfeu5fdc


            gives



            "a"     "b"     "c{d"
            {"1":"female","2":"197312","3":"359","4":"201109","5":"mail"}
            {
            "1": {"x": "y"}
            "2": "}}}"
            "3": ["{"x", "}"]
            }
            \uff08\u524d\u5bfe\u5fdc


            You may want to clarify what you want to do if the input contains "foo"bar" or "foonbar" outside of json objects.






            share|improve this answer













            To be more robust, you could do a full json parsing:



            perl -0777 -pe '
            s@(".*?"|\)|({(?:"(?:\.|[^"])*+"|(?2)|[^"{}]++)*+})|[^{}\"]+@
            $1 ? $1 =~ s/["\]/\$&/gr : $&@gse'


            Which on an input like



            "a"     "b"     "c{d"
            {"1":"female","2":"197312","3":"359","4":"201109","5":"mail"}
            {
            "1": {"x": "y"}
            "2": "}}}"
            "3": ["{"x", "}"]
            }
            uff08u524du5bfeu5fdc


            gives



            "a"     "b"     "c{d"
            {"1":"female","2":"197312","3":"359","4":"201109","5":"mail"}
            {
            "1": {"x": "y"}
            "2": "}}}"
            "3": ["{"x", "}"]
            }
            \uff08\u524d\u5bfe\u5fdc


            You may want to clarify what you want to do if the input contains "foo"bar" or "foonbar" outside of json objects.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 1 hour ago









            Stéphane ChazelasStéphane Chazelas

            313k57593950




            313k57593950

























                2














                In the simple example you show, this is easy. Just escape the characters only on lines that don't start with a {:



                $ sed -E '/^[^{]/s|(["])|\1|g' file 
                "a" "b"
                {"1":"female","2":"197312","3":"359","4":"201109","5":"mail"}
                \uff08\u524d\u5bfe\u5fdc


                However, things get considerably more complicated if your JSON can span several lines. For such cases, you can write a little script that counts the number of opening { and closing } and only applies the replacement while those numbers are equal (so when we aren't in a JSON string). Something like:



                perl -F'' -ne 'for (@F){$op++ if /{/; $cl++ if /}/; if($cl==$op){s|["\]|\$&|g;}print}' file 


                However, this will also break if the JSON string itself can contain { or } which don't signify a JSON section (e.g. {"1":"b-{c}"} or whatever). For such cases, use Stéphane's approach instead.






                share|improve this answer






























                  2














                  In the simple example you show, this is easy. Just escape the characters only on lines that don't start with a {:



                  $ sed -E '/^[^{]/s|(["])|\1|g' file 
                  "a" "b"
                  {"1":"female","2":"197312","3":"359","4":"201109","5":"mail"}
                  \uff08\u524d\u5bfe\u5fdc


                  However, things get considerably more complicated if your JSON can span several lines. For such cases, you can write a little script that counts the number of opening { and closing } and only applies the replacement while those numbers are equal (so when we aren't in a JSON string). Something like:



                  perl -F'' -ne 'for (@F){$op++ if /{/; $cl++ if /}/; if($cl==$op){s|["\]|\$&|g;}print}' file 


                  However, this will also break if the JSON string itself can contain { or } which don't signify a JSON section (e.g. {"1":"b-{c}"} or whatever). For such cases, use Stéphane's approach instead.






                  share|improve this answer




























                    2












                    2








                    2







                    In the simple example you show, this is easy. Just escape the characters only on lines that don't start with a {:



                    $ sed -E '/^[^{]/s|(["])|\1|g' file 
                    "a" "b"
                    {"1":"female","2":"197312","3":"359","4":"201109","5":"mail"}
                    \uff08\u524d\u5bfe\u5fdc


                    However, things get considerably more complicated if your JSON can span several lines. For such cases, you can write a little script that counts the number of opening { and closing } and only applies the replacement while those numbers are equal (so when we aren't in a JSON string). Something like:



                    perl -F'' -ne 'for (@F){$op++ if /{/; $cl++ if /}/; if($cl==$op){s|["\]|\$&|g;}print}' file 


                    However, this will also break if the JSON string itself can contain { or } which don't signify a JSON section (e.g. {"1":"b-{c}"} or whatever). For such cases, use Stéphane's approach instead.






                    share|improve this answer















                    In the simple example you show, this is easy. Just escape the characters only on lines that don't start with a {:



                    $ sed -E '/^[^{]/s|(["])|\1|g' file 
                    "a" "b"
                    {"1":"female","2":"197312","3":"359","4":"201109","5":"mail"}
                    \uff08\u524d\u5bfe\u5fdc


                    However, things get considerably more complicated if your JSON can span several lines. For such cases, you can write a little script that counts the number of opening { and closing } and only applies the replacement while those numbers are equal (so when we aren't in a JSON string). Something like:



                    perl -F'' -ne 'for (@F){$op++ if /{/; $cl++ if /}/; if($cl==$op){s|["\]|\$&|g;}print}' file 


                    However, this will also break if the JSON string itself can contain { or } which don't signify a JSON section (e.g. {"1":"b-{c}"} or whatever). For such cases, use Stéphane's approach instead.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited 41 mins ago

























                    answered 1 hour ago









                    terdonterdon

                    134k33269449




                    134k33269449























                        0














                        $ sed 's/(\|^"|"$|"[ t])/\1/g' test.txt | sed 's/([ t])"/1\"/g'



                        • Replace or " at start of line or end of line or before space/tab character:
                          's/(\|^"|"$|"[ t])/\1/g'

                        • or " after space/tab character: sed 's/([ t])"/1\"/g'


                        So this only works if your json string doesn't contain any space/tab characters and is on one line.






                        share|improve this answer






























                          0














                          $ sed 's/(\|^"|"$|"[ t])/\1/g' test.txt | sed 's/([ t])"/1\"/g'



                          • Replace or " at start of line or end of line or before space/tab character:
                            's/(\|^"|"$|"[ t])/\1/g'

                          • or " after space/tab character: sed 's/([ t])"/1\"/g'


                          So this only works if your json string doesn't contain any space/tab characters and is on one line.






                          share|improve this answer




























                            0












                            0








                            0







                            $ sed 's/(\|^"|"$|"[ t])/\1/g' test.txt | sed 's/([ t])"/1\"/g'



                            • Replace or " at start of line or end of line or before space/tab character:
                              's/(\|^"|"$|"[ t])/\1/g'

                            • or " after space/tab character: sed 's/([ t])"/1\"/g'


                            So this only works if your json string doesn't contain any space/tab characters and is on one line.






                            share|improve this answer















                            $ sed 's/(\|^"|"$|"[ t])/\1/g' test.txt | sed 's/([ t])"/1\"/g'



                            • Replace or " at start of line or end of line or before space/tab character:
                              's/(\|^"|"$|"[ t])/\1/g'

                            • or " after space/tab character: sed 's/([ t])"/1\"/g'


                            So this only works if your json string doesn't contain any space/tab characters and is on one line.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited 2 hours ago

























                            answered 2 hours ago









                            FreddyFreddy

                            1,559210




                            1,559210






















                                JOIN MAX is a new contributor. Be nice, and check out our Code of Conduct.










                                draft saved

                                draft discarded


















                                JOIN MAX is a new contributor. Be nice, and check out our Code of Conduct.













                                JOIN MAX is a new contributor. Be nice, and check out our Code of Conduct.












                                JOIN MAX is a new contributor. Be nice, and check out our Code of Conduct.
















                                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%2f511415%2fsed-pattern-replace-to-and-to-except-json-string%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

                                الفوسفات في المغرب

                                Four equal circles intersect: What is the area of the small shaded portion and its height

                                جامعة ليفربول