I have a text file that has a list of dates in it ( see below example) is there i can just pull out the lines that are from this week ( week starting on monday) and then work out the how many occurances there are on each name in collum 2
So, filter for these lines, filter for date range, extract user name and get counts? Shell/sed might be easier, but I am not a PERL fancier. To do the date bit, need input or clock algorythm, your definition of a week, get rid of the hyphens and do integer compare. You can use an associative vector to keep counts or sort|uniq -c.
Just mostly off the cuff, not PERL but correct general approach, not tested, narrative follows:
export lo=20130513 hi=20130519
(IFS="-$IFS"
declare -A cts
while read y m d t u rest
do
ymd="$y$m$d"
case "$ymd" in
([21][09][0-9][0-9][0-1][0-9][0-3][0-9])
if (( ymd <= hi && ymd >= lo ))
then
if (( ++cts[$u] == 1 ))
then
us="$us $u"
fi
fi
;;
(*)
;;
esac
done
for u in $us
do
echo $u $cts[$u]
done
)<input_file >cts_file
Store low and high dates in integer form
Open a subshell to segregate code with funny $IFS including dash. (Hope it does not mess up other things!)
Creat an associative array cts for counts.
Begin reading lines into 6 variables: year, month, day, time, user, rest.
If the line starts with a date, increment/create a count for that user.
If this is the first count, save user names in us.
After reading all lines, dump the cts for all users.
I might have left IFS alone and read the date as d, removed the - to create ymd: ymd=`echo $d | tr -d '[-]'` but thats an exec per line, si I need to read bash man to see how to remove them with builtins. I could have neatened up the input with sed so it was only 'ymd u' lines.
I don't understand.
You want to adjust the date that this code looks for??
You'll have to adjust the date in the input file (data file) itself. Not sure what the point of doing that would be.
You can grep out just one date, or use sed, since sed can quit when it sees a higher date. Sed can find the first desired date, start passing data until it sees the next date or EOF, and quit without passing (printing) that line. You might have to keep 1-2 lines in the buffer so you can detect the exit in time to prevent it being printed. Sed can do this using N and P. I am sure PERL can emulate sed in this even more simply.