The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Sed/Awk Help needed liketheshell Shell Programming and Scripting 1 07-14-2008 02:43 AM
help with sed needed arushunter Shell Programming and Scripting 8 06-25-2007 12:10 PM
Scp Help Needed !!!! scooter17 UNIX for Dummies Questions & Answers 3 09-20-2006 01:50 PM
Sed help needed stevefox Shell Programming and Scripting 5 12-05-2005 01:44 AM
SED help needed stevefox Shell Programming and Scripting 8 11-16-2005 09:21 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-27-2009
mglenney mglenney is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 85
Awk help needed

I have a log file monitor script that checks through a log file for a string. I use awk to search the log file, starting at the last checked line, for the specified string and then output the count and the last row number checked. The part of the script that does all the work is here:


Code:
set -- $(awk -v lines=$lineslastrun -v pattern="$searchpattern" '
  BEGIN { count=0 }
  NR > lines && $0 ~ pattern { count++ }
  END { print NR; print count }
' $logfile)
totallines=$1
matches=$2

This particular log is a java app server log and we're looking for the occurance of "Full GC". Now, they want to only count the Full GC if the amount of recovered space is less than 100000. This is what the total output of a Full GC looks like in the log:


Code:
844174.631: [Full GC 844174.631: [ParNew
Desired survivor size 13402112 bytes, new threshold 15 (max 15)
- age   1:    1365536 bytes,    1365536 total
- age   2:     122888 bytes,    1488424 total
- age   3:     225712 bytes,    1714136 total
- age   4:     320592 bytes,    2034728 total
- age   5:       8368 bytes,    2043096 total
- age   6:     483952 bytes,    2527048 total
- age   7:      28256 bytes,    2555304 total
- age   8:      63768 bytes,    2619072 total
- age   9:     106576 bytes,    2725648 total
- age  10:      11960 bytes,    2737608 total
- age  11:        344 bytes,    2737952 total
- age  12:     111824 bytes,    2849776 total
- age  13:     105072 bytes,    2954848 total
- age  14:     189000 bytes,    3143848 total
- age  15:       1544 bytes,    3145392 total
: 212035K->3096K(235968K), 0.0180000 secs] 2079826K->1871065K(2333120K), 0.0181290 secs]

I have bolded the 2 numbers I need to calculate. If the first number minus the second number is less than 100000 then I want to advance the count.

I'm stuck. I've created a mock log and messed around a bit and only came up with this:


Code:
awk '/Full GC/,/^:/ {if (/^:/) print $5}' testawk.data | awk -F \( '{ print $1 }' | tr -d '\-\>' | tr 'K' ' ' | awk '{if ($1-$2<100000) print}'

I can figure out how to get that into my set command above and get it to output the count. I can't figure out how I'll output the last line number of the log that was checked. I'm hoping someone can take the time to show me how to do this properly.

Thanks
  #2 (permalink)  
Old 10-27-2009
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
start with this:

Code:
echo ': 212035K->3096K(235968K), 0.0180000 secs] 200000K->90K(2333120K), 0.0181290 secs]' | nawk '{n=split($5, a, "[->(]"); print (a[1]-a[3]<100000)? "less" : "NOTless"}'

  #3 (permalink)  
Old 10-28-2009
mglenney mglenney is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 85
Thanks for the nudge in the right direction. First off, I didn't know I could perform the calculation without removing the "K" from each number. That certainly saved some steps. Besides that, your rewrite allows me to fit it all in a single awk which makes outputting the number of lines easy. Here's what I came up with:


Code:
awk 'BEGIN { count=0 } NR > 10 && /Full GC/,/^:/ {if (/^:/) split($5, a, "[->(]"); else next; if (a[1]-a[3]<100000) count++} END { print NR; print count }' testawk.data

The 'NR > 10' will actually be 'NR > lines' once I incorporate it into the script. You can see it in my first post. This allows me to skip over log file entries checked in previous runs of the script.

Thanks again

---------- Post updated 10-28-09 at 04:07 PM ---------- Previous update was 10-27-09 at 06:25 PM ----------

All is working well except I can't seem to pass the search string to awk. Here's the snippit:


Code:
set -- $(awk -v lines=$lineslastrun -v pattern="$searchpattern" '
  BEGIN { count=0 }
  NR > lines && /Full GC/,/^:/ {if (/^:/) split($5, a, "[->(]"); else next; if (a[1]-a[3]<100000) count++}
  END { print NR; print count }
' $logfile)
totallines=$1
matches=$2

I would like to change "Full GC" to "pattern" so I can pass the string as a variable to awk. Unfortunately it only works if I put in as it is above. I have tried many versions of single and double quotes:


Code:
NR > lines && /pattern/,/^:/ {if (/^:/) split($5, a, "[->(]"); else next; if (a[1]-a[3]<100000) count++}
NR > lines && "/pattern/",/^:/ {if (/^:/) split($5, a, "[->(]"); else next; if (a[1]-a[3]<100000) count++}
NR > lines && /"pattern"/,/^:/ {if (/^:/) split($5, a, "[->(]"); else next; if (a[1]-a[3]<100000) count++}

Plus many more. None worked. Any suggestions?
  #4 (permalink)  
Old 10-29-2009
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131

Code:
$0 ~ pattern

  #5 (permalink)  
Old 10-29-2009
mglenney mglenney is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 85
That's what I had before when I was just looking for 1 line. Now I'm looking for a block of text.
  #6 (permalink)  
Old 10-29-2009
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131

Code:
NR > lines && $0 ~ pattern,/^:/

  #7 (permalink)  
Old 10-29-2009
mglenney mglenney is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 85
Ok. That did it. Working perfectly. Thank you.
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:01 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0