How do you use pull data from multiple lines to do a for statement?

Guys I am having a problem with being able to find missing monitors in a configuration check script I am trying to create for accountability purposes for managing a large number of systems. What I am trying to do is run a script that will look at the raw config data in a file and pull all the pool entries that doesn't have a monitor assigned in it. So that I can fix that error. The file that I am pulling has a ton of information but I am only worried about the pool information so I have made a script that pulls only that needed information into a new file to make it easier to work against. Here is the output of Config Data from that script:

pool Happy1 {
   monitor all happy1-check
   members
      1.1.1.4: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
}

I am having a hard time being able to pull the data from multiple lines to a single line entry as exampled below is what I am looking for since there is no monitor listed in the config entrry for each pool:

-------------------------------------------------------------------------
| Checking for Pools not being monitored...
|
|   Pool Happy2 may not being monitored
|   Pool Happy4 may not being monitored
|
-------------------------------------------------------------------------

Here is what I was using for the older version that was only on a single line so it made it much easier to do the search.

TMPDIR="/var/tmp/monitor"
cd ${TMPDIR}
cd - > /dev/null
CONF1=${TMPDIR}/config/test.txt
echo "-------------------------------------------------------------------------"
echo "| Checking for Pools not being monitored..."
echo "|"
pool=`grep -i "^pool" ${CONF1} | cut -f2 -d" "`
for i in $pool; do
   monitorcheck=`grep "monitor" ${CONF1}`
   [ "${monitorcheck}" = "" ] || echo "|   Pool $i may not being monitored."
done
echo ${monitorcheck}
echo "|"
echo "-------------------------------------------------------------------------"

Hi, scottzx7rr:

$ cat data
pool Happy1 {
   monitor all happy1-check
   members
      1.1.1.4: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
}

$ awk '/^pool/ {name=$2; getline; if ($1!="monitor") print "Pool " name " may not be monitored"}' data
Pool Happy2 may not be monitored
Pool Happy4 may not be monitored

Take care,
alister

You could convert newlines to spaces, then add newlines back only where you want them:

tr '\n' ' ' < input | sed 's/}/}\n/g' | while read LINE
do
        ...
done

Allister,

Thanks for your assistance. I just found out/remember that some might variate in the line location of the monitor. Some will be line 2 and other might be line 3 or 4. So with the getline that will only return the ones with it on line two. So I am gonna try and find a way using awk for this.

scottzx7rr:

The following code awk and sed solutions look for a line with the first word "monitor" at any point before the closing brace before giving up and deigning it unmonitored. I tested against a slightly modified version of the sample data that you provided in your original post.

$ cat data
pool Happy1 {
   members
   monitor all happy1-check
      1.1.1.4:https
}
pool Happy2 {
   members
      1.1.2.4:https
}
pool Happy3 {
   members
      1.1.3.4:https
   monitor all happy3-check
}
pool Happy4 {
   members
      1.1.4.4:https
}
pool Happy5 {
   monitor all happy5-check
   members
      1.1.5.4:https
}

$ awk '/^pool/ {name=$2; next} $1=="monitor" {name=""} /^\}/ && name { print "Pool " name " may not be monitored"}' data
Pool Happy2 may not be monitored
Pool Happy4 may not be monitored



$######## A sed alternative ########


$ cat monitor.sed 
#n
/^pool/ {
    s/pool \([^ ]*\) .*/\1/; h;
    :next
    n
    /^}/! {
        /^ *monitor/!b next
        d
    }
    g; s/.*/Pool & may not be monitored/p
}


$ sed -f monitor.sed data
Pool Happy2 may not be monitored
Pool Happy4 may not be monitored

Regards,
Alister

Alister,

Great thx. I think I am missing a open { as I am getting an error trying to run it.

Hmmmm. Copy-pasting your quoted code works fine here. I tested on an OSX 10.4 system using nawk and a winxp cygwin system using gawk.

The O/S I am in is Solaris 10. But I was able to use nawk and it worked perfectly. So thanks a great deal!