How to extract specific values/fields from the text file?

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












4















How to extract the following values/fields from the text file in Linux system:



EventCorrelationId="615-493|-1899671563||1550927718000"
CreationTime="20190225094504"
SubscriberNumber=92705073362


Text file sample data is mentioned below:



2019-02-25 09:45:04.427 FAIL RETRY: Failed for request id: 11235993 Cause: userNotReachable Info: <undef> Code: 27,USSD RequestId=11235993 OriginalId=11235993 EventCorrelationId="615-493|-1899671563||1550927718000" CreationTime="20190225094504" ResendCount=0 Timestamp=1551071704342 (Mon Feb 25 09:45:04 AFT 2019) State=STATE_SENT SubscriberNumber=92705073362 UssdText=Last event was charged 687.95 MB from 3GB Monthly, Main Account 6.00 PKR, Remaining data 2,388.75 MB (Exp 25.03.2019), Main Account 7.62 PKR1500 PKR = 32GB valid 30 Days, Dial *477*32*1#. NumberingPlan=1 Nadi=4 UssdFormat=2 









share|improve this question




























    4















    How to extract the following values/fields from the text file in Linux system:



    EventCorrelationId="615-493|-1899671563||1550927718000"
    CreationTime="20190225094504"
    SubscriberNumber=92705073362


    Text file sample data is mentioned below:



    2019-02-25 09:45:04.427 FAIL RETRY: Failed for request id: 11235993 Cause: userNotReachable Info: <undef> Code: 27,USSD RequestId=11235993 OriginalId=11235993 EventCorrelationId="615-493|-1899671563||1550927718000" CreationTime="20190225094504" ResendCount=0 Timestamp=1551071704342 (Mon Feb 25 09:45:04 AFT 2019) State=STATE_SENT SubscriberNumber=92705073362 UssdText=Last event was charged 687.95 MB from 3GB Monthly, Main Account 6.00 PKR, Remaining data 2,388.75 MB (Exp 25.03.2019), Main Account 7.62 PKR1500 PKR = 32GB valid 30 Days, Dial *477*32*1#. NumberingPlan=1 Nadi=4 UssdFormat=2 









    share|improve this question


























      4












      4








      4








      How to extract the following values/fields from the text file in Linux system:



      EventCorrelationId="615-493|-1899671563||1550927718000"
      CreationTime="20190225094504"
      SubscriberNumber=92705073362


      Text file sample data is mentioned below:



      2019-02-25 09:45:04.427 FAIL RETRY: Failed for request id: 11235993 Cause: userNotReachable Info: <undef> Code: 27,USSD RequestId=11235993 OriginalId=11235993 EventCorrelationId="615-493|-1899671563||1550927718000" CreationTime="20190225094504" ResendCount=0 Timestamp=1551071704342 (Mon Feb 25 09:45:04 AFT 2019) State=STATE_SENT SubscriberNumber=92705073362 UssdText=Last event was charged 687.95 MB from 3GB Monthly, Main Account 6.00 PKR, Remaining data 2,388.75 MB (Exp 25.03.2019), Main Account 7.62 PKR1500 PKR = 32GB valid 30 Days, Dial *477*32*1#. NumberingPlan=1 Nadi=4 UssdFormat=2 









      share|improve this question
















      How to extract the following values/fields from the text file in Linux system:



      EventCorrelationId="615-493|-1899671563||1550927718000"
      CreationTime="20190225094504"
      SubscriberNumber=92705073362


      Text file sample data is mentioned below:



      2019-02-25 09:45:04.427 FAIL RETRY: Failed for request id: 11235993 Cause: userNotReachable Info: <undef> Code: 27,USSD RequestId=11235993 OriginalId=11235993 EventCorrelationId="615-493|-1899671563||1550927718000" CreationTime="20190225094504" ResendCount=0 Timestamp=1551071704342 (Mon Feb 25 09:45:04 AFT 2019) State=STATE_SENT SubscriberNumber=92705073362 UssdText=Last event was charged 687.95 MB from 3GB Monthly, Main Account 6.00 PKR, Remaining data 2,388.75 MB (Exp 25.03.2019), Main Account 7.62 PKR1500 PKR = 32GB valid 30 Days, Dial *477*32*1#. NumberingPlan=1 Nadi=4 UssdFormat=2 






      text-processing






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 25 at 12:59









      terdon

      132k32262441




      132k32262441










      asked Feb 25 at 6:02









      Jack AndersonJack Anderson

      192




      192




















          4 Answers
          4






          active

          oldest

          votes


















          3














          Using grep



          $ grep -oE '(EventCorrelationId|CreationTime|SubscriberNumber)[^ ]*' textfile
          EventCorrelationId="615-493|-1899671563||1550927718000"
          CreationTime="20190225094504"
          SubscriberNumber=92705073362


          Using awk



          Try:



          $ awk -v RS=' ' '/^EventCorrelationId=/ || /^CreationTime=/ || /^SubscriberNumber=/' textfile
          EventCorrelationId="615-493|-1899671563||1550927718000"
          CreationTime="20190225094504"
          SubscriberNumber=92705073362


          How it works




          • -v RS=' '



            This tells awk to use a blank as the record separator.




          • /^EventCorrelationId=/ || /^CreationTime=/ || /^SubscriberNumber=/



            This tells awk to print the record if it matches any of these three regular expressions. Some notes:



            • In a regex, ^ means beginning-of-the-record. Thus /^CreationTime=/ means a record that starts with CreationTime=


            • In awk, like many languages, || means logical-or. Thus ^EventCorrelationId=/ || /^CreationTime=/ is true if either regex is matched.



          Using sed



          $ sed -En 's/.*(EventCorrelationId=[^ ]*).*(CreationTime=[^ ]*).*(SubscriberNumber=[^ ]*).*/1n2n3/p' textfile
          EventCorrelationId="615-493|-1899671563||1550927718000"
          CreationTime="20190225094504"
          SubscriberNumber=92705073362


          Using an alternate separator in the output



          To use | as the output separator in place of newline:



          $ sed -En 's/.*(EventCorrelationId=[^ ]*).*(CreationTime=[^ ]*).*(SubscriberNumber=[^ ]*).*/1|2|3/p' textfile
          EventCorrelationId="615-493|-1899671563||1550927718000"|CreationTime="20190225094504"|SubscriberNumber=92705073362





          share|improve this answer
































            2














            $ grep -Eo '(EventCorrelationId|CreationTime|SubscriberNumber)=[^ ]+' file
            EventCorrelationId="615-493|-1899671563||1550927718000"
            CreationTime="20190225094504"
            SubscriberNumber=92705073362



            • grep -Eo grep in extended regular expression mode (less escaping of special chars) and print only the matched parts


            • (EventCorrelationId|CreationTime|SubscriberNumber) match EventCorrelationId or CreationTime or SubscriberNumber


            • =[^ ]+ followed by "=", followed by non-space-character one or more times

            Edit1:



            Now separated by "|":



            $ echo $(grep -Eo '(EventCorrelationId|CreationTime|SubscriberNumber)=[^ ]+' file) | tr ' ' '|'
            EventCorrelationId="615-493|-1899671563||1550927718000"|CreationTime="20190225094504"|SubscriberNumber=92705073362


            Edit2:



            Now separated by "|" in reversed order using tac:



            $ echo $(grep -Eo '(EventCorrelationId|CreationTime|SubscriberNumber)=[^ ]+' file | tac) | tr ' ' '|'
            SubscriberNumber=92705073362|CreationTime="20190225094504"|EventCorrelationId="615-493|-1899671563||1550927718000"





            share|improve this answer

























            • hanks for your kind support, using your (Using sed) command, I was able to extract the information. However, kindly to please suggest how to extract the mentioned values in one single line separated by "|" sign as shown below? SubscriberNumber=92705073362|CreationTime="20190225094504"|EventCorrelationId="615-493|-1899671563||1550927718000"

              – Jack Anderson
              Feb 26 at 4:41











            • Example added. But I didn't use sed :)

              – Freddy
              Feb 26 at 4:56


















            0














            I Tried with below awk method and it worked fine



            j=`awk 'print NF' filename `
            for ((i=1;i<=$j;i++)); do awk -v i="$i" '$i ~ /EventCorrelationId/||$i ~ /CreationTime/||$i ~ /SubscriberNumber/print $i' filename ; done


            output



            EventCorrelationId="615-493|-1899671563||1550927718000"
            CreationTime="20190225094504"
            SubscriberNumber=9270507336





            share|improve this answer






























              0














              You can perform this exercise bu building up your regex dynamically, based on the fields you want outputted:



              $ perl -lne '
              $re = join "|", map +quotemeta qw/EventCorrelationId CreationTime SubscriberNumber/;
              print join "|", /(?:$re)=H+/g;
              ' input.file


              Output:



              EventCorrelationId="615-493|-1899671563||1550927718000"|CreationTime="20190225094504"|SubscriberNumber=92705073362


              Working:



              • The fields that you want output are placed in an anonymous array qw/.../.

              • Then we transform each of them using map ... by joining with logical OR |.

              • In the last step, we apply the regex just constructed on the current record, $_, and the fields fished out are joined via the pipe symbol | to get the output.





              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%2f502782%2fhow-to-extract-specific-values-fields-from-the-text-file%23new-answer', 'question_page');

                );

                Post as a guest















                Required, but never shown

























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                3














                Using grep



                $ grep -oE '(EventCorrelationId|CreationTime|SubscriberNumber)[^ ]*' textfile
                EventCorrelationId="615-493|-1899671563||1550927718000"
                CreationTime="20190225094504"
                SubscriberNumber=92705073362


                Using awk



                Try:



                $ awk -v RS=' ' '/^EventCorrelationId=/ || /^CreationTime=/ || /^SubscriberNumber=/' textfile
                EventCorrelationId="615-493|-1899671563||1550927718000"
                CreationTime="20190225094504"
                SubscriberNumber=92705073362


                How it works




                • -v RS=' '



                  This tells awk to use a blank as the record separator.




                • /^EventCorrelationId=/ || /^CreationTime=/ || /^SubscriberNumber=/



                  This tells awk to print the record if it matches any of these three regular expressions. Some notes:



                  • In a regex, ^ means beginning-of-the-record. Thus /^CreationTime=/ means a record that starts with CreationTime=


                  • In awk, like many languages, || means logical-or. Thus ^EventCorrelationId=/ || /^CreationTime=/ is true if either regex is matched.



                Using sed



                $ sed -En 's/.*(EventCorrelationId=[^ ]*).*(CreationTime=[^ ]*).*(SubscriberNumber=[^ ]*).*/1n2n3/p' textfile
                EventCorrelationId="615-493|-1899671563||1550927718000"
                CreationTime="20190225094504"
                SubscriberNumber=92705073362


                Using an alternate separator in the output



                To use | as the output separator in place of newline:



                $ sed -En 's/.*(EventCorrelationId=[^ ]*).*(CreationTime=[^ ]*).*(SubscriberNumber=[^ ]*).*/1|2|3/p' textfile
                EventCorrelationId="615-493|-1899671563||1550927718000"|CreationTime="20190225094504"|SubscriberNumber=92705073362





                share|improve this answer





























                  3














                  Using grep



                  $ grep -oE '(EventCorrelationId|CreationTime|SubscriberNumber)[^ ]*' textfile
                  EventCorrelationId="615-493|-1899671563||1550927718000"
                  CreationTime="20190225094504"
                  SubscriberNumber=92705073362


                  Using awk



                  Try:



                  $ awk -v RS=' ' '/^EventCorrelationId=/ || /^CreationTime=/ || /^SubscriberNumber=/' textfile
                  EventCorrelationId="615-493|-1899671563||1550927718000"
                  CreationTime="20190225094504"
                  SubscriberNumber=92705073362


                  How it works




                  • -v RS=' '



                    This tells awk to use a blank as the record separator.




                  • /^EventCorrelationId=/ || /^CreationTime=/ || /^SubscriberNumber=/



                    This tells awk to print the record if it matches any of these three regular expressions. Some notes:



                    • In a regex, ^ means beginning-of-the-record. Thus /^CreationTime=/ means a record that starts with CreationTime=


                    • In awk, like many languages, || means logical-or. Thus ^EventCorrelationId=/ || /^CreationTime=/ is true if either regex is matched.



                  Using sed



                  $ sed -En 's/.*(EventCorrelationId=[^ ]*).*(CreationTime=[^ ]*).*(SubscriberNumber=[^ ]*).*/1n2n3/p' textfile
                  EventCorrelationId="615-493|-1899671563||1550927718000"
                  CreationTime="20190225094504"
                  SubscriberNumber=92705073362


                  Using an alternate separator in the output



                  To use | as the output separator in place of newline:



                  $ sed -En 's/.*(EventCorrelationId=[^ ]*).*(CreationTime=[^ ]*).*(SubscriberNumber=[^ ]*).*/1|2|3/p' textfile
                  EventCorrelationId="615-493|-1899671563||1550927718000"|CreationTime="20190225094504"|SubscriberNumber=92705073362





                  share|improve this answer



























                    3












                    3








                    3







                    Using grep



                    $ grep -oE '(EventCorrelationId|CreationTime|SubscriberNumber)[^ ]*' textfile
                    EventCorrelationId="615-493|-1899671563||1550927718000"
                    CreationTime="20190225094504"
                    SubscriberNumber=92705073362


                    Using awk



                    Try:



                    $ awk -v RS=' ' '/^EventCorrelationId=/ || /^CreationTime=/ || /^SubscriberNumber=/' textfile
                    EventCorrelationId="615-493|-1899671563||1550927718000"
                    CreationTime="20190225094504"
                    SubscriberNumber=92705073362


                    How it works




                    • -v RS=' '



                      This tells awk to use a blank as the record separator.




                    • /^EventCorrelationId=/ || /^CreationTime=/ || /^SubscriberNumber=/



                      This tells awk to print the record if it matches any of these three regular expressions. Some notes:



                      • In a regex, ^ means beginning-of-the-record. Thus /^CreationTime=/ means a record that starts with CreationTime=


                      • In awk, like many languages, || means logical-or. Thus ^EventCorrelationId=/ || /^CreationTime=/ is true if either regex is matched.



                    Using sed



                    $ sed -En 's/.*(EventCorrelationId=[^ ]*).*(CreationTime=[^ ]*).*(SubscriberNumber=[^ ]*).*/1n2n3/p' textfile
                    EventCorrelationId="615-493|-1899671563||1550927718000"
                    CreationTime="20190225094504"
                    SubscriberNumber=92705073362


                    Using an alternate separator in the output



                    To use | as the output separator in place of newline:



                    $ sed -En 's/.*(EventCorrelationId=[^ ]*).*(CreationTime=[^ ]*).*(SubscriberNumber=[^ ]*).*/1|2|3/p' textfile
                    EventCorrelationId="615-493|-1899671563||1550927718000"|CreationTime="20190225094504"|SubscriberNumber=92705073362





                    share|improve this answer















                    Using grep



                    $ grep -oE '(EventCorrelationId|CreationTime|SubscriberNumber)[^ ]*' textfile
                    EventCorrelationId="615-493|-1899671563||1550927718000"
                    CreationTime="20190225094504"
                    SubscriberNumber=92705073362


                    Using awk



                    Try:



                    $ awk -v RS=' ' '/^EventCorrelationId=/ || /^CreationTime=/ || /^SubscriberNumber=/' textfile
                    EventCorrelationId="615-493|-1899671563||1550927718000"
                    CreationTime="20190225094504"
                    SubscriberNumber=92705073362


                    How it works




                    • -v RS=' '



                      This tells awk to use a blank as the record separator.




                    • /^EventCorrelationId=/ || /^CreationTime=/ || /^SubscriberNumber=/



                      This tells awk to print the record if it matches any of these three regular expressions. Some notes:



                      • In a regex, ^ means beginning-of-the-record. Thus /^CreationTime=/ means a record that starts with CreationTime=


                      • In awk, like many languages, || means logical-or. Thus ^EventCorrelationId=/ || /^CreationTime=/ is true if either regex is matched.



                    Using sed



                    $ sed -En 's/.*(EventCorrelationId=[^ ]*).*(CreationTime=[^ ]*).*(SubscriberNumber=[^ ]*).*/1n2n3/p' textfile
                    EventCorrelationId="615-493|-1899671563||1550927718000"
                    CreationTime="20190225094504"
                    SubscriberNumber=92705073362


                    Using an alternate separator in the output



                    To use | as the output separator in place of newline:



                    $ sed -En 's/.*(EventCorrelationId=[^ ]*).*(CreationTime=[^ ]*).*(SubscriberNumber=[^ ]*).*/1|2|3/p' textfile
                    EventCorrelationId="615-493|-1899671563||1550927718000"|CreationTime="20190225094504"|SubscriberNumber=92705073362






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Feb 25 at 8:42

























                    answered Feb 25 at 6:16









                    John1024John1024

                    47.8k5112126




                    47.8k5112126























                        2














                        $ grep -Eo '(EventCorrelationId|CreationTime|SubscriberNumber)=[^ ]+' file
                        EventCorrelationId="615-493|-1899671563||1550927718000"
                        CreationTime="20190225094504"
                        SubscriberNumber=92705073362



                        • grep -Eo grep in extended regular expression mode (less escaping of special chars) and print only the matched parts


                        • (EventCorrelationId|CreationTime|SubscriberNumber) match EventCorrelationId or CreationTime or SubscriberNumber


                        • =[^ ]+ followed by "=", followed by non-space-character one or more times

                        Edit1:



                        Now separated by "|":



                        $ echo $(grep -Eo '(EventCorrelationId|CreationTime|SubscriberNumber)=[^ ]+' file) | tr ' ' '|'
                        EventCorrelationId="615-493|-1899671563||1550927718000"|CreationTime="20190225094504"|SubscriberNumber=92705073362


                        Edit2:



                        Now separated by "|" in reversed order using tac:



                        $ echo $(grep -Eo '(EventCorrelationId|CreationTime|SubscriberNumber)=[^ ]+' file | tac) | tr ' ' '|'
                        SubscriberNumber=92705073362|CreationTime="20190225094504"|EventCorrelationId="615-493|-1899671563||1550927718000"





                        share|improve this answer

























                        • hanks for your kind support, using your (Using sed) command, I was able to extract the information. However, kindly to please suggest how to extract the mentioned values in one single line separated by "|" sign as shown below? SubscriberNumber=92705073362|CreationTime="20190225094504"|EventCorrelationId="615-493|-1899671563||1550927718000"

                          – Jack Anderson
                          Feb 26 at 4:41











                        • Example added. But I didn't use sed :)

                          – Freddy
                          Feb 26 at 4:56















                        2














                        $ grep -Eo '(EventCorrelationId|CreationTime|SubscriberNumber)=[^ ]+' file
                        EventCorrelationId="615-493|-1899671563||1550927718000"
                        CreationTime="20190225094504"
                        SubscriberNumber=92705073362



                        • grep -Eo grep in extended regular expression mode (less escaping of special chars) and print only the matched parts


                        • (EventCorrelationId|CreationTime|SubscriberNumber) match EventCorrelationId or CreationTime or SubscriberNumber


                        • =[^ ]+ followed by "=", followed by non-space-character one or more times

                        Edit1:



                        Now separated by "|":



                        $ echo $(grep -Eo '(EventCorrelationId|CreationTime|SubscriberNumber)=[^ ]+' file) | tr ' ' '|'
                        EventCorrelationId="615-493|-1899671563||1550927718000"|CreationTime="20190225094504"|SubscriberNumber=92705073362


                        Edit2:



                        Now separated by "|" in reversed order using tac:



                        $ echo $(grep -Eo '(EventCorrelationId|CreationTime|SubscriberNumber)=[^ ]+' file | tac) | tr ' ' '|'
                        SubscriberNumber=92705073362|CreationTime="20190225094504"|EventCorrelationId="615-493|-1899671563||1550927718000"





                        share|improve this answer

























                        • hanks for your kind support, using your (Using sed) command, I was able to extract the information. However, kindly to please suggest how to extract the mentioned values in one single line separated by "|" sign as shown below? SubscriberNumber=92705073362|CreationTime="20190225094504"|EventCorrelationId="615-493|-1899671563||1550927718000"

                          – Jack Anderson
                          Feb 26 at 4:41











                        • Example added. But I didn't use sed :)

                          – Freddy
                          Feb 26 at 4:56













                        2












                        2








                        2







                        $ grep -Eo '(EventCorrelationId|CreationTime|SubscriberNumber)=[^ ]+' file
                        EventCorrelationId="615-493|-1899671563||1550927718000"
                        CreationTime="20190225094504"
                        SubscriberNumber=92705073362



                        • grep -Eo grep in extended regular expression mode (less escaping of special chars) and print only the matched parts


                        • (EventCorrelationId|CreationTime|SubscriberNumber) match EventCorrelationId or CreationTime or SubscriberNumber


                        • =[^ ]+ followed by "=", followed by non-space-character one or more times

                        Edit1:



                        Now separated by "|":



                        $ echo $(grep -Eo '(EventCorrelationId|CreationTime|SubscriberNumber)=[^ ]+' file) | tr ' ' '|'
                        EventCorrelationId="615-493|-1899671563||1550927718000"|CreationTime="20190225094504"|SubscriberNumber=92705073362


                        Edit2:



                        Now separated by "|" in reversed order using tac:



                        $ echo $(grep -Eo '(EventCorrelationId|CreationTime|SubscriberNumber)=[^ ]+' file | tac) | tr ' ' '|'
                        SubscriberNumber=92705073362|CreationTime="20190225094504"|EventCorrelationId="615-493|-1899671563||1550927718000"





                        share|improve this answer















                        $ grep -Eo '(EventCorrelationId|CreationTime|SubscriberNumber)=[^ ]+' file
                        EventCorrelationId="615-493|-1899671563||1550927718000"
                        CreationTime="20190225094504"
                        SubscriberNumber=92705073362



                        • grep -Eo grep in extended regular expression mode (less escaping of special chars) and print only the matched parts


                        • (EventCorrelationId|CreationTime|SubscriberNumber) match EventCorrelationId or CreationTime or SubscriberNumber


                        • =[^ ]+ followed by "=", followed by non-space-character one or more times

                        Edit1:



                        Now separated by "|":



                        $ echo $(grep -Eo '(EventCorrelationId|CreationTime|SubscriberNumber)=[^ ]+' file) | tr ' ' '|'
                        EventCorrelationId="615-493|-1899671563||1550927718000"|CreationTime="20190225094504"|SubscriberNumber=92705073362


                        Edit2:



                        Now separated by "|" in reversed order using tac:



                        $ echo $(grep -Eo '(EventCorrelationId|CreationTime|SubscriberNumber)=[^ ]+' file | tac) | tr ' ' '|'
                        SubscriberNumber=92705073362|CreationTime="20190225094504"|EventCorrelationId="615-493|-1899671563||1550927718000"






                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Feb 26 at 4:53

























                        answered Feb 25 at 6:31









                        FreddyFreddy

                        1,339210




                        1,339210












                        • hanks for your kind support, using your (Using sed) command, I was able to extract the information. However, kindly to please suggest how to extract the mentioned values in one single line separated by "|" sign as shown below? SubscriberNumber=92705073362|CreationTime="20190225094504"|EventCorrelationId="615-493|-1899671563||1550927718000"

                          – Jack Anderson
                          Feb 26 at 4:41











                        • Example added. But I didn't use sed :)

                          – Freddy
                          Feb 26 at 4:56

















                        • hanks for your kind support, using your (Using sed) command, I was able to extract the information. However, kindly to please suggest how to extract the mentioned values in one single line separated by "|" sign as shown below? SubscriberNumber=92705073362|CreationTime="20190225094504"|EventCorrelationId="615-493|-1899671563||1550927718000"

                          – Jack Anderson
                          Feb 26 at 4:41











                        • Example added. But I didn't use sed :)

                          – Freddy
                          Feb 26 at 4:56
















                        hanks for your kind support, using your (Using sed) command, I was able to extract the information. However, kindly to please suggest how to extract the mentioned values in one single line separated by "|" sign as shown below? SubscriberNumber=92705073362|CreationTime="20190225094504"|EventCorrelationId="615-493|-1899671563||1550927718000"

                        – Jack Anderson
                        Feb 26 at 4:41





                        hanks for your kind support, using your (Using sed) command, I was able to extract the information. However, kindly to please suggest how to extract the mentioned values in one single line separated by "|" sign as shown below? SubscriberNumber=92705073362|CreationTime="20190225094504"|EventCorrelationId="615-493|-1899671563||1550927718000"

                        – Jack Anderson
                        Feb 26 at 4:41













                        Example added. But I didn't use sed :)

                        – Freddy
                        Feb 26 at 4:56





                        Example added. But I didn't use sed :)

                        – Freddy
                        Feb 26 at 4:56











                        0














                        I Tried with below awk method and it worked fine



                        j=`awk 'print NF' filename `
                        for ((i=1;i<=$j;i++)); do awk -v i="$i" '$i ~ /EventCorrelationId/||$i ~ /CreationTime/||$i ~ /SubscriberNumber/print $i' filename ; done


                        output



                        EventCorrelationId="615-493|-1899671563||1550927718000"
                        CreationTime="20190225094504"
                        SubscriberNumber=9270507336





                        share|improve this answer



























                          0














                          I Tried with below awk method and it worked fine



                          j=`awk 'print NF' filename `
                          for ((i=1;i<=$j;i++)); do awk -v i="$i" '$i ~ /EventCorrelationId/||$i ~ /CreationTime/||$i ~ /SubscriberNumber/print $i' filename ; done


                          output



                          EventCorrelationId="615-493|-1899671563||1550927718000"
                          CreationTime="20190225094504"
                          SubscriberNumber=9270507336





                          share|improve this answer

























                            0












                            0








                            0







                            I Tried with below awk method and it worked fine



                            j=`awk 'print NF' filename `
                            for ((i=1;i<=$j;i++)); do awk -v i="$i" '$i ~ /EventCorrelationId/||$i ~ /CreationTime/||$i ~ /SubscriberNumber/print $i' filename ; done


                            output



                            EventCorrelationId="615-493|-1899671563||1550927718000"
                            CreationTime="20190225094504"
                            SubscriberNumber=9270507336





                            share|improve this answer













                            I Tried with below awk method and it worked fine



                            j=`awk 'print NF' filename `
                            for ((i=1;i<=$j;i++)); do awk -v i="$i" '$i ~ /EventCorrelationId/||$i ~ /CreationTime/||$i ~ /SubscriberNumber/print $i' filename ; done


                            output



                            EventCorrelationId="615-493|-1899671563||1550927718000"
                            CreationTime="20190225094504"
                            SubscriberNumber=9270507336






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Feb 26 at 8:05









                            Praveen Kumar BSPraveen Kumar BS

                            1,6471311




                            1,6471311





















                                0














                                You can perform this exercise bu building up your regex dynamically, based on the fields you want outputted:



                                $ perl -lne '
                                $re = join "|", map +quotemeta qw/EventCorrelationId CreationTime SubscriberNumber/;
                                print join "|", /(?:$re)=H+/g;
                                ' input.file


                                Output:



                                EventCorrelationId="615-493|-1899671563||1550927718000"|CreationTime="20190225094504"|SubscriberNumber=92705073362


                                Working:



                                • The fields that you want output are placed in an anonymous array qw/.../.

                                • Then we transform each of them using map ... by joining with logical OR |.

                                • In the last step, we apply the regex just constructed on the current record, $_, and the fields fished out are joined via the pipe symbol | to get the output.





                                share|improve this answer



























                                  0














                                  You can perform this exercise bu building up your regex dynamically, based on the fields you want outputted:



                                  $ perl -lne '
                                  $re = join "|", map +quotemeta qw/EventCorrelationId CreationTime SubscriberNumber/;
                                  print join "|", /(?:$re)=H+/g;
                                  ' input.file


                                  Output:



                                  EventCorrelationId="615-493|-1899671563||1550927718000"|CreationTime="20190225094504"|SubscriberNumber=92705073362


                                  Working:



                                  • The fields that you want output are placed in an anonymous array qw/.../.

                                  • Then we transform each of them using map ... by joining with logical OR |.

                                  • In the last step, we apply the regex just constructed on the current record, $_, and the fields fished out are joined via the pipe symbol | to get the output.





                                  share|improve this answer

























                                    0












                                    0








                                    0







                                    You can perform this exercise bu building up your regex dynamically, based on the fields you want outputted:



                                    $ perl -lne '
                                    $re = join "|", map +quotemeta qw/EventCorrelationId CreationTime SubscriberNumber/;
                                    print join "|", /(?:$re)=H+/g;
                                    ' input.file


                                    Output:



                                    EventCorrelationId="615-493|-1899671563||1550927718000"|CreationTime="20190225094504"|SubscriberNumber=92705073362


                                    Working:



                                    • The fields that you want output are placed in an anonymous array qw/.../.

                                    • Then we transform each of them using map ... by joining with logical OR |.

                                    • In the last step, we apply the regex just constructed on the current record, $_, and the fields fished out are joined via the pipe symbol | to get the output.





                                    share|improve this answer













                                    You can perform this exercise bu building up your regex dynamically, based on the fields you want outputted:



                                    $ perl -lne '
                                    $re = join "|", map +quotemeta qw/EventCorrelationId CreationTime SubscriberNumber/;
                                    print join "|", /(?:$re)=H+/g;
                                    ' input.file


                                    Output:



                                    EventCorrelationId="615-493|-1899671563||1550927718000"|CreationTime="20190225094504"|SubscriberNumber=92705073362


                                    Working:



                                    • The fields that you want output are placed in an anonymous array qw/.../.

                                    • Then we transform each of them using map ... by joining with logical OR |.

                                    • In the last step, we apply the regex just constructed on the current record, $_, and the fields fished out are joined via the pipe symbol | to get the output.






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Feb 26 at 15:08









                                    Rakesh SharmaRakesh Sharma

                                    582




                                    582



























                                        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%2f502782%2fhow-to-extract-specific-values-fields-from-the-text-file%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?