multiline data block parse and CSV data extraction with perl

cat z.log | perl -ne 'BEGIN{ print "DATE;RATE\n"; } /\[(\d.*)\]/ && print $1; /CURRENT RATE: +(\S+) msg.*/ && print ";" .$1 . "\n"; '
extract data in multiline blocks of data with perl pattern matching loop
Sample Output
from file:


@SG: --------------------------------------------------------------------
@SG: [pmphost6] [2012-04-02 21:23:59.268]
@SG: --------------------------------------------------------------------
@SG: [SAFEGUARD STATUS]
@SG:   > RATE THRESHOLD:           30 msg/sec
@SG:   > TIME TO LIVE:           1800 sec
@SG:   > CURRENT RATE:         43.017 msg/sec  2581.000 msg/min
@SG:   > ELAPSED TIME:             60 sec
@SG:   > TIME COUNT:                0 sec
@SG:   > TIME TO LIVE COUNT:      360 sec
@SG:   > SAFEGUARD STATUS:         ON
@SG: --------------------------------------------------------------------
@SG: --------------------------------------------------------------------
@SG: [pmphost6] [2012-04-02 21:24:59.268]
@SG: --------------------------------------------------------------------
@SG: [SAFEGUARD STATUS]
@SG:   > RATE THRESHOLD:           30 msg/sec
@SG:   > TIME TO LIVE:           1800 sec
@SG:   > CURRENT RATE:         42.417 msg/sec  2545.000 msg/min
@SG:   > ELAPSED TIME:             60 sec
@SG:   > TIME COUNT:                0 sec
@SG:   > TIME TO LIVE COUNT:      420 sec
@SG:   > SAFEGUARD STATUS:         ON
@SG: --------------------------------------------------------------------
@SG: --------------------------------------------------------------------
@SG: [pmphost6] [2012-04-02 21:25:59.269]
@SG: --------------------------------------------------------------------
@SG: [SAFEGUARD STATUS]
@SG:   > RATE THRESHOLD:           30 msg/sec
@SG:   > TIME TO LIVE:           1800 sec
@SG:   > CURRENT RATE:         42.833 msg/sec  2570.000 msg/min
@SG:   > ELAPSED TIME:             60 sec
@SG:   > TIME COUNT:                0 sec
@SG:   > TIME TO LIVE COUNT:      480 sec
@SG:   > SAFEGUARD STATUS:         ON
@SG: --------------------------------------------------------------------


estract csv


DATE;RATE
2012-04-02 21:23:59.268;43.017
2012-04-02 21:24:59.268;42.417
2012-04-02 21:25:59.269;42.833

1
By: hute37
2012-04-03 13:52:20

These Might Interest You

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: