unix - Grab Date Range From Text File -


so have simple ksh script on aix machine users @ company use perform tasks. each time use script entry placed in log file containing information such user id, date, , using script for. need can use pull log file, entries last 7 days.

the date placed in log file formatted follows:

d=`date "+%m/%d/%c%y%n. 

the options either ksh93's printf '%(fmt)t or date's date -d fmt available in ast , gnu implementations. can convert epoch seconds , filter log accordingly.

example:

#!/usr/bin/env ksh  typeset -t record=(     integer uid     integer date=0     typeset description )  typeset -t log=(     record -h '(internal) array of log records.' -a records     typeset -h 'path log file' filename     typeset -h 'the date format use printing' fmt=%m/%d/%c%y      function readlog {         if [[ ! ( -f ${_.filename} && -r ${_.filename} ) ]];             printf 'log file: %q not found.\n' "${_.filename}" >&2             return 1         fi          typeset uid date desc         integer n         while ifs=, read -r uid date desc;             _.records[n++]=(uid=uid; date=$(printf '%(%s)t' "$date"); description=$desc)         done <"${_.filename}"     }      function printlastndays {         if [[ $1 != +([[:digit:]]) ]];             printf '%s: must specify positive integer of days\n' "${.sh.fun}" >&2             return 1         fi          integer fromtime idx         printf -v fromtime '%(%s)t' "$1 days ago"         idx in "${!_.records[@]}";             ((_.records[idx].date > fromtime)) || continue             #printf "uid: %d\ndate: %(${_.fmt})t\ndescription: %s\n\n" \             #    "${_.records[idx].uid}" "${_.records[idx].date}" "${_.records[idx].description}"             printf 'uid: %d\ndate: %s\ndescription: %s\n\n' \                 "${_.records[idx].uid}" "$(/bin/date -d "@${_.records[idx].date}" "+${_.fmt}")" "${_.records[idx].description}"         done     } )  function main {     log mylog=(filename=/dev/fd/3)     mylog.readlog &&     mylog.printlastndays 7 }  #typeset -ft main .sh.type.log.readlog .sh.type.log.printlastndays main "$@" 3<<-'eof'         123,07/16/2015,foo         124,07/17/2015,bar         125,07/18/2015,baz         126,07/19/2015,bork eof   # vim: set fenc=utf-8 ff=unix ft=sh noet : 

output:

uid: 124 date: 07/17/2015 description: bar  uid: 125 date: 07/18/2015 description: baz  uid: 126 date: 07/19/2015 description: bork 

printf buggy @ moment printlastndays forces using /bin/date.


Comments