Grep log between range of minutes

grep -i "$(date +%b" "%d )13:4[0-5]" syslog
Returns logs between HH:M[Mx-My], for example, between 13:40 and 13:45.
Sample Output
May 23 13:45:01 tortatil /USR/SBIN/CRON[22944]: (root) CMD (pidof cpulimit || cpulimit -z -p  `pidof amule` -l 24 &)
May 23 13:45:01 tortatil /USR/SBIN/CRON[22947]: (root) CMD (if [ -x /usr/bin/mrtg ] && [ -r /etc/mrtg.cfg ]; then mkdir -p /var/log/mrtg ; env LANG=C /usr/bin/mrtg /etc/mrtg.cfg 2>&1 | tee -a /var/log/mrtg/mrtg.log ; fi

6
2010-05-23 16:30:46

These Might Interest You

  • Useful for grepping an IP range from the maillog. When for instance dealing with a spam-run from a specific IP range, or when errors occur from or to a specific IP-range. In the example above the IP range 183.0.0.0/10 (183.0.0.0 - 183.63.255.255) To grep the IP range 124.217.224.0/19 (124.217.224.0 - 124.217.255.255) from the maillog: egrep '124\.217\.2(2[4-9]|[34][0-9]|5[0-5])' -J /var/log/maillog* NOTE: the location of the maillog may vary based upon operating system and distribution.


    0
    egrep '183\.([0-9]|(1[0-6]|2[0-3]))' -J /var/log/maillog*
    wazigster · 2010-10-17 21:44:57 0
  • cut -f1,2 - IP range 16 cut -f1,2,3 - IP range 24 cut -f1,2,3,4 - IP range 24 Show Sample Output


    0
    netstat -tn | grep :80 | awk '{print $5}'| grep -v ':80' | cut -f1 -d: |cut -f1,2,3 -d. | sort | uniq -c| sort -n
    krishnan · 2012-06-26 08:29:37 0
  • Simple one-liner for scanning a range of hosts, you can also scan a range of ports with Netcat by ex.: nc -v -n -z -w 1 192.168.0.1 21-443 Useful when Nmap is not available:) Range declaration like X..X "for i in {21..29}" is only works with bash 3.0+ Show Sample Output


    9
    for i in {21..29}; do nc -v -n -z -w 1 192.168.0.$i 443; done
    rez0r · 2009-09-25 03:31:29 3

  • -1
    RANGE=`wc -l /usr/share/dict/words | sed 's/^\([0-9]*\) .*$/\1/'`; for i in {1..4}; do let "N = $RANDOM % $RANGE"; sed -n -e "${N}p" /usr/share/dict/words | tr -d '\n'; done; RANGE=100; let "N = $RANDOM % $RANGE"; echo $N
    unixmonkey24597 · 2011-08-16 07:04:57 0

What Others Think

That kind of things cannot be easily adapted to grep range of minutes like : 39-45. Depending on your input, the use of awk may be a wise choice.
Josay · 421 weeks and 3 days ago
Backticks are more harm than good. Learn the way of the Jedi: grep -i "$(date +%b" "%d" ")13:4[0-5]" syslog
atoponce · 421 weeks and 3 days ago
be carefull because sometimes you may have date inside log messages, for example : Nov 23 13:53:16 my_mx postfix/scache[29106]: statistics: start interval Nov 23 13:43:16 so try this : grep -i ^"$(date +%b" "%d" ")13:4[0-5]" syslog
gerard · 343 weeks and 1 day ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: