print the first line of those files in current directory which are having last line with a pattern












3















I have multiple log files in a directory and want to print first line of those files which are having pattern vanished on last line.



Could it be done with awk for *.logs having also printed name of the logfile before the first line is printed out?










share|improve this question





























    3















    I have multiple log files in a directory and want to print first line of those files which are having pattern vanished on last line.



    Could it be done with awk for *.logs having also printed name of the logfile before the first line is printed out?










    share|improve this question



























      3












      3








      3


      0






      I have multiple log files in a directory and want to print first line of those files which are having pattern vanished on last line.



      Could it be done with awk for *.logs having also printed name of the logfile before the first line is printed out?










      share|improve this question
















      I have multiple log files in a directory and want to print first line of those files which are having pattern vanished on last line.



      Could it be done with awk for *.logs having also printed name of the logfile before the first line is printed out?







      text-processing awk files






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 22 '18 at 17:35









      don_crissti

      50.3k15134162




      50.3k15134162










      asked Dec 22 '18 at 14:44









      ChrisChris

      146112




      146112






















          2 Answers
          2






          active

          oldest

          votes


















          4














          something like this?



          for f in *.logs; do tail -n 1 -- "$f" | grep -q vanished && { printf '%s: ' "$f"; head -n 1 -- "$f"; }; done


          You can also do it with awk but it will be less efficient if the files are big, because you will have to read the whole file, not just the beginning and the end.






          share|improve this answer

































            3














            If you want to do this with awk only this could probably get you in the right direction:



            for f in *.log; do awk 'NR==1{ first_line=$0 } END { last_line=$0; print( last_line ~ /varnished/) ? "File: " FILENAME "n" first_line : "" }' "$f"; done


            The for loop is probably not necessarily but this is the most efficient way I could find doing it with awk



            And thanks to the comments below, now we can let gawk do its work as follows:



            gawk 'FNR==1 {first = $0} ENDFILE {if($0 ~ /varnished/) print substr(FILENAME,3) ":", first}' ./*.log 


            On recent linux systems, awk is a symlink to gawk. For example, on recent Fedora releases this is the case:



            # ls -l /bin/awk 
            lrwxrwxrwx. 1 root root 4 Jul 13 07:55 /bin/awk -> gawk





            share|improve this answer





















            • 2





              With a reasonably recent GNU awk you probably could avoid the loop e.g. gawk 'FNR==1 {first = $0} ENDFILE {if($0 ~ /varnished/) print FILENAME ":", first}' *.log

              – steeldriver
              Dec 22 '18 at 17:31






            • 2





              That wouldn't work with files names foo=bar.log for instance. You can work around it by using ./*.log and replace FILENAME with substr(FILENAME, 3).

              – Stéphane Chazelas
              Dec 22 '18 at 18:02











            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%2f490481%2fprint-the-first-line-of-those-files-in-current-directory-which-are-having-last-l%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            4














            something like this?



            for f in *.logs; do tail -n 1 -- "$f" | grep -q vanished && { printf '%s: ' "$f"; head -n 1 -- "$f"; }; done


            You can also do it with awk but it will be less efficient if the files are big, because you will have to read the whole file, not just the beginning and the end.






            share|improve this answer






























              4














              something like this?



              for f in *.logs; do tail -n 1 -- "$f" | grep -q vanished && { printf '%s: ' "$f"; head -n 1 -- "$f"; }; done


              You can also do it with awk but it will be less efficient if the files are big, because you will have to read the whole file, not just the beginning and the end.






              share|improve this answer




























                4












                4








                4







                something like this?



                for f in *.logs; do tail -n 1 -- "$f" | grep -q vanished && { printf '%s: ' "$f"; head -n 1 -- "$f"; }; done


                You can also do it with awk but it will be less efficient if the files are big, because you will have to read the whole file, not just the beginning and the end.






                share|improve this answer















                something like this?



                for f in *.logs; do tail -n 1 -- "$f" | grep -q vanished && { printf '%s: ' "$f"; head -n 1 -- "$f"; }; done


                You can also do it with awk but it will be less efficient if the files are big, because you will have to read the whole file, not just the beginning and the end.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Dec 22 '18 at 18:00









                Stéphane Chazelas

                301k55565917




                301k55565917










                answered Dec 22 '18 at 15:04









                Uncle BillyUncle Billy

                4185




                4185

























                    3














                    If you want to do this with awk only this could probably get you in the right direction:



                    for f in *.log; do awk 'NR==1{ first_line=$0 } END { last_line=$0; print( last_line ~ /varnished/) ? "File: " FILENAME "n" first_line : "" }' "$f"; done


                    The for loop is probably not necessarily but this is the most efficient way I could find doing it with awk



                    And thanks to the comments below, now we can let gawk do its work as follows:



                    gawk 'FNR==1 {first = $0} ENDFILE {if($0 ~ /varnished/) print substr(FILENAME,3) ":", first}' ./*.log 


                    On recent linux systems, awk is a symlink to gawk. For example, on recent Fedora releases this is the case:



                    # ls -l /bin/awk 
                    lrwxrwxrwx. 1 root root 4 Jul 13 07:55 /bin/awk -> gawk





                    share|improve this answer





















                    • 2





                      With a reasonably recent GNU awk you probably could avoid the loop e.g. gawk 'FNR==1 {first = $0} ENDFILE {if($0 ~ /varnished/) print FILENAME ":", first}' *.log

                      – steeldriver
                      Dec 22 '18 at 17:31






                    • 2





                      That wouldn't work with files names foo=bar.log for instance. You can work around it by using ./*.log and replace FILENAME with substr(FILENAME, 3).

                      – Stéphane Chazelas
                      Dec 22 '18 at 18:02
















                    3














                    If you want to do this with awk only this could probably get you in the right direction:



                    for f in *.log; do awk 'NR==1{ first_line=$0 } END { last_line=$0; print( last_line ~ /varnished/) ? "File: " FILENAME "n" first_line : "" }' "$f"; done


                    The for loop is probably not necessarily but this is the most efficient way I could find doing it with awk



                    And thanks to the comments below, now we can let gawk do its work as follows:



                    gawk 'FNR==1 {first = $0} ENDFILE {if($0 ~ /varnished/) print substr(FILENAME,3) ":", first}' ./*.log 


                    On recent linux systems, awk is a symlink to gawk. For example, on recent Fedora releases this is the case:



                    # ls -l /bin/awk 
                    lrwxrwxrwx. 1 root root 4 Jul 13 07:55 /bin/awk -> gawk





                    share|improve this answer





















                    • 2





                      With a reasonably recent GNU awk you probably could avoid the loop e.g. gawk 'FNR==1 {first = $0} ENDFILE {if($0 ~ /varnished/) print FILENAME ":", first}' *.log

                      – steeldriver
                      Dec 22 '18 at 17:31






                    • 2





                      That wouldn't work with files names foo=bar.log for instance. You can work around it by using ./*.log and replace FILENAME with substr(FILENAME, 3).

                      – Stéphane Chazelas
                      Dec 22 '18 at 18:02














                    3












                    3








                    3







                    If you want to do this with awk only this could probably get you in the right direction:



                    for f in *.log; do awk 'NR==1{ first_line=$0 } END { last_line=$0; print( last_line ~ /varnished/) ? "File: " FILENAME "n" first_line : "" }' "$f"; done


                    The for loop is probably not necessarily but this is the most efficient way I could find doing it with awk



                    And thanks to the comments below, now we can let gawk do its work as follows:



                    gawk 'FNR==1 {first = $0} ENDFILE {if($0 ~ /varnished/) print substr(FILENAME,3) ":", first}' ./*.log 


                    On recent linux systems, awk is a symlink to gawk. For example, on recent Fedora releases this is the case:



                    # ls -l /bin/awk 
                    lrwxrwxrwx. 1 root root 4 Jul 13 07:55 /bin/awk -> gawk





                    share|improve this answer















                    If you want to do this with awk only this could probably get you in the right direction:



                    for f in *.log; do awk 'NR==1{ first_line=$0 } END { last_line=$0; print( last_line ~ /varnished/) ? "File: " FILENAME "n" first_line : "" }' "$f"; done


                    The for loop is probably not necessarily but this is the most efficient way I could find doing it with awk



                    And thanks to the comments below, now we can let gawk do its work as follows:



                    gawk 'FNR==1 {first = $0} ENDFILE {if($0 ~ /varnished/) print substr(FILENAME,3) ":", first}' ./*.log 


                    On recent linux systems, awk is a symlink to gawk. For example, on recent Fedora releases this is the case:



                    # ls -l /bin/awk 
                    lrwxrwxrwx. 1 root root 4 Jul 13 07:55 /bin/awk -> gawk






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Dec 23 '18 at 13:59

























                    answered Dec 22 '18 at 16:17









                    Valentin BajramiValentin Bajrami

                    5,96111627




                    5,96111627








                    • 2





                      With a reasonably recent GNU awk you probably could avoid the loop e.g. gawk 'FNR==1 {first = $0} ENDFILE {if($0 ~ /varnished/) print FILENAME ":", first}' *.log

                      – steeldriver
                      Dec 22 '18 at 17:31






                    • 2





                      That wouldn't work with files names foo=bar.log for instance. You can work around it by using ./*.log and replace FILENAME with substr(FILENAME, 3).

                      – Stéphane Chazelas
                      Dec 22 '18 at 18:02














                    • 2





                      With a reasonably recent GNU awk you probably could avoid the loop e.g. gawk 'FNR==1 {first = $0} ENDFILE {if($0 ~ /varnished/) print FILENAME ":", first}' *.log

                      – steeldriver
                      Dec 22 '18 at 17:31






                    • 2





                      That wouldn't work with files names foo=bar.log for instance. You can work around it by using ./*.log and replace FILENAME with substr(FILENAME, 3).

                      – Stéphane Chazelas
                      Dec 22 '18 at 18:02








                    2




                    2





                    With a reasonably recent GNU awk you probably could avoid the loop e.g. gawk 'FNR==1 {first = $0} ENDFILE {if($0 ~ /varnished/) print FILENAME ":", first}' *.log

                    – steeldriver
                    Dec 22 '18 at 17:31





                    With a reasonably recent GNU awk you probably could avoid the loop e.g. gawk 'FNR==1 {first = $0} ENDFILE {if($0 ~ /varnished/) print FILENAME ":", first}' *.log

                    – steeldriver
                    Dec 22 '18 at 17:31




                    2




                    2





                    That wouldn't work with files names foo=bar.log for instance. You can work around it by using ./*.log and replace FILENAME with substr(FILENAME, 3).

                    – Stéphane Chazelas
                    Dec 22 '18 at 18:02





                    That wouldn't work with files names foo=bar.log for instance. You can work around it by using ./*.log and replace FILENAME with substr(FILENAME, 3).

                    – Stéphane Chazelas
                    Dec 22 '18 at 18:02


















                    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%2f490481%2fprint-the-first-line-of-those-files-in-current-directory-which-are-having-last-l%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

                    Plaza Victoria

                    In PowerPoint, is there a keyboard shortcut for bulleted / numbered list?

                    How to put 3 figures in Latex with 2 figures side by side and 1 below these side by side images but in...