Excise recent date from log file, output as motd












1















For finding a date in a log file I've arrived at the following crude command:



grep Updated /var/log/socklog/xbps/current | tail -n 1 | grep -Eo ^20[0-9][0-9]-[0-9][0-9]-[0-9][0-9]


That does what I need, which is to output the date of the last system upgrade I did on my Void machine: for example 2019-01-24. But it's a pretty cumbersome line and quite crude as well. I assume there must be a more elegant solution to extract the needed date from this file. Incidentally, the date is comprised by the first 10 characters of the line containing the text grep'd for. Any suggestions for a more elegant solution?



This line, btw, is being used to produce a motd so that when I log into my system I'll see something like "last system upgrade 2019-01-24." So I created a script /etc/motd.sh with that line in it, which then gets called from /etc/profile. Am I going about making the information visible in the correct way? What might be some other alternatives for doing what I want?



The script /etc/motd.sh looks like:



#!/bin/bash
echo last system upgrade $(grep Updated /var/log/socklog/xbps/current | tail -n 1 | grep -Eo ^20[0-9][0-9]-[0-9][0-9]-[0-9][0-9])


PS Here's a sample line from the log file, as requested:



2019-01-15T18:05:51.31699 user.notice: Jan 15 12:05:51 xbps-install: Installed `brotli-1.0.7_1' successfully (rootdir: /).
2019-01-15T18:05:51.35465 user.notice: Jan 15 12:05:51 xbps-install: Updated `xwininfo-1.1.4_2' successfully (rootdir: /).









share|improve this question




















  • 2





    I think it'd help answerers to see sample lines from the input file

    – Jeff Schaller
    yesterday











  • I agree with Jeff, but also, if it works, it works. Without seeing the input file, it doesn't seem so inelegant to me.

    – Sparhawk
    yesterday











  • The sample line doesn't have 'Updated' in it, is there more to the line?

    – user1794469
    yesterday
















1















For finding a date in a log file I've arrived at the following crude command:



grep Updated /var/log/socklog/xbps/current | tail -n 1 | grep -Eo ^20[0-9][0-9]-[0-9][0-9]-[0-9][0-9]


That does what I need, which is to output the date of the last system upgrade I did on my Void machine: for example 2019-01-24. But it's a pretty cumbersome line and quite crude as well. I assume there must be a more elegant solution to extract the needed date from this file. Incidentally, the date is comprised by the first 10 characters of the line containing the text grep'd for. Any suggestions for a more elegant solution?



This line, btw, is being used to produce a motd so that when I log into my system I'll see something like "last system upgrade 2019-01-24." So I created a script /etc/motd.sh with that line in it, which then gets called from /etc/profile. Am I going about making the information visible in the correct way? What might be some other alternatives for doing what I want?



The script /etc/motd.sh looks like:



#!/bin/bash
echo last system upgrade $(grep Updated /var/log/socklog/xbps/current | tail -n 1 | grep -Eo ^20[0-9][0-9]-[0-9][0-9]-[0-9][0-9])


PS Here's a sample line from the log file, as requested:



2019-01-15T18:05:51.31699 user.notice: Jan 15 12:05:51 xbps-install: Installed `brotli-1.0.7_1' successfully (rootdir: /).
2019-01-15T18:05:51.35465 user.notice: Jan 15 12:05:51 xbps-install: Updated `xwininfo-1.1.4_2' successfully (rootdir: /).









share|improve this question




















  • 2





    I think it'd help answerers to see sample lines from the input file

    – Jeff Schaller
    yesterday











  • I agree with Jeff, but also, if it works, it works. Without seeing the input file, it doesn't seem so inelegant to me.

    – Sparhawk
    yesterday











  • The sample line doesn't have 'Updated' in it, is there more to the line?

    – user1794469
    yesterday














1












1








1








For finding a date in a log file I've arrived at the following crude command:



grep Updated /var/log/socklog/xbps/current | tail -n 1 | grep -Eo ^20[0-9][0-9]-[0-9][0-9]-[0-9][0-9]


That does what I need, which is to output the date of the last system upgrade I did on my Void machine: for example 2019-01-24. But it's a pretty cumbersome line and quite crude as well. I assume there must be a more elegant solution to extract the needed date from this file. Incidentally, the date is comprised by the first 10 characters of the line containing the text grep'd for. Any suggestions for a more elegant solution?



This line, btw, is being used to produce a motd so that when I log into my system I'll see something like "last system upgrade 2019-01-24." So I created a script /etc/motd.sh with that line in it, which then gets called from /etc/profile. Am I going about making the information visible in the correct way? What might be some other alternatives for doing what I want?



The script /etc/motd.sh looks like:



#!/bin/bash
echo last system upgrade $(grep Updated /var/log/socklog/xbps/current | tail -n 1 | grep -Eo ^20[0-9][0-9]-[0-9][0-9]-[0-9][0-9])


PS Here's a sample line from the log file, as requested:



2019-01-15T18:05:51.31699 user.notice: Jan 15 12:05:51 xbps-install: Installed `brotli-1.0.7_1' successfully (rootdir: /).
2019-01-15T18:05:51.35465 user.notice: Jan 15 12:05:51 xbps-install: Updated `xwininfo-1.1.4_2' successfully (rootdir: /).









share|improve this question
















For finding a date in a log file I've arrived at the following crude command:



grep Updated /var/log/socklog/xbps/current | tail -n 1 | grep -Eo ^20[0-9][0-9]-[0-9][0-9]-[0-9][0-9]


That does what I need, which is to output the date of the last system upgrade I did on my Void machine: for example 2019-01-24. But it's a pretty cumbersome line and quite crude as well. I assume there must be a more elegant solution to extract the needed date from this file. Incidentally, the date is comprised by the first 10 characters of the line containing the text grep'd for. Any suggestions for a more elegant solution?



This line, btw, is being used to produce a motd so that when I log into my system I'll see something like "last system upgrade 2019-01-24." So I created a script /etc/motd.sh with that line in it, which then gets called from /etc/profile. Am I going about making the information visible in the correct way? What might be some other alternatives for doing what I want?



The script /etc/motd.sh looks like:



#!/bin/bash
echo last system upgrade $(grep Updated /var/log/socklog/xbps/current | tail -n 1 | grep -Eo ^20[0-9][0-9]-[0-9][0-9]-[0-9][0-9])


PS Here's a sample line from the log file, as requested:



2019-01-15T18:05:51.31699 user.notice: Jan 15 12:05:51 xbps-install: Installed `brotli-1.0.7_1' successfully (rootdir: /).
2019-01-15T18:05:51.35465 user.notice: Jan 15 12:05:51 xbps-install: Updated `xwininfo-1.1.4_2' successfully (rootdir: /).






bash grep motd






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 11 mins ago







MJiller

















asked yesterday









MJillerMJiller

9217




9217








  • 2





    I think it'd help answerers to see sample lines from the input file

    – Jeff Schaller
    yesterday











  • I agree with Jeff, but also, if it works, it works. Without seeing the input file, it doesn't seem so inelegant to me.

    – Sparhawk
    yesterday











  • The sample line doesn't have 'Updated' in it, is there more to the line?

    – user1794469
    yesterday














  • 2





    I think it'd help answerers to see sample lines from the input file

    – Jeff Schaller
    yesterday











  • I agree with Jeff, but also, if it works, it works. Without seeing the input file, it doesn't seem so inelegant to me.

    – Sparhawk
    yesterday











  • The sample line doesn't have 'Updated' in it, is there more to the line?

    – user1794469
    yesterday








2




2





I think it'd help answerers to see sample lines from the input file

– Jeff Schaller
yesterday





I think it'd help answerers to see sample lines from the input file

– Jeff Schaller
yesterday













I agree with Jeff, but also, if it works, it works. Without seeing the input file, it doesn't seem so inelegant to me.

– Sparhawk
yesterday





I agree with Jeff, but also, if it works, it works. Without seeing the input file, it doesn't seem so inelegant to me.

– Sparhawk
yesterday













The sample line doesn't have 'Updated' in it, is there more to the line?

– user1794469
yesterday





The sample line doesn't have 'Updated' in it, is there more to the line?

– user1794469
yesterday










2 Answers
2






active

oldest

votes


















2














If the date is first 10 characters, then you can use cut instead of the second grep



tail -n 1 | cut -c1-10





share|improve this answer








New contributor




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




























    0














    I'd probably use awk. Split fields at the T in the datestamp, look for Updated, and print the last occurrence.



    awk -FT '/Updated/ {date=$1} END {print date}' /var/log/socklog/xbps/current


    You can interpolate that directly, or take the slightly more readable approach



    #!/bin/bash
    #
    updated=$(awk -FT '/Updated/ {date=$1} END {print date}' /var/log/socklog/xbps/current)
    [[ -n "$updated" ]] && printf "Last system upgrade %sn" "$updated"





    share|improve this answer
























    • This definitely looks more elegant than the multiple greps + tail that I came up with. Think I'll go ahead and mark it as the best answer.

      – MJiller
      10 mins ago











    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%2f496579%2fexcise-recent-date-from-log-file-output-as-motd%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









    2














    If the date is first 10 characters, then you can use cut instead of the second grep



    tail -n 1 | cut -c1-10





    share|improve this answer








    New contributor




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

























      2














      If the date is first 10 characters, then you can use cut instead of the second grep



      tail -n 1 | cut -c1-10





      share|improve this answer








      New contributor




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























        2












        2








        2







        If the date is first 10 characters, then you can use cut instead of the second grep



        tail -n 1 | cut -c1-10





        share|improve this answer








        New contributor




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










        If the date is first 10 characters, then you can use cut instead of the second grep



        tail -n 1 | cut -c1-10






        share|improve this answer








        New contributor




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









        share|improve this answer



        share|improve this answer






        New contributor




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









        answered yesterday









        Ilia GilmijarowIlia Gilmijarow

        1212




        1212




        New contributor




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





        New contributor





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






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

























            0














            I'd probably use awk. Split fields at the T in the datestamp, look for Updated, and print the last occurrence.



            awk -FT '/Updated/ {date=$1} END {print date}' /var/log/socklog/xbps/current


            You can interpolate that directly, or take the slightly more readable approach



            #!/bin/bash
            #
            updated=$(awk -FT '/Updated/ {date=$1} END {print date}' /var/log/socklog/xbps/current)
            [[ -n "$updated" ]] && printf "Last system upgrade %sn" "$updated"





            share|improve this answer
























            • This definitely looks more elegant than the multiple greps + tail that I came up with. Think I'll go ahead and mark it as the best answer.

              – MJiller
              10 mins ago
















            0














            I'd probably use awk. Split fields at the T in the datestamp, look for Updated, and print the last occurrence.



            awk -FT '/Updated/ {date=$1} END {print date}' /var/log/socklog/xbps/current


            You can interpolate that directly, or take the slightly more readable approach



            #!/bin/bash
            #
            updated=$(awk -FT '/Updated/ {date=$1} END {print date}' /var/log/socklog/xbps/current)
            [[ -n "$updated" ]] && printf "Last system upgrade %sn" "$updated"





            share|improve this answer
























            • This definitely looks more elegant than the multiple greps + tail that I came up with. Think I'll go ahead and mark it as the best answer.

              – MJiller
              10 mins ago














            0












            0








            0







            I'd probably use awk. Split fields at the T in the datestamp, look for Updated, and print the last occurrence.



            awk -FT '/Updated/ {date=$1} END {print date}' /var/log/socklog/xbps/current


            You can interpolate that directly, or take the slightly more readable approach



            #!/bin/bash
            #
            updated=$(awk -FT '/Updated/ {date=$1} END {print date}' /var/log/socklog/xbps/current)
            [[ -n "$updated" ]] && printf "Last system upgrade %sn" "$updated"





            share|improve this answer













            I'd probably use awk. Split fields at the T in the datestamp, look for Updated, and print the last occurrence.



            awk -FT '/Updated/ {date=$1} END {print date}' /var/log/socklog/xbps/current


            You can interpolate that directly, or take the slightly more readable approach



            #!/bin/bash
            #
            updated=$(awk -FT '/Updated/ {date=$1} END {print date}' /var/log/socklog/xbps/current)
            [[ -n "$updated" ]] && printf "Last system upgrade %sn" "$updated"






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 15 hours ago









            roaimaroaima

            43.8k555118




            43.8k555118













            • This definitely looks more elegant than the multiple greps + tail that I came up with. Think I'll go ahead and mark it as the best answer.

              – MJiller
              10 mins ago



















            • This definitely looks more elegant than the multiple greps + tail that I came up with. Think I'll go ahead and mark it as the best answer.

              – MJiller
              10 mins ago

















            This definitely looks more elegant than the multiple greps + tail that I came up with. Think I'll go ahead and mark it as the best answer.

            – MJiller
            10 mins ago





            This definitely looks more elegant than the multiple greps + tail that I came up with. Think I'll go ahead and mark it as the best answer.

            – MJiller
            10 mins ago


















            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%2f496579%2fexcise-recent-date-from-log-file-output-as-motd%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

            サソリ

            広島県道265号伴広島線

            Setup Asymptote in Texstudio