Sed command

I have a file a.txt which contains the date and time.
22/Aug/2010:02:03:00
21/Aug/2010:00:00:00
21/Aug/2010:00:02:00
22/Aug/2010:00:00:00
22/Aug/2010:01:00:00
22/Aug/2010:02:00:01
22/Aug/2010:03:00:00

How to get the lines greater than or equal to 21\/Aug\/2010:06:00:00 and less than or equal 22\/Aug\/2010:09:00:00.

sort -k1 a.txt | sed -n "/\[21\/Aug\/2010:06:00:00/,/\[22\/Aug\/2010:09:00:00/p" > b.txt

awk:

sort a.txt | awk '$0>="21/Aug/2010:06:00:00"; $0>="22/Aug/2010:09:00:00" { print; exit }'

Try this,

 sort inputfile | sed -n '/22\/Aug\/2010:06:00:00/,/22\/Aug\/2010:09:00:00/p'
67.162.10.216   -       -       [21/Aug/2010:00:00:00   +0000]  GET /2010-08-18/news/ct-met-barrington-student-death-20100818_1_mental-illness-suicide-prevention-teen-suicides HTTP/1.1        200     6826 
67.162.10.216   -       -       [22/Aug/2010:01:00:00   +0000]  GET /tracker.js.php?45aa01ed37b58d2a537b1ba12bb97fe2e5695a8c HTTP/1.1   200     2915
67.162.10.216   -       -       [22/Aug/2010:02:00:00   +0000]  GET /tracker.js.php?45aa01ed37b58d2a537b1ba12bb97fe2e5695a8c HTTP/1.1   200     2882
66.249.71.98    -       -       [22/Aug/2010:00:04:00   +0000]  GET /ad-openx.php?out=js&d=mod-top-hdr-defer&z-i=24809&z-n=top-leaderboard&i-w=728&i-h=90&i-e=pi%3D45%26amp%3Btv%3Dkp-CT1-G%26amp%3Bpm_mode%3Dp&i-s=pgtp%3Dkeyword%26pi%3D45%26pe_id%3Dcarrot-cake%26tn%3Dnone%26tv%3Dkp-CT1-G HTTP/1.1 200     1020

how to sort the file based on timestamp, How to get the lines greater than or equal to 21\/Aug\/2010:06:00:00 and less than or equal 22\/Aug\/2010:09:00:00
sort -k4 a.txt | sed -n "/\[21\/Aug\/2010:06:00:00/,/\[22\/Aug\/2010:09:00:00/p" > b.txt

Post your input file and expected output.

awk '
BEGIN{
 m["Jan"]="01"
 m["Feb"]="02"
 m["Mar"]="03"
 m["Apr"]="04"
 m["May"]="05"
 m["Jun"]="06"
 m["Jul"]="07"
 m["Aug"]="08"
 m["Sep"]="09"
 m["Oct"]="10"
 m["Nov"]="11"
 m["Dec"]="12"
 start="20100821060000"
 end="20100822090000"
}
{ split($4,a,":"); sub(/\[/,"",a[1]);split(a[1],b,"/")
  t=b[3] m[b[2]] b[1] a[2] a[3] a[4];
  if (t>start&&t<end) print
}' a.txt

I have a tab delimited file(as mentioned above),which contains the timestamps entries for 2 days,(Aug 21st and Aug 22nd) in which 4th column is the timestamps.
I have to sort the file based on timestamps and extract the lines which is
greater than or equal to 21\/Aug\/2010:06:00:00 and less than or equal 22\/Aug\/2010:09:00:00

my code is base on your latest input file, do you try it?

alternative rdcwayx solution's

# ./justdoit
 
66.249.71.98    -       -       [22/Aug/2010:00:04:00   +0000]  GET /ad-openx.php?out=js&d=mod-top-hdr-defer&z-i=24809&z-n=top-leaderboard&i-w=728&i-h=90&i-e=pi%3D45%26amp%3Btv%3Dkp-CT1-G%26amp%3Bpm_mode%3Dp&i-s=pgtp%3Dkeyword%26pi%3D45%26pe_id%3Dcarrot-cake%26tn%3Dnone%26tv%3Dkp-CT1-G HTTP/1.1 200     1020
67.162.10.216   -       -       [22/Aug/2010:01:00:00   +0000]  GET /tracker.js.php?45aa01ed37b58d2a537b1ba12bb97fe2e5695a8c HTTP/1.1   200     2915
67.162.10.216   -       -       [22/Aug/2010:02:00:00   +0000]  GET /tracker.js.php?45aa01ed37b58d2a537b1ba12bb97fe2e5695a8c HTTP/1.1   200     2882
## justdoit ##
#!/bin/bash
sort a.txt | while read -r l
 do
cond=$(date +"%s" -d "`echo "$l" | awk '{print $4}'  | sed 's/^.//;s/\// /g' | sed 's/\(.*\) \(.*\) [^:]*:\(.*\)/\2 \1 \3/'`")
  xd=$(date +"%s" -d "Aug 21 06:00:00")
  yd=$(date +"%s" -d "Aug 22 09:00:00")
   if [ $cond -ge $xd  ] && [ $cond -le $yd ] ; then
     echo "$l"
   fi
 done

also rdcwayx solution also works charm :slight_smile:
just edit it with sort

awk '
BEGIN{
m["Jan"]="01"
m["Feb"]="02"
m["Mar"]="03"
m["Apr"]="04"
m["May"]="05"
m["Jun"]="06"
m["Jul"]="07"
m["Aug"]="08"
m["Sep"]="09"
m["Oct"]="10"
m["Nov"]="11"
m["Dec"]="12"
start="20100821060000"
end="20100822090000"
}
{ split($4,a,":"); sub(/\[/,"",a[1]);split(a[1],b,"/")
  t=b[3] m[b[2]] b[1] a[2] a[3] a[4];
  if (t>start&&t<end) print
}' a.txt | sort
 
66.249.71.98    -       -       [22/Aug/2010:00:04:00   +0000]  GET /ad-openx.php?out=js&d=mod-top-hdr-defer&z-i=24809&z-n=top-leaderboard&i-w=728&i-h=90&i-e=pi%3D45%26amp%3Btv%3Dkp-CT1-G%26amp%3Bpm_mode%3Dp&i-s=pgtp%3Dkeyword%26pi%3D45%26pe_id%3Dcarrot-cake%26tn%3Dnone%26tv%3Dkp-CT1-G HTTP/1.1 200     1020
67.162.10.216   -       -       [22/Aug/2010:01:00:00   +0000]  GET /tracker.js.php?45aa01ed37b58d2a537b1ba12bb97fe2e5695a8c HTTP/1.1   200     2915
67.162.10.216   -       -       [22/Aug/2010:02:00:00   +0000]  GET /tracker.js.php?45aa01ed37b58d2a537b1ba12bb97fe2e5695a8c HTTP/1.1   200     2882