Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

grep using date 1

Status
Not open for further replies.
Mar 31, 2004
151
US
There is a file which has date in the first three columns.
Mar 30 15:07:30

Can somebody tell how we can grep records within the past 5 hrs/24 hrs?

 
Try and adapt the following ksh script.
The GetDate function is a modified version of the function given by PHV (see faq822-4802).
Code:
#!/usr/bin/ksh

#
# Get arguments
#

File="$1"
MinHours=${2:-5}
MaxHours=${3:-24}

#
# Local function
#

GetDate(){ # GetDate nHours [format]
  typeset -i nHours=$1; format=$2
  typeset -i localOffset
  typeset    sign tz

  localOffset=$(echo $TZ |
      sed 's!^[^-0-9]*\([-0-9]*\).*!\1!')
  (( (localOffset-nHours) >=0 )) && sign="+"
  tz=$(echo $TZ |
      sed 's!^\([^-0-9]*\)\([-0-9]*\)\(.*\)!\1'"$sign$((localOffset-nHours))"'\3!')

  TZ=$tz date $format
}

#
# Main
#

awk '
function TimeStamp(    mm,hms,ts) {
  mm = index("__JanFebMarAprMayJunJulAugSepOctNovDec",$1) / 3;
  if (mm == 0) mm=99;
  hms = $3;
  gsub(/:/,"",hms);
  return sprintf("%02d%02d%06d",mm,$2,hms) 
}
NF>=3 {
  ts = TimeStamp();
  if (ts >= From && ts <= To) print;
} 
' From=$(LANG=C GetDate -$MaxHours '+%m%d%H%M%S')   To=$(LANG=C GetDate -$MinHours '+%m%d%H%M%S')     $File

Jean Pierre.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top