tags:

views:

31

answers:

3

I have a configuration file that has entries for various devices, with each entry separated by a blank line. I need to search the file for all instances of a given device type, and count the number of non-blank lines following the occurrence, stopping at the first blank.

For example:

Server=foo
config line 1
config line 2
config line 3

Server=bar
config line 1
config line 2

Server=foo
config line 1

If I wanted to know how many total "config lines" were associated with server "foo", I should get four. Can you please help?

A: 

Thank you, but I am on AIX 5.3. It doesn't have pcregrep. :( Grep, sed, and awk are all I have access to.

Chris
A: 

This simple awk script will tell you information you need:

#!/usr/bin/awk -f    

$1 ~ /^Server=/ {
    server = $1;
}

($0 != "") && ($1 !~ /^Server=/) {
    count[server] += 1
}

END {
    for (server in count) {
        print server, count[server]
    }
}

You may need to adjust /usr/bin/awk path to match yours. If you place this code in counter script, it and invoke it like this:

./counter < config

It will output following for your example config:

Server=foo 4
Server=bar 2

If you need to get rid of Server= at the beginning of lines, you can pipe it through sed:

./counter < config | sed 's/^Server=//'
thor
A: 

I guess I am total newbie, here. Let me see if I understand you correctly. If the name of my config file is "HoodyHoo", then I would invoke the script like this:

./counter < HoodyHoo

Correct?

Chris