How to test each line in a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to test each line in a file
# 1  
Old 12-13-2011
How to test each line in a file

Hi guys,

I have a file which contains like this:
Code:
Command was launched from partition 0.


------------------------------------------------
Executing command in server server6
Event Text                     Mib                Trap Status


------------------------------------------------
Executing command in server server3
Event Text                     Mib                Trap Status


------------------------------------------------
Executing command in server backup-server
Event Text                     Mib                Trap Status


------------------------------------------------
Executing command in server server1
Event Text                     Mib                Trap Status

I want to test each line if there is/are errors, error/s would appear like this below:
Code:
Executing command in server server1
Event Text                     Mib                Trap Status
101 <details>

I want a script (perl or shell) that will check each line of the file, then if error is found (e.g. 101 <details>) it will return an output something like this:
Code:
Error detected = 101 <details> in server$number (e.g: in server1)

but if not it will return
Code:
system is ok

ty Smilie
# 2  
Old 12-13-2011
What defines an error? A specific number, or any non-blank line in a server 'record'?

Assuming errors are anything that starts with a number, you could use:
Code:
awk '
/Executing command in server/ {curr_server=$NF} 
/^[0-9]/ { printf ("Error %s in %s\n", $0, curr_server);got_err=1 }
END {if (got_err!=1) {print "OK"}}' 1.txt


Last edited by CarloM; 12-13-2011 at 11:58 AM..
# 3  
Old 12-13-2011
You can start with that:
Code:
perl -0nle 'print grep /^\d/m,split /--+/;' file

# 4  
Old 12-14-2011
Thanks CarloM,

Your command works! Btw, would appreciate more if you could give a brief explanation of your code/command.


Thanks again,
rymnd_12345

---------- Post updated at 02:07 AM ---------- Previous update was at 01:35 AM ----------

Hi CarloM,

I would like to seek your assistance again on this matter Sir. I have a file that looks like this:

Code:
Command was launched from partition 0.


------------------------------------------------
Executing command in server server6
System: active
Monitor: running
Modules: running (92/92)


------------------------------------------------
Executing command in server server3
System: active
Monitor: running
Modules: running (92/92)


------------------------------------------------
Executing command in server backup-server
System: active
Monitor: running
Modules: running (97/97)


------------------------------------------------
Executing command in server server1
System: active
Monitor: running
Modules: running (92/92)

I want a simple code that will check each line of the file. Let's say for example for server1, System = active (will prompt an error if not active), Monitor = running (will prompt an error if not running) and Modules = running (n/n (e.g 92/92)) (it will prompt an error if modules is not running and numerator is not equal to denominator (e.g 90/92).

Hope you could help me again.


Br,
rymnd_12345

---------- Post updated at 04:01 AM ---------- Previous update was at 02:07 AM ----------

Hi CarloM/All,

I have a file which looks like this

Code:
------------------------------------------------
Executing command in server server6
System: active
Monitor: running
Modules: running (92/92)


------------------------------------------------
Executing command in server server3
System: active
Monitor: running
Modules: running (92/92)


------------------------------------------------
Executing command in server backup-server
System: inactive
Monitor: stopped
Modules: running (90/97)

And here is my code, but I cannot print the "Modules: running (with not equal count, e.g is (90/97))"

Code:
#!/usr/bin/sh
awk '
/Executing command in server/ {curr_server=$NF}
/inactive/ { printf ("%s in %s\n", $0, curr_server);got_err=1 }
/stopped/ { printf ("%s in %s\n", $0, curr_server);got_err2=1 }
END {if (got_err!=1 && got_err2!=1) {print "SYSTEM Status = OK"}}' /tmp/SYSTEM_Output.debug

Question is, how can insert in the code lets say if it monitored something in the line
Code:
Modules: running (90/97)

As you can see 7 processes are not running, how to print this lines with lacking processes only.

Please help!


Br,
rymnd_12345
# 5  
Old 12-14-2011
Try this:
Code:
[user@host2: /tmp] awk -f 1.awk 1.txt
System: inactive in backup-server
Monitor: stopped in backup-server
90 of 97 modules running in backup-server
[user@host2: /tmp] cat 1.awk
/Executing command in server/ {
   curr_server=$NF;
}
/inactive/ {
   printf ("%s in %s\n", $0, curr_server);
   got_err=1;
}
/stopped/ {
   printf ("%s in %s\n", $0, curr_server);
   got_err2=1;
}
/^Modules/ {
   split($3, counts, "\\(|/|\\)");
   if (counts[2] != counts[3]) {
      printf ("%d of %d modules running in %s\n", counts[2], counts[3], curr_server);
   }
}
END {
   if (got_err!=1 && got_err2!=1) {
      print "SYSTEM Status = OK";
   }
}

split($3, counts, "\\(|/|\\)") creates the counts[] array by splitting the 3rd field using separators '(', ')' and '/' - you may not need both the backslashes, depending on which awk you have.
# 6  
Old 12-14-2011
Thanks again CarloM..! Smilie

BR,
rymnd_12345

---------- Post updated at 07:49 AM ---------- Previous update was at 06:13 AM ----------

Hi CarloM,

You might want to help me again (pardon I'm really a beginner in programming) Smilie
I have a file which looks like this:
Code:
------------------------------------------------
Executing command in server server5
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda5             4.0G  346M  3.5G  10% /
devtmpfs               24G  268K   24G   1% /dev
tmpfs                  24G     0   24G   0% /dev/shm
/dev/sda9             9.9G  195M  9.2G   3% /home
/dev/sda10            109G   54G   49G  53% /opt
/dev/sda8              20G  173M   19G   1% /tmp
/dev/sda6              20G  2.5G   17G  14% /usr
/dev/sda7             109G  517M  103G   1% /var


------------------------------------------------
Executing command in server server2
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda5             4.0G  346M  3.5G  10% /
devtmpfs               24G  268K   24G   1% /dev
tmpfs                  24G     0   24G   0% /dev/shm
/dev/sda9             9.9G  195M  9.2G   3% /home
/dev/sda10            109G   44G   60G  42% /opt
/dev/sda8              20G  173M   19G   1% /tmp
/dev/sda6              20G  2.5G   17G  14% /usr
/dev/sda7             109G  488M  103G   1% /var


------------------------------------------------
Executing command in server server0
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda5             4.0G  346M  3.5G  10% /
devtmpfs               24G  276K   24G   1% /dev
tmpfs                  24G     0   24G   0% /dev/shm
/dev/sda9             9.9G  239M  9.2G   3% /home
/dev/sda10            109G   47G   57G  45% /opt
/dev/sda8              20G  173M   19G   1% /tmp
/dev/sda6              20G  2.5G   17G  14% /usr
/dev/sda7             109G  473M  103G   1% /var

I want to check each line this time particularly the Use% if it has reached already or equal to the threshold that we've set which is 85%, if it is equal or more than the set threshold, it will prompt the user that
Code:
Use% of /dev/sda5 in server curr_server is already 86%

, else output will be Diskspace Status = ok.


Thanks,
rymnd_12345
# 7  
Old 12-14-2011
It's mostly just the same principles.

The main difference is how to identify a filesystem line to process - using the number of fields is probably easiest (NF==6).

You'll also need to remove the percentage from the Use% field before comparing against it. Try using gsub (gsub ("%","",$5);).
 
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find Data in test file and write each out to a line

I have a .csv file that has been create from a google form and I need to extract the data from it that has been entered by users. The CSV will have anywhere between 100 and 1000 lines which comprise entr data for a sports carnival A few typical line is shown here to show the problem I have ... (19 Replies)
Discussion started by: kcpoole
19 Replies

2. Shell Programming and Scripting

How do I test the first char of each line in an array

Hi folks, I am self-learning as I can I have a script that has read a file into an array. I can read out each line in the array with the code: for INDEX in {0..$LENGTH} ## $LENGTH was determined at the read in do echo "${data}" done What I need to do is test the first char... (2 Replies)
Discussion started by: Marc G
2 Replies

3. Shell Programming and Scripting

Get last lines of file after last line with word TEST

i need to get least lines of file after last word TEST in file, and send that lines to mail example of file structure: TEST 10.10.2010 jdfjdnjfndjfndnfkdk djfjdnfjkdjkfnjkdfk jdfjdjfnjdjnfjkdnfjk TEST 11.10.2010 jdjfnjdnfdkdfjdfjdnk jdnfjdnjkfndnfjdnfjk fjdnfjkndnfdfnjdnfjk TEST... (6 Replies)
Discussion started by: waso
6 Replies

4. Shell Programming and Scripting

How to check weather a string is like test* or test* ot *test* in if condition

How to check weather a string is like test* or test* ot *test* in if condition (5 Replies)
Discussion started by: johnjerome
5 Replies

5. Shell Programming and Scripting

Test on string containing spacewhile test 1 -eq 1 do read a $a if test $a = quitC then break fi d

This is the code: while test 1 -eq 1 do read a $a if test $a = stop then break fi done I read a command on every loop an execute it. I check if the string equals the word stop to end the loop,but it say that I gave too many arguments to test. For example echo hello. Now the... (1 Reply)
Discussion started by: Max89
1 Replies

6. UNIX for Dummies Questions & Answers

What's wrong with this line: if ${TEST:?} ; then echo empty; fi

I want to print "empty" if $TEST is empty. TEST= if ${TEST:?} ; then echo empty; fi But it doesn't work. What's wrong? (2 Replies)
Discussion started by: meili100
2 Replies
Login or Register to Ask a Question