check values in columns and print certain error messages if value > 10, 40 , 60


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting check values in columns and print certain error messages if value > 10, 40 , 60
# 1  
Old 04-16-2011
Question check values in columns and print certain error messages if value > 10, 40 , 60

Hi Experts,

I need your assistance to check values in certain columns (5 and 8 %Err) from awk o/p and if the value is > 10 print "<Minor>: <complete row>" , if value > 40 then print "<Major>: <complete row> and If value > 60 then print "<Critical>: <complete row>". and separates the repetition between occurrences by ==================== line.

Example:
Test file:
Code:
$ more test4.log
        Device          |             IPMB0-A              |             IPMB0-B
Board      Address      |Sent      SentErr      %Errr      |Sent      SentErr      %Errr
============================================================================================
Board1;9a;60;36;60.0;60;0;0.0
Board2;96;60;35;58.3;55;0;0.0
Board3;92;60;60;100.0;60;60;100.0
Board4;8e;60;35;58.3;60;0;0.0
        Device          |             IPMB0-A              |             IPMB0-B
Board      Address      |Sent      SentErr      %Errr      |Sent      SentErr      %Errr
============================================================================================
Board1;9a;60;19;31.6;60;0;0.0
Board2;96;60;20;33.3;54;0;0.0
Board3;92;60;60;100.0;60;60;100.0
Board4;8e;60;20;33.3;60;0;0.0

awk script:
Code:
#!/bin/awk -f
BEGIN {
	FS = ";";
}
{
	printf ("%-11s%-14s%-10s%-13s%-11s%-10s%-13s%-11s\n", $1, $2, $3, $4, $5, $6, $7, $8)
}

awk o/p:
$ awk -f awkfile test4.log
Code:
        Device          |             IPMB0-A              |             IPMB0-B
Board      Address      |Sent      SentErr      %Errr      |Sent      SentErr      %Errr
============================================================================================
Board1     9a            60        36           60.0       60        0            0.0
Board2     96            60        35           58.3       55        0            0.0
Board3     92            60        60           100.0      60        60           100.0
Board4     8e            60        35           58.3       60        0            0.0
        Device          |             IPMB0-A              |             IPMB0-B
Board      Address      |Sent      SentErr      %Errr      |Sent      SentErr      %Errr
============================================================================================
Board1     9a            60        19           31.6       60        0            0.0
Board2     96            60        20           33.3       54        0            0.0
Board3     92            60        60           100.0      60        60           100.0
Board4     8e            60        20           33.3       60        0            0.0

Desired O/P:

Code:
Please check Errors in Error.txt

Code:
#more Error.txt
<Major> IPMB0-A : Board1     9a            60        19           31.6       60        0            0.0
<Major> IPMB0-A : Board2     96            60        20           33.3       54        0            0.0 
<critical> IPMB0-A/IPMB0-B : Board3     92            60        60           100.0      60        60           100.0
<Major> IPMB0-A : Board4     8e            60        20           33.3       60        0     0.0       
=====================================
<Major> IPMB0-A : Board1     9a            60        19           31.6       60        0            0.0
<Major> IPMB0-A : Board2     96            60        20           33.3       54        0            0.0 
<critical> IPMB0-A/IPMB0-B : Board3     92            60        60           100.0      60        60           100.0
<Major> IPMB0-A : Board4     8e            60        20           33.3       60        0     0.0


It would be great also if it specifies whether the error is on IPMB0-A or IPMB0-B or both as in the desired o/p example.

Thanks in advance
# 2  
Old 04-16-2011
Try this:
Code:
#!/bin/awk -f
BEGIN {
        FS = ";";
        x=0;
}
NF==1 && x==0{
        x=1;
        split($0,a," ");
        print;
        print "===================================" >> "Error.txt";
        next;
}
NF==1{
        print;
        next;
}
{
        if ($5>=10 && $5<40){printf ("<Minor> %s : %-11s%-14s%-10s%-13s%-11s%-10s%-13s%-11s\n", a[3],$1, $2, $3, $4, $5, $6, $7, $8) >> "Error.txt"};
        if ($5>=40 && $5<60){printf ("<Major> %s : %-11s%-14s%-10s%-13s%-11s%-10s%-13s%-11s\n", a[3],$1, $2, $3, $4, $5, $6, $7, $8) >> "Error.txt"};
        if ($5>=60){printf ("<Critical> %s : %-11s%-14s%-10s%-13s%-11s%-10s%-13s%-11s\n", a[3],$1, $2, $3, $4, $5, $6, $7, $8) >> "Error.txt"};
        if ($8>=10 && $8<40){printf ("<Minor> %s : %-11s%-14s%-10s%-13s%-11s%-10s%-13s%-11s\n", a[5],$1, $2, $3, $4, $5, $6, $7, $8) >> "Error.txt"};
        if ($8>=40 && $8<60){printf ("<Major> %s : %-11s%-14s%-10s%-13s%-11s%-10s%-13s%-11s\n", a[5],$1, $2, $3, $4, $5, $6, $7, $8) >> "Error.txt"};
        if ($8>=60){printf ("<Critical> %s : %-11s%-14s%-10s%-13s%-11s%-10s%-13s%-11s\n", a[5],$1, $2, $3, $4, $5, $6, $7, $8) >> "Error.txt"};
        x=0;
        printf ("%-11s%-14s%-10s%-13s%-11s%-10s%-13s%-11s\n", $1, $2, $3, $4, $5, $6, $7, $8)
}

Run it on your log file: $ awk -f awkfile test4.log

Last edited by bartus11; 04-16-2011 at 12:26 PM..
This User Gave Thanks to bartus11 For This Post:
# 3  
Old 04-16-2011
Quote:
Originally Posted by bartus11
Try this:
Code:
#!/bin/awk -f
BEGIN {
        FS = ";";
        x=0;
}
NF==1 && x==0{
        x=1;
        split($0,a," ");
        print;
        print "===================================" >> "Error.txt";
        next;
}
NF==1{
        print;
        next;
}
{
        if ($5>=10 && $5<40){printf ("<Minor> %s : %-11s%-14s%-10s%-13s%-11s%-10s%-13s%-11s\n", a[3],$1, $2, $3, $4, $5, $6, $7, $8) >> "Error.txt"};
        if ($5>=40 && $5<60){printf ("<Major> %s : %-11s%-14s%-10s%-13s%-11s%-10s%-13s%-11s\n", a[3],$1, $2, $3, $4, $5, $6, $7, $8) >> "Error.txt"};
        if ($5>=60){printf ("<Critical> %s : %-11s%-14s%-10s%-13s%-11s%-10s%-13s%-11s\n", a[3],$1, $2, $3, $4, $5, $6, $7, $8) >> "Error.txt"};
        if ($8>=10 && $8<40){printf ("<Minor> %s : %-11s%-14s%-10s%-13s%-11s%-10s%-13s%-11s\n", a[5],$1, $2, $3, $4, $5, $6, $7, $8) >> "Error.txt"};
        if ($8>=40 && $8<60){printf ("<Major> %s : %-11s%-14s%-10s%-13s%-11s%-10s%-13s%-11s\n", a[5],$1, $2, $3, $4, $5, $6, $7, $8) >> "Error.txt"};
        if ($8>=60){printf ("<Critical> %s : %-11s%-14s%-10s%-13s%-11s%-10s%-13s%-11s\n", a[5],$1, $2, $3, $4, $5, $6, $7, $8) >> "Error.txt"};
        x=0;
        printf ("%-11s%-14s%-10s%-13s%-11s%-10s%-13s%-11s\n", $1, $2, $3, $4, $5, $6, $7, $8)
}

Run it on your log file: $ awk -f awkfile test4.log
Thanks man! it worked perfectly on Cygwin Smilie i will test it on Solaris & Linux platforms and see if i get same result.

For my knowledge, can you please comment each command in your code? this is really important for me.

Sorry but one more question about the header you see in the test file:
Code:
        Device          |             IPMB0-A              |             IPMB0-B
Board      Address      |Sent      SentErr      %Errr      |Sent      SentErr      %Errr
============================================================================================

I achieved it (with the help of Unix.com forum) by running one of the below commands, my problem is that the header is ok on cygwin but not on Solaris or Linux platforms which is blocking my project Smilie

Code:
sed "s/^$/$(sed -e :a -e '$! s/$/\\n/;' -e 'N;s/\n//g' -e 'ta' header)/" test.log

or

Code:
#sed "s/^$/$(sed -e ':a' -e 'N;$!ba' -e 's/\n/\\n/g' header)/" test.log

where test.log is:

Board1;9a;60;36;60.0;60;0;0.0
Board2;96;60;35;58.3;55;0;0.0
Board3;92;60;60;100.0;60;60;100.0
Board4;8e;60;35;58.3;60;0;0.0

Board1;9a;60;19;31.6;60;0;0.0
Board2;96;60;20;33.3;54;0;0.0
Board3;92;60;60;100.0;60;60;100.0
Board4;8e;60;20;33.3;60;0;0.0

O/P on Linux platform:
Code:
        Device          |             IPMB0-A              |             IPMB0-BnBoard      Address      |Sent      SentErr      %Errr      |Sent      SentErr      %Errr      n============================================================================================
Board1;9a;60;36;60.0;60;0;0.0
Board2;96;60;35;58.3;55;0;0.0
Board3;92;60;60;100.0;60;60;100.0
Board4;8e;60;35;58.3;60;0;0.0
        Device          |             IPMB0-A              |             IPMB0-BnBoard      Address      |Sent      SentErr      %Errr      |Sent      SentErr      %Errr      n============================================================================================
Board1;9a;60;19;31.6;60;0;0.0
Board2;96;60;20;33.3;54;0;0.0
Board3;92;60;60;100.0;60;60;100.0
Board4;8e;60;20;33.3;60;0;0.0

O/P on Solaris:
Code:
#sed -e :a -e '$! s/$/\\n/;' -e 'N;s/\n//g' -e 'ta' header

Unrecognized command: $! s/$/\\n/;

Do you know how can i overcome this problem?
# 4  
Old 04-21-2011
Any one can help please?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Check for null values in a columns. I have dozen of CSV files in a directory.

Hi Folks, I'm trying to write a simple file sanity check script. I have a directory with dozen CSV files containing id,edname,firstname,lastname,suffix,email. I like to write a awk script to check if first field contain a number and is not empty. and fields number 3,4 & 6 are not empty and... (3 Replies)
Discussion started by: dc34684
3 Replies

2. Shell Programming and Scripting

How to print lines that have values in certain columns ?

Hi, everyone I have a dataset like this: A B C D A C C D E F G H F D K Y X A K K C Gsome of columns have no values in each line. I want to print all lines that have 1/2/3/4 values, export separately to four files. What I expected is like this: file1 Y file 2 A C X Afile 3... (3 Replies)
Discussion started by: nengcheng
3 Replies

3. UNIX for Dummies Questions & Answers

Find Null values in Columns and fail execution by displaying error message

Hi All, I am new to shell scripting. I have a requirement as part of my job to find out null/empty values in column 2 and column 3 from a CSV file and exit the further execution of script by displaying a simple error message. I have developed a script to do this by reading various articles... (7 Replies)
Discussion started by: tpk
7 Replies

4. Shell Programming and Scripting

Adding columns with values dependent on existing columns

Hello I have a file as below chr1 start ref alt code1 code2 chr1 18884 C CAAAA 2 0 chr1 135419 TATACA T 2 0 chr1 332045 T TTG 0 2 chr1 453838 T TAC 2 0 chr1 567652 T TG 1 0 chr1 602541 ... (2 Replies)
Discussion started by: plumb_r
2 Replies

5. Shell Programming and Scripting

Check for null values in columns

Hi , I have below data with fixed with of 52 bytes having three columns value data. 01930 MA GLOUCESTER 02033 02025 COHASSET 01960 MA ... (3 Replies)
Discussion started by: sonu_pal
3 Replies

6. Shell Programming and Scripting

Print columns matching to specific values

Hello Friends, I have a CDR file and i need to print out 2 columns with their field position which matches to some constant values, a part of input file CZ=1|CZA=1|DIAL=415483420001|EE=13|ESF=1|ET=|FF=0|9|MNC=99|MNP=9041|MTC=0|NID=2|NOA=international|ON=1| OutPut ... (3 Replies)
Discussion started by: EAGL€
3 Replies

7. UNIX for Dummies Questions & Answers

[Solved] Print a line using a max and a min values of different columns

Hi guys, I already search on the forum but i can't solve this on my own. I have a lot of files like this: And i need to print the line with the maximum value in last column but if the value is the same (2 in this exemple for the 3 last lines) i need get the line with the minimum value in... (4 Replies)
Discussion started by: MetaBolic0
4 Replies

8. UNIX for Dummies Questions & Answers

Removing columns from a text file that do not have any values in second and third columns

I have a text file that has three columns. But at the end of the text file, there are trailing lines that have missing second and third columns: 4 0.04972604 KLHL28 4 0.0497332 CSTB 4 0.04979822 AIF1 4 0.04983331 DECR2 4 0.04990344 KATNB1 4 4 4 4 How can I remove the trailing... (3 Replies)
Discussion started by: evelibertine
3 Replies

9. Shell Programming and Scripting

perl script to print to values in rows and columns

Hi guys I want to print the values by using this script but its giving the no of rows and columns as input instead of values Would you plz help me on this FILE- chr1.txt 1981 1 1971 1 1961 1 1941 1 perl script #!/usr/bin/perl -w $infile1 = 'chr1.txt'; $outfile3 = 'out3.txt'; ... (3 Replies)
Discussion started by: nogu0001
3 Replies

10. Shell Programming and Scripting

How to check Null values in a file column by column if columns are Not NULLs

Hi All, I have a table with 10 columns. Some columns(2nd,4th,5th,7th,8th and 10th) are Not Null columns. I'll get a tab-delimited file and want to check col by col and generate seperate error code for each col eg:102 if 2nd col value is NULL and 104 if 4th col value is NULL so on... I am a... (7 Replies)
Discussion started by: Mandab
7 Replies
Login or Register to Ask a Question