1

I have a logfile with the following timestamp format

2016-06-02 13:59:58.069

The log I am working with spans about an hour.

2016-06-02 13:05:06.144
2016-06-02 13:05:06.144
2016-06-02 13:05:06.160
2016-06-02 13:05:06.160
2016-06-02 13:05:06.176
2016-06-02 13:05:06.177
.
.
.
.
2016-06-02 14:05:03.033
2016-06-02 14:05:03.034
2016-06-02 14:05:03.034
2016-06-02 14:05:03.084
2016-06-02 14:05:03.096
2016-06-02 14:05:03.112

When I try to use awk to extract out the last 10 minutes I still get the whole file.

awk -vDate=`date -d'now-10 minutes' +%Y/%m/%d:%H:%M:%S` '$1,$2 >   Date {print Date, $0}' logfile.log | less

Awk output:

2016/06/02:08:57:35 2016-06-02 13:05:06.144
. 
.
.
2016/06/02:08:57:35 2016-06-02 14:08:05.214

What am I doing wrong?

1
  • Your input format is "YYYY-MM-DD hh:mm:ss" but your Date variable is "YYYY/MM/DD:hh:mm:ss" - totally different and so the inequality doesn't match. Jun 2, 2016 at 15:14

3 Answers 3

1

Using a different approach. I get the vDate, grep to obtain the line number and tail the file from that line number :

vDate=`date -d'now-10 minutes' '+%Y-%m-%d %H:%M'` 
line=`grep  -n "$vDate" logfile.log | head -1  | cut -d: -f1`
tail -n +${line} logfile.log
1

Try

awk -vDate="`date -d'TZ="UTC" now-10 minutes' +'%Y-%m-%d %H:%M:%S.%3N'`" '$1" "$2 > Date {print Date, $0}'

In the pattern $1,$2 > Date, the comma doesn't mean "join these fields" like it does in the output; instead, it means the pattern is a record range. $1 is a non-empty string, which is always true, so the match starts immediately. You need to put a literal space there to join the fields before comparing: $1" "$2

Next, you can see in your example output that you were setting Date to "2016/06/02:...", while the logs used "2016-06-02 ...". As you are comparing date strings, and / is later than - in ASCII, the test would never succeed. Changing the string format to match exactly (with some added quoting to protect it from the shell) means only the digits will differ.

Edit: Your logfile timestamps are a few hours later than what date -d'now-10 minutes' shows. You can tell date to use the relevant timezone either with an environment variable (TZ=UTC awk ...) or inside the date string 'TZ="UTC" now...'. If it's not UTC, pick the right one from the complete list.

14
  • Its still not working using your updated command. I'm still getting the entire file.
    – user53029
    Jun 2, 2016 at 15:25
  • @user53029 I've put back the print Date, $0 output like you had it - could you show one of the lines that was output but shouldn't be?
    – JigglyNaga
    Jun 2, 2016 at 15:41
  • 2016-06-02 13:40:06.989...this is the first line output when I run the command and redirect to an output file. I ran awk -vDate="date -d'now-10 minutes' +'%Y-%m-%d %H:%M:%S.%3N'" '$1" "$2 > Date {print}' logfile.log > tmp.bak && mv -f tmp.bak logfile_last10. I went back to using only > Date {print}' because it made no difference the other way. I also redirected to a tmp location and moved the contents of that location into a file that should only contain the last 10 minutes.
    – user53029
    Jun 2, 2016 at 15:55
  • I also just | less'ed the actual file and it had the same output.
    – user53029
    Jun 2, 2016 at 15:56
  • @user53029 I meant for you to post the output line that contains both the original timestamp, and the value of Date awk is comparing it to, so I can work out whether it's the date or awk part that is going wrong. Also, could you mention the versions of date and awk?
    – JigglyNaga
    Jun 2, 2016 at 16:00
1

awk can print the lines of a text in a section with start_pattern and end_pattern, use the following syntax:

$ awk '/start_pattern/, /end _pattern/' filename

quote from Linux Shell Scripting Cookbook.

So, you can do it like this:

awk '/2016-06-02 13:55/, /2016-06-02 14:05/' logfile`

You could also writing this to the script file if you want:

START=`date --date='now-10 minutes' '+%Y-%m-%d %H:%M'`
END=`date --date='now' '+%Y-%m-%d %H:%M'`
awk "/$START/, /$END/" logfile
1
  • 1
    Yes but this will only work if the file contains at least one line that matches exactly the start of range e.g. 2016-06-02 13:55. If it doesn't, e.g. if nothing was logged at 13:55 and it only has 13:54 and 13:56 your command won't print anything. Jun 3, 2016 at 10:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .