Re: Read lines and compare in a loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Re: Read lines and compare in a loop
# 1  
Old 09-10-2012
Re: Read lines and compare in a loop

I have a file which has following content:

Code:
NAME=ora.DG1.dg
TYPE=ora.diskgroup.type
TARGET=ONLINE
STATE=ONLINE

NAME=ora.DG2.dg
TYPE=ora.diskgroup.type
TARGET=ONLINE
STATE=ONLINE

NAME=ora.DG3.dg
TYPE=ora.diskgroup.type
TARGET=ONLINE
STATE=ONLINE

NAME=ora.DG4.dg
TYPE=ora.diskgroup.type
TARGET=ONLINE
STATE=ONLINE

NAME=ora.LISTENER.lsnr
TYPE=ora.listener.type
TARGET=OFFLINE
STATE=ONLINE

NAME=ora.LISTENER_SCAN2.lsnr
TYPE=ora.scan_listener.type
CARDINALITY_ID=1
TARGET=ONLINE
STATE=ONLINE

I want to print the NAME value where STATE <> TARGET .

i.e for the NAME=ora.LISTENER.lsnr the TARGET value and STATE values are different , so i need to print that NAME

Can some one help to write a loop query in UNIX to achieve above results.

I am new to UNIX, so any help will be appreciated.

-Thanks

Last edited by rcc50886; 09-10-2012 at 12:16 PM.. Reason: Please wrap data and sripts with CodeTags
# 2  
Old 09-10-2012
Have you tried anything yet? If yes could you please post so that we can help you build on that?
# 3  
Old 09-10-2012
Try using a single awk command...its actually sufficient
# 4  
Old 09-10-2012
I am trying to do as follows:

Code:
line_no=1
max_line_no=`cat file_name.txt | wc -l`

if  [[line_no <= max_line_no]]
 then
       range_line_no = line_no + 4
     STATE=`awk 'NR>=$line_no && NR<=$range_line_no'  file_name.txt | tail -1 |awk -F "=" '{print $2}'`
     TARGET=awk 'NR>=$line_no && NR<=$range_line_no'  file_name.txt | tail -2 |head -1| awk -F "=" '{print $2}'`
     if [[ STATE = TARGET ]]
       then
            NAME=`awk 'NR>=$line_no && NR<=$range_line_no'  file_name.txt | head -1 |awk -F "=" '{print $2}'`
             printf "The follwoing resource was not matched: $NAME"
     fi

 line_no=line_no+5;
fi

Is it possible to pass variables into AWK satement ??

Code:
STATE=`awk 'NR>=$line_no && NR<=$range_line_no'  file_name.txt

in the above command , is it possible to pass those variables, i tried but its not working.

-Thanks,
# 5  
Old 09-10-2012
A bit straightforward, but it should do the trick, at least on the sample you provided:
Code:
~/unix.com$ awk -F'=' '/NAME/{n=$2}/TARGET/{t=$2}/STATE/{s=$2}{if(s&&t&&(s!=t)){s="";t="";print n}}' file

Replace the last word file with your data file

Last edited by tukuyomi; 09-10-2012 at 01:05 PM.. Reason: Better safe than sorry
This User Gave Thanks to tukuyomi For This Post:
# 6  
Old 09-10-2012
That was amazing, It worked for me.

Your previous one worked for me but not the updated one.

Last edited by rcc50886; 09-10-2012 at 01:16 PM..
# 7  
Old 09-10-2012
Code:
awk -F"=" '/NAME/{name=$2;} {if($0~/TARGET/){tar=$2;} else if($0~/STATE/){st=$2;if(st!=tar)print name;}}' input_file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reading two lines in a while loop and combining the lines

Dear all, I have a file like this: imput scaffold_0 1 scaffold_0 10000 scaffold_0 20000 scaffold_0 25000 scaffold_1 1 scaffold_1 10000 scaffold_1 20000 scaffold_1 23283 and I want the output like this: scaffold_0 1 scaffold_0 10000 scaffold_0 10000 scaffold_0 20000... (6 Replies)
Discussion started by: valente
6 Replies

2. UNIX for Dummies Questions & Answers

When reading a csv file, counter to read 20 lines and wait for minute then read next 20 till end

Hello All, i am a newbie and need some help when reading a csv file in a bourne shell script. I want to read 10 lines, then wait for a minute and then do a reading of another 10 lines and so on in the same way. I want to do this till the end of file. Any inputs are appreciated ... (3 Replies)
Discussion started by: victor.s
3 Replies

3. UNIX for Dummies Questions & Answers

Read statement within while read loop

hi, this is my script #!/bin/ksh cat temp_file.dat | while read line do read test if ]; then break else echo "ERROR" fi done when i execute this code , the script does wait for the user input . it directly prints "ERROR" and terminates after the no. of times as there... (3 Replies)
Discussion started by: siva1612
3 Replies

4. Shell Programming and Scripting

need to read lines in file and compare value in if not working

Hello, I have this file that sometime contains 0 lines and sometimes 1 or more. It's supposed to then put the result (could be 0 or 1 or 2 or more) into a variable. Then it's supposed to echo using an if else statement depending on the value of the variable. flagvar='wc -l $tempfile |... (1 Reply)
Discussion started by: script_op2a
1 Replies

5. Shell Programming and Scripting

Having a for loop read in lines with spaces?

Is this possible? I have a for loop in a shell script reading a list, but I want each line to be a loop, not each thing with a space. Here is the example: HOSTLIST="\ 1.2.3.4 serverA 1.2.3.5 serverB" for NBUHOST in `echo $HOSTLIST` do ssh ${SERVERNAME} "echo "${NBUHOST}"... (3 Replies)
Discussion started by: LordJezoX
3 Replies

6. Shell Programming and Scripting

Read Two lines in a CSV File and Compare

Hi , I have a CSV file ( file.csv) with some data as below: A,1,abc,x,y,z,,xyz,20100101,99991231 A,1,abc,x,y,z,234,xyz,20100101,99991231 I have to delete the duplicate line based on unique identifiers which are values in the fields- 2,3,4,8.These coulmns in both the rows have same... (6 Replies)
Discussion started by: Sheel
6 Replies

7. Shell Programming and Scripting

Read lines with different lengths in while loop

Hi there ! I need to treat files with variable line length, and process the tab-delimited words of each line. The tools I know are some basic bash scripting and sed ... I haven't got to python or perl yet. So my file looks like this obj1 0.01953 0.34576 0.04418 0.01249 obj2 0.78140... (7 Replies)
Discussion started by: jossojjos
7 Replies

8. Shell Programming and Scripting

Read from a file and compare

i want a shell script to read the a list of numbers from a file and compare with a unique number with the list. FOR EXAMPLE : abc.lst file contains d following 2343 3214 45654 563456 5436 Unique number is 2342 (6 Replies)
Discussion started by: pgmfourms
6 Replies

9. Shell Programming and Scripting

How to use while loop in bash shell to read a file with 4 lines of gap

Hi , I am currently using the while loop in bash shell, as follows. while read line do echo $line done < file.txt However, i want to use the while loop on file.txt, which will read the file with 4 lines of gap. Ex- if file.txt is a file of 100 lines, then i want to use the loop such... (3 Replies)
Discussion started by: jitendriya.dash
3 Replies

10. Shell Programming and Scripting

read a file and compare

hi how can i read a file using the unix script and check for one or more field value for a predefined status/value for example : the file contains the following text Name IP Address/Mask Type Connection Status mgmt-eth0(1) ... (2 Replies)
Discussion started by: aemunathan
2 Replies
Login or Register to Ask a Question