Discussion:
grep maillog by date
(too old to reply)
Reindl Harald
2013-05-07 14:03:34 UTC
Permalink
Hi

i would like a grep of all records from the previous
day with "NOQUEUE" in a bash script - how do i get
exactly the format like below from /var/log/maillog
and yesterday?

May 7 12:29:39 mail postfix/smtpd[29696]: NOQUEUE

final goal:
add the output at the bottom a my daily logwatch
Newton Pasqualini Filho
2013-05-07 14:09:50 UTC
Permalink
Use AWK

Like this:

cat /var/log/maillog | awk '{ if ($1=="May" && $2=="7") print $0 }' | grep NOQUEUE

Newton Pasqualini Filho
Post by Reindl Harald
Hi
i would like a grep of all records from the previous
day with "NOQUEUE" in a bash script - how do i get
exactly the format like below from /var/log/maillog
and yesterday?
May 7 12:29:39 mail postfix/smtpd[29696]: NOQUEUE
add the output at the bottom a my daily logwatch
Reindl Harald
2013-05-07 14:15:10 UTC
Permalink
the main question is

a) dynamically
b) ! yesterday ! from the time the script runs

this is intended for a cron-job
Post by Newton Pasqualini Filho
Use AWK
cat /var/log/maillog | awk '{ if ($1=="May" && $2=="7") print $0 }' | grep NOQUEUE
Post by Reindl Harald
Hi
i would like a grep of all records from the previous
day with "NOQUEUE" in a bash script - how do i get
exactly the format like below from /var/log/maillog
and yesterday?
May 7 12:29:39 mail postfix/smtpd[29696]: NOQUEUE
add the output at the bottom a my daily logwatch
Martin Schütte
2013-05-07 14:20:46 UTC
Permalink
exactly the format like below from /var/log/maillog and yesterday?
With GNU date:
fgrep -e "`date -d yesterday +'%b %e'`" /var/log/mail.log | fgrep NOQUEUE
--
Martin
Reindl Harald
2013-05-07 14:24:03 UTC
Permalink
Post by Martin Schütte
exactly the format like below from /var/log/maillog and yesterday?
fgrep -e "`date -d yesterday +'%b %e'`" /var/log/mail.log | fgrep NOQUEUE
perfect - thank you very much!
DTNX Postmaster
2013-05-07 14:30:18 UTC
Permalink
Post by Reindl Harald
the main question is
a) dynamically
b) ! yesterday ! from the time the script runs
this is intended for a cron-job
Things like;

==
$ date -d yesterday
Mon May 6 16:20:20 CEST 2013

$ date -d yesterday "+%Y%m%d"
20130506
==

Add 'dateext' to your logrotate.conf, which results in logfiles with names like;

"auth.log-20130506.gz"

If you rotate daily, of course. Another option is using rsyslog, which can quite easily create logs in a /year/month/day/ directory structure, automatically generated. No need to rotate logs that way. It can also use more precise timestamps, like;

"2013-05-07T16:19:57.422297+02:00"

This is what we use, and it makes search yesterday's logs as easy as cd'ing into the right directory, and grepping to your heart's content, with something like this;

==
#!/bin/bash
#
#

DATESTRING="$( date -d yesterday +"%Y/%m/%d" )"
cd /var/log/${DATESTRING} || exit 1

for LOGFILE in *.log*; do
zgrep -h 'NOQUEUE' ${LOGFILE}
done | sort
==

Adjust as required, YMMV, and so on.

HTH,
Jona

--
Post by Reindl Harald
Post by Newton Pasqualini Filho
Use AWK
cat /var/log/maillog | awk '{ if ($1=="May" && $2=="7") print $0 }' | grep NOQUEUE
Post by Reindl Harald
Hi
i would like a grep of all records from the previous
day with "NOQUEUE" in a bash script - how do i get
exactly the format like below from /var/log/maillog
and yesterday?
May 7 12:29:39 mail postfix/smtpd[29696]: NOQUEUE
add the output at the bottom a my daily logwatch
Loading...