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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

awk chaining statement 1

Status
Not open for further replies.

1in10

Technical User
Jun 29, 2014
8
BR
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
Code:
awk 'myarr["June"]=$4, NR~/^1$]/  NR~/^4320$/; END {print myarr["June"]};' database.txt
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?
Code:
awk 'myarr["June"]=$4, NR~/^1$]/  NR~/^4320$/; END {print myarr[" "]};' database.txt
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.
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
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?
 


Here is something you could use to start with:
Code:
==> cat m0
CRMM=`date +%m`
awk -v mm=$CRMM 'BEGIN{ mth=substr("JanFebMarAprMayJunJulAugSepOctNovDec",(mm-1)*3+1,3)}
substr($4,1,3)==mth { print $0 }
' database.txt

==> ./m0
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
[3eyes]



----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Thanks a ton, for I am about to start with that regex and match pattern. Therefore this is really helpful, because until now all my wisdom let me to a if-else-if-ladder. Regards and thank you again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top