1

Apologies straight away for bad-styled-question-and-heading.....learning unix and its command these days, m trying to figure out a solution for below scenario :

Lets say i have a log file which looks like :

 01-02-1988     12:00:00    I start 
 01-02-1988     12:00:01    I start
 01-02-1988     12:00:02    I start 
 01-02-1988     12:00:03    I start
 01-02-1988     12:00:04    I start 
 01-02-1988     12:00:05    I start
 01-02-1988     12:00:06    I start 
 01-02-1988     12:00:07    I start
 01-02-1988     12:00:08    I start 
 01-02-1988     12:00:09    I start
 01-02-1988     01:00:10    I start

and i take a time bracket from user for which he wants to see the status.To filter out all the process, i can do :

egrep $usr_time log.txt

But is there any way i can do the same for a particular time period, like awk, can match the value for me for $2 here...but how to compare it for seconds(ss in hh:mm:ss)???

Like for example : assume i want records from 12:00:01 - 12:00:06, so basically what i would need is to extract 3rd value from : delimiter and compare it....how can this be achieved....please help!!!!

  • The last line should be the first. The sorting is wrong. – Hauke Laging Feb 12 '14 at 18:46
  • @HaukeLaging : its just a template log file...not the real one, i showed just to get visitors familiar with the format of log file...plus its in 12hrs format, not 24hr!! :) – NoobEditor Feb 12 '14 at 18:53
2

If you can be sure that the entries are in time order then you can use both times (if both exist in the file!) as start and stop addresses:

start cmd:> sed -n -e /12:00:03/,/12:00:09/p file
01-02-1988     12:00:03    I start
01-02-1988     12:00:04    I start 
01-02-1988     12:00:05    I start
01-02-1988     12:00:06    I start 
01-02-1988     12:00:07    I start
01-02-1988     12:00:08    I start 
01-02-1988     12:00:09    I start

If it is possible that the start and stop time do not appear in the file then you have to compare (it may be necessary to include the date in this calculation)

awk -v starttime="12:00:03" -v stoptime="12:00:09" \
  'BEGIN {FS=":"; $0=starttime; startts=$1*3600+$2*60+$3; '\
  '$0=stoptime; stopts=$1*3600+$2*60+$3; FS=" ";}; ' \
  '{line=$0; FS=":"; $0=$2; secs=$1*3600+$2*60+$3; if (secs>stopts) exit; '\
  'if (secs>=startts) print line; FS=" ";}' file

The strings can even be compared directly:

awk -v starttime="12:00:03" -v stoptime="12:00:09" \
  '{line=$0; if ($2>stoptime) exit; '\
  'if ($2>=starttime) print line; FS=" ";}' file
  • this is much cleaner as i dont need to cmpare in seconds but m generating the log from sql, so start and end time are not surely present in the log, any workarounds....when time bounds are missing from log file??? – NoobEditor Feb 12 '14 at 18:38
  • @NoobEditor That makes the solution "a bit" more complicated... – Hauke Laging Feb 12 '14 at 18:56
  • haha... your 'bit' is rocket-science for me fella!!! :) – NoobEditor Feb 12 '14 at 18:57
  • @NoobEditor And that doesn't even cover AM/PM yet. You may accept the answer now. – Hauke Laging Feb 12 '14 at 18:59
  • is there anything like comparing time which can do something like start_time > user_start_time && end_time <= user_end_time – NoobEditor Feb 12 '14 at 19:05
2

Assuming the contents are in a file x.txt, you can achieve this as follows:

awk ' $2~/12:00:0[1-6]/ { print }' x.txt

The expression $2~/12:00:0[1-6]/ will evaluate the second column against provided regex and the print statement will print all matching rows. If you want to print just the second column of each row, you can do print $2 and so on.

To include shell variables in awk, use -v, eg:

ss=1 es=6 awk -v reg=12:00:0[$ss-$es] ' $2 ~ reg { print }' x.txt

I suggest to check out this beautifully written awk user documentation : http://www.gnu.org/software/gawk/manual/gawk.html

  • assuming i take start and end second from user in ss and es variables,awk '$2~/12:00:[$ss-$es]/ {print;} throws and error of awk: fatal: Invalid range end: /12:00:[$ss-$es]/ – NoobEditor Feb 12 '14 at 18:13
  • prompt > $ ss=1 es=6 awk -v reg=12:00:0[$ss-$es] ' $2 ~ reg { print }' x.txt From awk user manual: gnu.org/software/gawk/manual/html_node/… – mkc Feb 12 '14 at 18:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.