How to extract data after a field in a file

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











up vote
0
down vote

favorite












I have following text in a file



 "rules": [
"/Common/Tetration_TCP_L4_ipfix",
"/Common/_sys_https_redirect"
],
"rulesReference": [

"link": "https://localhost/mgmt/tm/ltm/rule/~Common~Tetration_TCP_L4_ipfix?ver=12.0.0"



want to extract anything inside "rules":
currently it is



/Common/Tetration_TCP_L4_ipfix
/Common/_sys_https_redirect


Output should look like



Tetration_TCP_L4_ipfix
_sys_https_redirect


but again this can be anything.







share|improve this question

























    up vote
    0
    down vote

    favorite












    I have following text in a file



     "rules": [
    "/Common/Tetration_TCP_L4_ipfix",
    "/Common/_sys_https_redirect"
    ],
    "rulesReference": [

    "link": "https://localhost/mgmt/tm/ltm/rule/~Common~Tetration_TCP_L4_ipfix?ver=12.0.0"



    want to extract anything inside "rules":
    currently it is



    /Common/Tetration_TCP_L4_ipfix
    /Common/_sys_https_redirect


    Output should look like



    Tetration_TCP_L4_ipfix
    _sys_https_redirect


    but again this can be anything.







    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have following text in a file



       "rules": [
      "/Common/Tetration_TCP_L4_ipfix",
      "/Common/_sys_https_redirect"
      ],
      "rulesReference": [

      "link": "https://localhost/mgmt/tm/ltm/rule/~Common~Tetration_TCP_L4_ipfix?ver=12.0.0"



      want to extract anything inside "rules":
      currently it is



      /Common/Tetration_TCP_L4_ipfix
      /Common/_sys_https_redirect


      Output should look like



      Tetration_TCP_L4_ipfix
      _sys_https_redirect


      but again this can be anything.







      share|improve this question













      I have following text in a file



       "rules": [
      "/Common/Tetration_TCP_L4_ipfix",
      "/Common/_sys_https_redirect"
      ],
      "rulesReference": [

      "link": "https://localhost/mgmt/tm/ltm/rule/~Common~Tetration_TCP_L4_ipfix?ver=12.0.0"



      want to extract anything inside "rules":
      currently it is



      /Common/Tetration_TCP_L4_ipfix
      /Common/_sys_https_redirect


      Output should look like



      Tetration_TCP_L4_ipfix
      _sys_https_redirect


      but again this can be anything.









      share|improve this question












      share|improve this question




      share|improve this question








      edited May 30 at 7:40









      Kusalananda

      102k13199313




      102k13199313









      asked May 30 at 6:13









      Sanjay Shitole

      6




      6




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          4
          down vote













          If your input file contains non full-fledged JSON but only inner fragment (though structurally valid) you may recover the missing "outer" parts to get a full valid JSON structure.



          sed + jq solution:



          jq -r '.rules | sub("/Common/"; "")' <(sed '1 s/^//; $ s/$/]/' file)


          The output:



          Tetration_TCP_L4_ipfix
          _sys_https_redirect



          Of course, if in real you have a valid JSON - just skip the sed processing and just use:



          jq -r '.rules | sub("/Common/"; "")' file





          share|improve this answer




























            up vote
            3
            down vote













            If this is a well-formed JSON file:



            $ jq -r '..|select(type=="object" and has("rules")).rules|map(split("/")|.[-1])|.' file.json
            Tetration_TCP_L4_ipfix
            _sys_https_redirect


            This will use jq to recursively find all the JSON objects that has a rules key. For all the array values of those keys, it will split the value on / and return the last component of it.






            share|improve this answer




























              up vote
              3
              down vote













              Using AWK:



              awk -F '[/"]' '/"rules": /,/],/if(/"rules": ' file.txt





              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',
                convertImagesToLinks: false,
                noModals: false,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: null,
                bindNavPrevention: true,
                postfix: "",
                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%2f446832%2fhow-to-extract-data-after-a-field-in-a-file%23new-answer', 'question_page');

                );

                Post as a guest






























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                4
                down vote













                If your input file contains non full-fledged JSON but only inner fragment (though structurally valid) you may recover the missing "outer" parts to get a full valid JSON structure.



                sed + jq solution:



                jq -r '.rules | sub("/Common/"; "")' <(sed '1 s/^//; $ s/$/]/' file)


                The output:



                Tetration_TCP_L4_ipfix
                _sys_https_redirect



                Of course, if in real you have a valid JSON - just skip the sed processing and just use:



                jq -r '.rules | sub("/Common/"; "")' file





                share|improve this answer

























                  up vote
                  4
                  down vote













                  If your input file contains non full-fledged JSON but only inner fragment (though structurally valid) you may recover the missing "outer" parts to get a full valid JSON structure.



                  sed + jq solution:



                  jq -r '.rules | sub("/Common/"; "")' <(sed '1 s/^//; $ s/$/]/' file)


                  The output:



                  Tetration_TCP_L4_ipfix
                  _sys_https_redirect



                  Of course, if in real you have a valid JSON - just skip the sed processing and just use:



                  jq -r '.rules | sub("/Common/"; "")' file





                  share|improve this answer























                    up vote
                    4
                    down vote










                    up vote
                    4
                    down vote









                    If your input file contains non full-fledged JSON but only inner fragment (though structurally valid) you may recover the missing "outer" parts to get a full valid JSON structure.



                    sed + jq solution:



                    jq -r '.rules | sub("/Common/"; "")' <(sed '1 s/^//; $ s/$/]/' file)


                    The output:



                    Tetration_TCP_L4_ipfix
                    _sys_https_redirect



                    Of course, if in real you have a valid JSON - just skip the sed processing and just use:



                    jq -r '.rules | sub("/Common/"; "")' file





                    share|improve this answer













                    If your input file contains non full-fledged JSON but only inner fragment (though structurally valid) you may recover the missing "outer" parts to get a full valid JSON structure.



                    sed + jq solution:



                    jq -r '.rules | sub("/Common/"; "")' <(sed '1 s/^//; $ s/$/]/' file)


                    The output:



                    Tetration_TCP_L4_ipfix
                    _sys_https_redirect



                    Of course, if in real you have a valid JSON - just skip the sed processing and just use:



                    jq -r '.rules | sub("/Common/"; "")' file






                    share|improve this answer













                    share|improve this answer



                    share|improve this answer











                    answered May 30 at 7:31









                    RomanPerekhrest

                    22.4k12144




                    22.4k12144






















                        up vote
                        3
                        down vote













                        If this is a well-formed JSON file:



                        $ jq -r '..|select(type=="object" and has("rules")).rules|map(split("/")|.[-1])|.' file.json
                        Tetration_TCP_L4_ipfix
                        _sys_https_redirect


                        This will use jq to recursively find all the JSON objects that has a rules key. For all the array values of those keys, it will split the value on / and return the last component of it.






                        share|improve this answer

























                          up vote
                          3
                          down vote













                          If this is a well-formed JSON file:



                          $ jq -r '..|select(type=="object" and has("rules")).rules|map(split("/")|.[-1])|.' file.json
                          Tetration_TCP_L4_ipfix
                          _sys_https_redirect


                          This will use jq to recursively find all the JSON objects that has a rules key. For all the array values of those keys, it will split the value on / and return the last component of it.






                          share|improve this answer























                            up vote
                            3
                            down vote










                            up vote
                            3
                            down vote









                            If this is a well-formed JSON file:



                            $ jq -r '..|select(type=="object" and has("rules")).rules|map(split("/")|.[-1])|.' file.json
                            Tetration_TCP_L4_ipfix
                            _sys_https_redirect


                            This will use jq to recursively find all the JSON objects that has a rules key. For all the array values of those keys, it will split the value on / and return the last component of it.






                            share|improve this answer













                            If this is a well-formed JSON file:



                            $ jq -r '..|select(type=="object" and has("rules")).rules|map(split("/")|.[-1])|.' file.json
                            Tetration_TCP_L4_ipfix
                            _sys_https_redirect


                            This will use jq to recursively find all the JSON objects that has a rules key. For all the array values of those keys, it will split the value on / and return the last component of it.







                            share|improve this answer













                            share|improve this answer



                            share|improve this answer











                            answered May 30 at 6:31









                            Kusalananda

                            102k13199313




                            102k13199313




















                                up vote
                                3
                                down vote













                                Using AWK:



                                awk -F '[/"]' '/"rules": /,/],/if(/"rules": ' file.txt





                                share|improve this answer



























                                  up vote
                                  3
                                  down vote













                                  Using AWK:



                                  awk -F '[/"]' '/"rules": /,/],/if(/"rules": ' file.txt





                                  share|improve this answer

























                                    up vote
                                    3
                                    down vote










                                    up vote
                                    3
                                    down vote









                                    Using AWK:



                                    awk -F '[/"]' '/"rules": /,/],/if(/"rules": ' file.txt





                                    share|improve this answer















                                    Using AWK:



                                    awk -F '[/"]' '/"rules": /,/],/if(/"rules": ' file.txt






                                    share|improve this answer















                                    share|improve this answer



                                    share|improve this answer








                                    edited May 30 at 7:15


























                                    answered May 30 at 6:34









                                    SivaPrasath

                                    4,54212243




                                    4,54212243






















                                         

                                        draft saved


                                        draft discarded


























                                         


                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f446832%2fhow-to-extract-data-after-a-field-in-a-file%23new-answer', 'question_page');

                                        );

                                        Post as a guest













































































                                        Popular posts from this blog

                                        Peggy Mitchell

                                        Palaiologos

                                        The Forum (Inglewood, California)