Get one thing from a line and concatenate it with one thing from another line









up vote
4
down vote

favorite












I have the following:



2018-11-10 23:57:21 [COMMAND]: sar -u 10 5

AIX host 1 7 11/10/18

System configuration: lcpu=64 mode=Capped

23:57:21 %usr %sys %wio %idle physc
23:57:31 10 7 0 83 16.00
23:57:41 9 6 0 85 16.00
23:57:51 9 6 0 85 16.00
23:58:01 9 7 0 84 16.00
23:58:11 10 6 0 84 16.00

Average 9 6 0 84 16.00

2018-11-10 23:58:21 [COMMAND]: sar -u 10 5

AIX host 1 7 11/10/18

System configuration: lcpu=64 mode=Capped

23:58:21 %usr %sys %wio %idle physc
23:58:31 10 8 0 82 15.99
23:58:41 9 6 0 85 16.00
23:58:51 9 6 0 85 16.00
23:59:01 9 6 0 84 16.00
23:59:11 10 6 0 83 16.00

Average 10 6 0 84 16.00


I need to get the time with the average value of %idle:



2018-11-10 23:57:21|84
2018-11-10 23:58:21|84









share|improve this question























  • actually i am getting this file from another server and it is only one file
    – ammar
    Nov 11 at 13:12






  • 1




    This is using this site as a script-writing service.
    – Peter Mortensen
    Nov 12 at 8:30














up vote
4
down vote

favorite












I have the following:



2018-11-10 23:57:21 [COMMAND]: sar -u 10 5

AIX host 1 7 11/10/18

System configuration: lcpu=64 mode=Capped

23:57:21 %usr %sys %wio %idle physc
23:57:31 10 7 0 83 16.00
23:57:41 9 6 0 85 16.00
23:57:51 9 6 0 85 16.00
23:58:01 9 7 0 84 16.00
23:58:11 10 6 0 84 16.00

Average 9 6 0 84 16.00

2018-11-10 23:58:21 [COMMAND]: sar -u 10 5

AIX host 1 7 11/10/18

System configuration: lcpu=64 mode=Capped

23:58:21 %usr %sys %wio %idle physc
23:58:31 10 8 0 82 15.99
23:58:41 9 6 0 85 16.00
23:58:51 9 6 0 85 16.00
23:59:01 9 6 0 84 16.00
23:59:11 10 6 0 83 16.00

Average 10 6 0 84 16.00


I need to get the time with the average value of %idle:



2018-11-10 23:57:21|84
2018-11-10 23:58:21|84









share|improve this question























  • actually i am getting this file from another server and it is only one file
    – ammar
    Nov 11 at 13:12






  • 1




    This is using this site as a script-writing service.
    – Peter Mortensen
    Nov 12 at 8:30












up vote
4
down vote

favorite









up vote
4
down vote

favorite











I have the following:



2018-11-10 23:57:21 [COMMAND]: sar -u 10 5

AIX host 1 7 11/10/18

System configuration: lcpu=64 mode=Capped

23:57:21 %usr %sys %wio %idle physc
23:57:31 10 7 0 83 16.00
23:57:41 9 6 0 85 16.00
23:57:51 9 6 0 85 16.00
23:58:01 9 7 0 84 16.00
23:58:11 10 6 0 84 16.00

Average 9 6 0 84 16.00

2018-11-10 23:58:21 [COMMAND]: sar -u 10 5

AIX host 1 7 11/10/18

System configuration: lcpu=64 mode=Capped

23:58:21 %usr %sys %wio %idle physc
23:58:31 10 8 0 82 15.99
23:58:41 9 6 0 85 16.00
23:58:51 9 6 0 85 16.00
23:59:01 9 6 0 84 16.00
23:59:11 10 6 0 83 16.00

Average 10 6 0 84 16.00


I need to get the time with the average value of %idle:



2018-11-10 23:57:21|84
2018-11-10 23:58:21|84









share|improve this question















I have the following:



2018-11-10 23:57:21 [COMMAND]: sar -u 10 5

AIX host 1 7 11/10/18

System configuration: lcpu=64 mode=Capped

23:57:21 %usr %sys %wio %idle physc
23:57:31 10 7 0 83 16.00
23:57:41 9 6 0 85 16.00
23:57:51 9 6 0 85 16.00
23:58:01 9 7 0 84 16.00
23:58:11 10 6 0 84 16.00

Average 9 6 0 84 16.00

2018-11-10 23:58:21 [COMMAND]: sar -u 10 5

AIX host 1 7 11/10/18

System configuration: lcpu=64 mode=Capped

23:58:21 %usr %sys %wio %idle physc
23:58:31 10 8 0 82 15.99
23:58:41 9 6 0 85 16.00
23:58:51 9 6 0 85 16.00
23:59:01 9 6 0 84 16.00
23:59:11 10 6 0 83 16.00

Average 10 6 0 84 16.00


I need to get the time with the average value of %idle:



2018-11-10 23:57:21|84
2018-11-10 23:58:21|84






text-processing awk sed perl






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 9:07









Peter Mortensen

85758




85758










asked Nov 11 at 13:02









ammar

357




357











  • actually i am getting this file from another server and it is only one file
    – ammar
    Nov 11 at 13:12






  • 1




    This is using this site as a script-writing service.
    – Peter Mortensen
    Nov 12 at 8:30
















  • actually i am getting this file from another server and it is only one file
    – ammar
    Nov 11 at 13:12






  • 1




    This is using this site as a script-writing service.
    – Peter Mortensen
    Nov 12 at 8:30















actually i am getting this file from another server and it is only one file
– ammar
Nov 11 at 13:12




actually i am getting this file from another server and it is only one file
– ammar
Nov 11 at 13:12




1




1




This is using this site as a script-writing service.
– Peter Mortensen
Nov 12 at 8:30




This is using this site as a script-writing service.
– Peter Mortensen
Nov 12 at 8:30










3 Answers
3






active

oldest

votes

















up vote
11
down vote



accepted










Going by your input file as-is, a simple awk command as simple as below should suffice.



awk '/sar/ time=$1" "$2; next /Average/ print time"' file





share|improve this answer



























    up vote
    5
    down vote













    A couple of choices:




    • awk



      $ awk '/COMMAND/printf "%s %s", $1,$2/Average/"$5' file 
      2018-11-10 23:57:21|84
      2018-11-10 23:58:21|84



    • perl



      $ perl -ale '$k="$F[0] $F[1]" if /COMMAND/; print "$k|$F[4]" if /Average/' file 
      2018-11-10 23:57:21|84
      2018-11-10 23:58:21|84






    share|improve this answer





























      up vote
      2
      down vote













      This Perl one-liner should do the trick:



      perl -lne'$d=$1 if /^(d4-d2-d2s+d2:d2:d2)/;
      if(/^Averages+S+s+S+s+S+s+(S+)/) $1";'


      (I made it two lines, so it's more readable.) You need to feed it with input though. The file name at the end, or <file_name at the beginning or the end, or a pipe will do. The workings are these:



      1. Go to a line with date and time at the beginning and memorise it.

      2. Go to a line with Average at beginning and once there, print the memorised date and time plus the | sign and the average taken from the fifth column assuming whitespace column separation.

      3. Point 1. repeats.





      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: 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%2f481082%2fget-one-thing-from-a-line-and-concatenate-it-with-one-thing-from-another-line%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








        up vote
        11
        down vote



        accepted










        Going by your input file as-is, a simple awk command as simple as below should suffice.



        awk '/sar/ time=$1" "$2; next /Average/ print time"' file





        share|improve this answer
























          up vote
          11
          down vote



          accepted










          Going by your input file as-is, a simple awk command as simple as below should suffice.



          awk '/sar/ time=$1" "$2; next /Average/ print time"' file





          share|improve this answer






















            up vote
            11
            down vote



            accepted







            up vote
            11
            down vote



            accepted






            Going by your input file as-is, a simple awk command as simple as below should suffice.



            awk '/sar/ time=$1" "$2; next /Average/ print time"' file





            share|improve this answer












            Going by your input file as-is, a simple awk command as simple as below should suffice.



            awk '/sar/ time=$1" "$2; next /Average/ print time"' file






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 11 at 13:16









            Inian

            3,815824




            3,815824






















                up vote
                5
                down vote













                A couple of choices:




                • awk



                  $ awk '/COMMAND/printf "%s %s", $1,$2/Average/"$5' file 
                  2018-11-10 23:57:21|84
                  2018-11-10 23:58:21|84



                • perl



                  $ perl -ale '$k="$F[0] $F[1]" if /COMMAND/; print "$k|$F[4]" if /Average/' file 
                  2018-11-10 23:57:21|84
                  2018-11-10 23:58:21|84






                share|improve this answer


























                  up vote
                  5
                  down vote













                  A couple of choices:




                  • awk



                    $ awk '/COMMAND/printf "%s %s", $1,$2/Average/"$5' file 
                    2018-11-10 23:57:21|84
                    2018-11-10 23:58:21|84



                  • perl



                    $ perl -ale '$k="$F[0] $F[1]" if /COMMAND/; print "$k|$F[4]" if /Average/' file 
                    2018-11-10 23:57:21|84
                    2018-11-10 23:58:21|84






                  share|improve this answer
























                    up vote
                    5
                    down vote










                    up vote
                    5
                    down vote









                    A couple of choices:




                    • awk



                      $ awk '/COMMAND/printf "%s %s", $1,$2/Average/"$5' file 
                      2018-11-10 23:57:21|84
                      2018-11-10 23:58:21|84



                    • perl



                      $ perl -ale '$k="$F[0] $F[1]" if /COMMAND/; print "$k|$F[4]" if /Average/' file 
                      2018-11-10 23:57:21|84
                      2018-11-10 23:58:21|84






                    share|improve this answer














                    A couple of choices:




                    • awk



                      $ awk '/COMMAND/printf "%s %s", $1,$2/Average/"$5' file 
                      2018-11-10 23:57:21|84
                      2018-11-10 23:58:21|84



                    • perl



                      $ perl -ale '$k="$F[0] $F[1]" if /COMMAND/; print "$k|$F[4]" if /Average/' file 
                      2018-11-10 23:57:21|84
                      2018-11-10 23:58:21|84







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 11 at 16:19

























                    answered Nov 11 at 14:48









                    terdon

                    127k31245422




                    127k31245422




















                        up vote
                        2
                        down vote













                        This Perl one-liner should do the trick:



                        perl -lne'$d=$1 if /^(d4-d2-d2s+d2:d2:d2)/;
                        if(/^Averages+S+s+S+s+S+s+(S+)/) $1";'


                        (I made it two lines, so it's more readable.) You need to feed it with input though. The file name at the end, or <file_name at the beginning or the end, or a pipe will do. The workings are these:



                        1. Go to a line with date and time at the beginning and memorise it.

                        2. Go to a line with Average at beginning and once there, print the memorised date and time plus the | sign and the average taken from the fifth column assuming whitespace column separation.

                        3. Point 1. repeats.





                        share|improve this answer


























                          up vote
                          2
                          down vote













                          This Perl one-liner should do the trick:



                          perl -lne'$d=$1 if /^(d4-d2-d2s+d2:d2:d2)/;
                          if(/^Averages+S+s+S+s+S+s+(S+)/) $1";'


                          (I made it two lines, so it's more readable.) You need to feed it with input though. The file name at the end, or <file_name at the beginning or the end, or a pipe will do. The workings are these:



                          1. Go to a line with date and time at the beginning and memorise it.

                          2. Go to a line with Average at beginning and once there, print the memorised date and time plus the | sign and the average taken from the fifth column assuming whitespace column separation.

                          3. Point 1. repeats.





                          share|improve this answer
























                            up vote
                            2
                            down vote










                            up vote
                            2
                            down vote









                            This Perl one-liner should do the trick:



                            perl -lne'$d=$1 if /^(d4-d2-d2s+d2:d2:d2)/;
                            if(/^Averages+S+s+S+s+S+s+(S+)/) $1";'


                            (I made it two lines, so it's more readable.) You need to feed it with input though. The file name at the end, or <file_name at the beginning or the end, or a pipe will do. The workings are these:



                            1. Go to a line with date and time at the beginning and memorise it.

                            2. Go to a line with Average at beginning and once there, print the memorised date and time plus the | sign and the average taken from the fifth column assuming whitespace column separation.

                            3. Point 1. repeats.





                            share|improve this answer














                            This Perl one-liner should do the trick:



                            perl -lne'$d=$1 if /^(d4-d2-d2s+d2:d2:d2)/;
                            if(/^Averages+S+s+S+s+S+s+(S+)/) $1";'


                            (I made it two lines, so it's more readable.) You need to feed it with input though. The file name at the end, or <file_name at the beginning or the end, or a pipe will do. The workings are these:



                            1. Go to a line with date and time at the beginning and memorise it.

                            2. Go to a line with Average at beginning and once there, print the memorised date and time plus the | sign and the average taken from the fifth column assuming whitespace column separation.

                            3. Point 1. repeats.






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 11 at 13:23

























                            answered Nov 11 at 13:15









                            Tomasz

                            8,80852863




                            8,80852863



























                                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.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • 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%2f481082%2fget-one-thing-from-a-line-and-concatenate-it-with-one-thing-from-another-line%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

                                Top Tejano songwriter Luis Silva dead of heart attack at 64

                                ReactJS Fetched API data displays live - need Data displayed static

                                政党