Guys I am having a problem with being able to do a count of entries in a file. What I am trying to get a count of the total number of members that are listed in the files. So I need to pull the number of the lines after members. I tried using sed but it only seems to count the first line(members line) and not the actualy members themselves.
Here is the output of Config Data that I am pulling from:
pool Happy1 {
monitor all happy1-check
members
1.1.1.1:https
1.1.1.2:https
1.1.1.3:https
1.1.1.4:https
1.1.1.5:https
1.1.1.6:https
}
pool Happy2 {
members
1.1.2.4:https
}
pool Happy3 {
monitor all happy3-check
members
1.1.3.4:https
}
pool Happy4 {
members
1.1.4.4:https
}
pool Happy5 {
monitor all happy5-check
members
1.1.5.4:https
}
So for the above I am looking for it to return a value of 10.
Unfortunately I can't search for anything specific in those lines cause it can be a port(80, 8080, etc...) or service name(https, dns, etc...)
Here is what I was working with and it works except for it pulls the members and the closed bracket } with it also. So I am getting incorrect data. I am not sure how to do it cleanly.
sed -n '/members/,/}/p' $CONF|wc -l
bash-3.00# sed -n '/members/,/}/p' input.txt |wc -l
20
anbu23's grep will work for that sample data, but in case "https" may occur elsewhere, or in case a member need not have "https" in its line, the following may be of use:
$ awk '$1=="members" {m=1;next} $1=="}" {m=0} m {i++} END {print i}' data
10
Assumptions: the list of members begins on the line immediately following a line whose first word is "members" and is terminated by a line beginning with a "}" (with optional leading whitespace).
Alister, that was my assumption till I tried using your awk command. Yeah I found a wrinkle with the way the system writes the configuration file. So one version it is as listed above. Then an older version writes them as single line.
New code:
members
1.1.1.1:https
1.1.1.2:https
1.1.1.3:https
Older Code:
members 1.1.1.1:https
members 1.1.1.2:https
members 1.1.1.3:https
If i understand you correctly, you have two different formats to deal with. perhaps:
$ cat old
members 1.1.1.1:https
members 1.1.1.2:https
members 1.1.1.3:https
$ ./scottzx7rr.sh old
3
$ ### Note that i emptied the Happy2 members section for testing purposes
$ cat new
pool Happy1 {
monitor all happy1-check
members
1.1.1.1:https
1.1.1.2:https
1.1.1.3:https
1.1.1.4:https
1.1.1.5:https
1.1.1.6:https
}
pool Happy2 {
members
}
pool Happy3 {
monitor all happy3-check
members
1.1.3.4:https
}
pool Happy4 {
members
1.1.4.4:https
}
pool Happy5 {
monitor all happy5-check
members
1.1.5.4:https
}
$ ./scottzx7rr.sh new
9
$ cat scottzx7rr.sh
#!/bin/sh
# Try the old format. if egrep finds nothing, then try the new
if ! egrep 'members ([0-9]+\.){3}[0-9]+' "$1"; then
sed -n '/members/,/}/{/members/d;/}/d;p;}' "$1"
fi | wc -l
The egrep doesn't do a strict job of matching an ip address, but you can tighten that up if the code is of any use to you and you think it's required