INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

awk chaining statement

awk chaining statement

(OP)
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

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

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

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?

RE: awk chaining 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

RE: awk chaining statement

(OP)
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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close