Search code examples
bashsortingmessage

Show past X days /var/log/messages using Bash?


in BASH I can't think of a good way to do this but I only want to see the past 30 days of entries in /var/log/messages*. The issue to me is how do I do that with just the Month and Day. For example:

Sep 2 14:26:13 <SOME ENTRY>
Sep 4 14:26:13 <SOME ENTRY> 
Sep 9 14:26:13 <SOME ENTRY>
Sep 14 14:26:13 <SOME ENTRY>
etc..

Any ideas ? HELP! ha ha


Solution

  • -- This works --- but ugly --

    -- Print only the searches that meet the date in each loop iteration (i.e last X num days)

    for (( i=0; i<=${MAXSEARCHDAYS}; i++)) ;do 
    egrep $(date --date "now -${i} days" +%b) ${USBFOUND} | grep $(date --date "now -${i} days" +%e) >> ${TEMPFILE} 
    done 
    sort -k1,1M -k2,2n ${TEMPFILE} | uniq >> ${LOGFILE}