theuniverseisflat
theuniverseisflat

Reputation: 881

Linux Shell Script help Need to parse out data from log file between two words

Hi I'm looking to parse a log file and get the following data out and assign to variable and do an if statement if the condition is met:

Here is the input of the log file:

host123.src_dist.serviceGenerator.srcPrdServicePool.1234_REC.dataStreams.activeStale:0
host123.src_dist.serviceGenerator.srcPrdServicePool.YYY_CONTROL.dataStreams.activeStale: 5
host123.src_dist.serviceGenerator.srcPrdServicePool.TPSG.dataStreams.activeStale: 0
host123.src_dist.serviceGenerator.srcPrdServicePool.LDN_ABC.dataStreams.activeStale: 10
host123.src_dist.serviceGenerator.srcPrdServicePool.SSS_BOR.dataStreams.activeStale: 0

Logic is

if activeStale is > 0   
then 
    echo YYY_CONTROL  >> filenamex
    echo LDN_ABC      >> filenamexz       
endif 

The resulting file is cat filenamex

YYY_CONTROL
LDN_ABC

I can do the if part but not sure how to yank out the service names YYY_CONTROL from the line? srcPrdServicePool.YYY_CONTROL.dataStreams

So the simple question is, how can I yank out the service names from the lines that meet my condition?

Thank you in advance

Upvotes: 1

Views: 589

Answers (4)

Thor
Thor

Reputation: 47099

You can also do this with implicit splitting by setting FS, for example:

awk '$2>0 { FS="."; $0=$0; print $5; FS=":" }' FS=: file

Output:

YYY_CONTROL
LDN_ABC

Upvotes: 0

jaypal singh
jaypal singh

Reputation: 77105

Here is another way with GNU awk:

awk -F: '$2>0 && match($0,/Pool[.]([^.]+)[.]dataStreams/,ary) && $0=ary[1]' file

$ awk -F: '$2>0 && match($0,/Pool[.]([^.]+)[.]dataStreams/,ary) && $0=ary[1]' file
YYY_CONTROL
LDN_ABC

Upvotes: 2

Tom Fenech
Tom Fenech

Reputation: 74615

From your question it sounds like you want to print them to the same file (note that in your pseudocode this is not the case). I would use awk for this:

awk -F':' '$2 > 0 { split($1, a, "."); print a[5]}' file

This tests the value after the colon is greater than 0 and if so, prints the part you are interested in.

bash-4.2$ awk -F':' '$2 > 0 { split($1, a, "."); print a[5]}' file
YYY_CONTROL        
LDN_ABC

Upvotes: 3

Josh Jolly
Josh Jolly

Reputation: 11786

Using awk and cut (assuming your log file is always formatted the same way):

 awk -F: '$NF>0' my_file.log | cut -d. -f5 > filenamex

awk looks at everything after the last colon, if it is greater than 0 then that line is printed. cut splits the line into fields by using . as a delimiter, then looks at the 5th field, which is the value you want.

Upvotes: 1

Related Questions