How to get the result in such log format using linux tools (awk,sed..) ?
hi guys,
I have a log file which has such format: Code:
Fri May 18 13:13:57 MYT 2012 Code:
Fri May 18 13:13:57 MYT 2012 Aborted_clients 1 Code:
LOG=./mysqlstatus.log Thanks a million for any assist and advice given :) |
Is this ok for you?
Code:
awk ' /MYT/ { date=$0; next } length { print date " " $0 } ' file _________________________________ Happy with solution ... mark as SOLVED If someone helps you, or you approve of what's posted, click the "Add to Reputation" button, on the left of the post. |
Hi,
If you are only interested in the date/time and Aborted_clients parts: Code:
awk '/ MYT / { date=$0 } /Aborted_clients/ { print date, $0 }' infile |
hi,
Thanks for both of your feedback, it's a nice one! I have taken 'druuna' solution, it fits with my need exactly :) |
From another similar log, but this time I would like to calculate the appearance of certain lines, for example:
Code:
Fri May 18 13:13:57 MYT 2012 Code:
Fri May 18 13:13:57 MYT 2012 Aborted_clients 3; Aborted_connects 1 |
Assuming the log file in question looks like the one in post #1:
Code:
awk '/ MYT / { date=$0 } /Aborted_clients/ { abcl=$0 } /Aborted_connects/ { print date, abcl " ; " $1, $2 }' infile Code:
awk '/ MYT / { date=$0 } /Aborted_clients/ { abcl=$0 } /Aborted_connects/ { print date, abcl " ; " $1, $2 }' infile |
hi druuna,
Apology that I didn't make the question clearer. Your solution is in fact not what I want this time. What I need is something a bit different with another format of log like this: Code:
Fri May 18 13:13:57 MYT 2012 Code:
Fri May 18 13:13:57 MYT 2012 Sleep 2; Locked 2 |
Code:
awk '/ MYT / { date=$0; next } /.../ { arr[$1]++; next } { printf date; for ( i in arr ) { printf ", %s %s", i, arr[i] }; printf "\n" } ' filename |
Rather than just using stuff people provide, you should try and understand it. There's a tutorial for AWK here that will help with that.
|
Code:
#!/bin/bash 1 - If a state isn't encountered, nothing instead of 0 will be printed. 2 - the sort part will only work if entries are all from the same day and month (date in examples aren't sane, sorting those will be hard. sorting the arrays from within gawk without losing indices might be possible). Example output: Code:
./blaat PS: I just tried pan64's solution and it seems the sleep/locked states aren't counted correctly. |
Quote:
Code:
awk '/ MYT / { date=$0; next } |
Quote:
Code:
d=`date`; echo $d - `mysql -u root -e 'show status'` | tee -a $LOG Code:
egrep "(MYT|Aborted_clients)" | tr "\n" " " |
Quote:
I don't quite understand the part where you do Code:
/.../ My problem in making awk to read a block of text are: i) I don't know how to make it print the result after every end of the block, as I understand awk is a stream processor, how do we make it aware to only print result at the end of each block? Like for instance, Code:
Fri May 18 13:14:57 MYT 2012 -> read input, date=$0, but don't print druuna's way is using a RS to do that, but I don't quite understand how's pan64 way can make awk aware about the above (i) and (ii). pan64, mind to elaborate more ? Nylex, Thanks for your advice, I do know taking blindly other people stuff is not a good habit, and if you have notice my question in LQ so far, I am not fall into those category. I will try understanding and discuss with the feedback and try to give my own finding. I'm slow in understanding awk even though I did go through many tutorials, hope you guys bear with my weakness in this part. |
Quote:
Code:
i=1;for d in `grep -n ^$ style.log | sed s/:/""/`; do sed -n "$i","$d"p style.log | sort | uniq -c; i=$d; done |
just to see post #9, you should try and understand.
. means any char, so /.../ means 3 chars, with other words it is a line containing at least 3 chars. Otherwise my awk works exactly as you described: first look for MYT to start counting next look for non-empty lines and sum up what found in it last on empty lines print result, reset counter and start over |
All times are GMT -5. The time now is 02:44 PM. |