I am not an expert in awk, just a technical user, but since I discovered that it works
much faster on huge databases I want to go on with it.
So excuse me for just being a beginner in that with a question about associative arrays.
The database for my further calculation is the following:
username uptime num-date month
webster 2:05, 29.06.2014 June
webster 2:06, 29.06.2014 June
webster 2:06, 29.06.2014 June
webster 2:07, 29.06.2014 June
webster 2:29, 29.06.2014 June
That means the user named webster, his uptime, the numeric date and the name of the month this year.
For a reason there is the last row with the name of the month, because it helps me to pick just the lines for that specific month for a calculation. So if the month changes, there will be no calculation. To cut a long story short, can anybody help me to fit in that associative array which is a first conception. Finally I want to do it with a "for"-condition or "while" with an else-statement, for no there is no NR with the row $4 named Juni it should stop. Thanks in advance
My conception for the associative array is
That means after reading line 1 up to 4320 from database.txt it should print the lines that contains the name of the month. The total number of NR (4320) or entries would say a user turns on an off his machine many many times. This will be adapted later.
Indeed it does, but there is a last entry with June, that doesn't fit. With this output:
webster 3:46, 29.06.2014 June
webster 3:48, 29.06.2014 June
webster 3:50, 29.06.2014 June
June
There are more lines, these are just the last four ones.
So how to get rid of it?
Like this?
And a second question for the calculation on that. Finally I just want to calculate and print the result, without printing the whole bunge of lines to stdout. Just the results for that month.
on that part. How can I solve this? Can I just put a second and a third awk-statement following that first one? Guess not? Or shall I set ";next}" to the end of that first statement?
much faster on huge databases I want to go on with it.
So excuse me for just being a beginner in that with a question about associative arrays.
The database for my further calculation is the following:
username uptime num-date month
webster 2:05, 29.06.2014 June
webster 2:06, 29.06.2014 June
webster 2:06, 29.06.2014 June
webster 2:07, 29.06.2014 June
webster 2:29, 29.06.2014 June
That means the user named webster, his uptime, the numeric date and the name of the month this year.
For a reason there is the last row with the name of the month, because it helps me to pick just the lines for that specific month for a calculation. So if the month changes, there will be no calculation. To cut a long story short, can anybody help me to fit in that associative array which is a first conception. Finally I want to do it with a "for"-condition or "while" with an else-statement, for no there is no NR with the row $4 named Juni it should stop. Thanks in advance
My conception for the associative array is
Code:
awk 'myarr["June"]=$4, NR~/^1$]/ NR~/^4320$/; END {print myarr["June"]};' database.txt
Indeed it does, but there is a last entry with June, that doesn't fit. With this output:
webster 3:46, 29.06.2014 June
webster 3:48, 29.06.2014 June
webster 3:50, 29.06.2014 June
June
There are more lines, these are just the last four ones.
So how to get rid of it?
Code:
awk 'myarr["June"]=$4, NR~/^1$]/ NR~/^4320$/; END {print myarr[" "]};' database.txt
And a second question for the calculation on that. Finally I just want to calculate and print the result, without printing the whole bunge of lines to stdout. Just the results for that month.
Code:
awk '{max = 0} {if ($2>max) max=$2} END {OFMT="%6f"; ORS ":"; print max};' database.txt #maximum
awk '{sum=sum+$2} END {OFMT="%-7.4f\n"; ORS ":"; print sum/NR};' database.txt #average