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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How do you use pull data from multiple lines to do a for statement?
# 1  
Old 01-06-2010
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:
Code:
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:
Code:
-------------------------------------------------------------------------
| 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.

Code:
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 "-------------------------------------------------------------------------"


Last edited by Scott; 01-06-2010 at 03:59 PM.. Reason: Code tags
# 2  
Old 01-06-2010
Hi, scottzx7rr:

Code:
$ 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
# 3  
Old 01-06-2010
You could convert newlines to spaces, then add newlines back only where you want them:
Code:
tr '\n' ' ' < input | sed 's/}/}\n/g' | while read LINE
do
        ...
done

# 4  
Old 01-06-2010
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.
# 5  
Old 01-06-2010
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.


Code:
$ 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

Last edited by alister; 01-06-2010 at 06:52 PM.. Reason: sed script tweak
# 6  
Old 01-06-2010
Alister,

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

Quote:
bash-3.00# awk '/^pool/ {name=$2; next} $1=="monitor" {name=""} /^\}/ && name { print "Pool " name " may not be monitored"}' test2.txt
awk: syntax error near line 1
awk: bailing out near line 1
# 7  
Old 01-06-2010
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Multiple lines of data?

I use this to get 8 random letters: cat /dev/urandom | tr -dc 'A-Z' | fold -w 8 | head -n 1 Result is, WLGFJFZY What I'm trying to do is get 10 lines of random letters, separated by a line and each block having ascending numbers i.e; 00 IWMTDFIM 01 KZZZCHPQ 02 YBTGFHGT 03 (4 Replies)
Discussion started by: jenny-mac
4 Replies

2. Shell Programming and Scripting

Split data into multiple lines

Hi, if i have data like below: Control|AC-00011-CN-2475208 AC-00011-CN-2475211 AC-00007-CN-2475238 AC-00007-CN-2475241 Im getting output in required format as below Control|AC-00011-CN-2475208 Control|AC-00011-CN-2475211 Control|AC-00007-CN-2475238 Control|AC-00007-CN-2475241 using awk... (9 Replies)
Discussion started by: JSKOBS
9 Replies

3. Shell Programming and Scripting

How do I use grep to pull incremental data and send to multiple files?

Hi Everyone, Im currently using the below code to pull data from a large CSV file and put it into smaller files with just the data associated with the number that I "grep". grep 'M053' test.csv > test053.csv Is there a way that I can use grep to run through my file like the example below... (6 Replies)
Discussion started by: TheStruggle
6 Replies

4. Shell Programming and Scripting

Extracting data from multiple lines

Hi All, I am stuck in one step.. I have one file named file.txt having content: And SGMT.perd_id = (SELECT cal.fiscal_perd_id FROM $ODS_TARGT.TIM_DT_CAL_D CAL FROM $ODS_TARGT.GL_COA_SEGMNT_XREF_A SGMT SGMT.COA_XREF_TYP_IDN In (SEL COA_XREF_TYP_IDN From... (4 Replies)
Discussion started by: Shilpi Gupta
4 Replies

5. UNIX for Dummies Questions & Answers

Pull out multiple lines with grep patternfile

Hi, I'm trying to get lines from a file using identifiers in the first two columns. I have used: cat MasterFile.txt | grep -f Pattern.txt and the lines I want display on screen. If I try to put them in a file the file is created but stays empty: cat MasterFile.txt | grep -f Pattern.txt... (14 Replies)
Discussion started by: FGPonce
14 Replies

6. Shell Programming and Scripting

multiple lines inside a case statement

echo "please enter ur choice.. 1. Make a file. 2. Display contents 3. Copy the file 4. Rename the file 5. Delete the file 6. Exit" read choice case $choice in 1 ) echo enter the file name read fname if then echo... (2 Replies)
Discussion started by: gotam
2 Replies

7. Shell Programming and Scripting

Outputting data from multiple lines

Hi guys, looking for a bit of advise, and as I am a complete novice, please excuse the daft questions!! I have a list of events and of which entry looks like this; # # Event 1 # NAME = Event 1 # 12345 : 123 : 1 : 1 : L,1,N : 1,0 : Event # # Event 2 # NAME = Event 2 # 12346... (8 Replies)
Discussion started by: JayC89
8 Replies

8. Shell Programming and Scripting

(sed) parsing insert statement column that crosses multiple lines

I have a file with a set of insert statements some of which have a single column value that crosses multiple lines causing the statement to fail in sql*plue. Can someone help me with a sed script to replace the new lines with chr(10)? here is an example: insert into mytable(id, field1, field2)... (3 Replies)
Discussion started by: jjordan
3 Replies

9. UNIX for Dummies Questions & Answers

Split data into multiple lines

All, I have a requirement where I will need to split a line into multiple lines. Ex: Input: 2ABCDEFGH2POIYUY2ASDGGF2QWERTY Output: 2ABCDEFGH 2POIYUY 2ASDGGF 2QWERTY The data is of no fixed lenght. Only the lines have to start with 2. How can this be done. (5 Replies)
Discussion started by: kingofprussia
5 Replies
Login or Register to Ask a Question