Reputation: 11
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 3Server=bar
config line 1
config line 2Server=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?
I am on AIX 5.3. It doesn't have pcregrep. :( Grep, sed, and awk are all I have access to.
Upvotes: 1
Views: 201
Reputation: 2284
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=//'
Upvotes: 2