Using Logical Expression in an AWK statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using Logical Expression in an AWK statement
# 1  
Old 12-29-2011
Using Logical Expression in an AWK statement

I'm would to create a script that would give me the results below.
Please note the spaces in the log file are actually commas(",".)

Log file Data

HTML Code:
[TABLE]
0:00    21:15    899    43    31    12    25.39
0:00    21:20    736    34    19    15    35.39
0:00    21:20    776    41    28    13    66.65
0:00    21:20    777    43    31    12    15.44
0:00    21:20    899    44    32    12    96.6
0:00    21:20    126    40    27    13    95.44
0:00    21:20    130    37    23    14    95.44
0:00    21:25    736    35    20    15    96.6
0:00    21:25    775    35    20    15    36.65
0:00    21:25    776    41    28    13    96.65
0:00    21:25    780    43    31    12    95.44
0:00    21:25    891    35    20    15    46.08
0:00    21:25    896    49    39    10    95.44
0:00    21:25    899    43    31    12    95.39
0:00    21:30    691    37    23    14    75.19
0:00    21:30    736    37    23    14    95.38
0:00    21:30    764    43    31    12    55.37
0:00    21:30    776    40    27    13    95.44
0:00    21:30    777    47    36    11    96.65
0:00    21:30    780    44    32    12    76.65
0:00    21:30    783    34    19    15    95.44
0:00    21:30    891    35    20    15    96.08
0:00    21:35    736    37    23    14    25.44
0:00    21:35    776    40    27    13    15.44
0:00    21:35    778    40    27    13    35.34
0:00    21:35    780    44    32    12    96.65
0:00    21:40    685    40    27    13    95.39
0:00    21:40    691    41    28    13    96.38
0:00    21:40    777    46    35    11    95.44
0:00    21:40    780    43    31    12    95.44
0:00    21:40    882    34    19    15    75.39
0:00    21:40    891    38    24    14    95.89
0:00    21:40    896    49    39    10    80.44
0:00    21:45    782    38    24    14    96.34
0:00    21:45    882    35    20    15    96.65
0:00    21:45    900    37    23    14    95.19
0:00    21:50    685    40    27    13    95.39
0:00    21:50    689    41    28    13    96.65
[/TABLE]
I've tested the following individual scripts and I get the desired results.

Tested Scripts

HTML Code:
cat /log/per_111217.txt | awk -F, '{if ($9 >= 95 && $5 > 40 && <= 50)print $0}'
cat /log/per_111217.txt | awk -F, '{if ($9 >= 95 && $5 < 50)print $0}'
I would nowlike to Simplify it a bit more and require some assist before I attempt. Please note I'm running Solaris OS.

Simplified Not Yet Test

HTML Code:
{
cat /log/per_111217.txt | awk -F, '{if ($7 >= 95 && $4 <= 40)print $0}';
else if ( ($7 >= 95 && $4 > 40 && <= 50)print $0);
else if ($7 >= 95 && $4 < 50)print $0};
else "0 Found"
}
Desired Output

Result for 40 or less

 
0:00 21:40 882 34 19 15 95.39
0:00 21:40 891 38 24 14 95.89
Results for more than 40 but less than 50

 
0:00 21:30 777 47 36 11 96.65
0:00 21:30 780 44 32 12 97.65
Results for 50 or more

 
0:00 21:20 126 50 27 13 95.44
0:00 21:20 130 57 23 14 95.44
# 2  
Old 12-29-2011
You don't need to cat the file into awk, awk can read the file on it's own.

This will read the file and create three output files: one for below or equal to 40, one for between 40 and 50 and one for greater or equal to 50. Note that it uses nawk rather than awk -- awk on Solaris is pretty outdated.

Code:
nawk -F , '
    NF >= 7 && $7+0 > 95 {      #only when this field is > than desired
        if( $4+0 >= 50)
            print $0 >"above.out";   # records above 50 to this file
        else
        if( $4+0 <= 40 )
            print $0 >"below.out";    # write to file for records below
        else
            print $0 >"between.out";  # write to file for records in middle
    }'  /log/per_111217.txt

Output will retain the comma separation as exists in the input file.
This User Gave Thanks to agama For This Post:
# 3  
Old 12-29-2011
Code:
nawk '$7<95{next}
$4>=50{print $0 >"above.txt";next}
$4>40{print $0 >"between.txt";next}
$4<=40{print $0 >"below.txt";next}
' /log/per_111217.txt

This User Gave Thanks to ctsgnb For This Post:
# 4  
Old 01-08-2012
I forgot a condition which I've added to Agama's code. I would like to try this first, however I'm getting and error.
I would need to enter the date in the format below.
filename: limits_check

Run for current day:
/tmp/limits_check

Run for a specific day:
/tmp/limits_check 111217
Code:
#!/bin/ksh
day=`date +%y%m%d`

   if [ "${1}" ]
   then

echo 40 limits:
nawk -F, '
NF >= 7 && $7+0 > {

if($4+0 <= 40) print $0;
echo 50 limits:
else
 if($4+0 >=40 && $4+0 <= 50) print $0;
echo 80 limits:
else
 if($4+0 > 50) print $0;
else 
echo no limits $1;

}' /log/per_$1.txt

else

nawk -F, '
NF >= 7 && $7+0 > 95{
echo 40 limits:
    if($4+0 <= 40) print $0;
echo 50 limits:
else
 if($4+0 >=40 && $4+0 <= 50) print $0;
echo 80 limits:
else
 if($4+0 > 50) print $0;
else 
echo No limits for $day;
}' /log/per_$day.txt

fi


Last edited by ravzter; 01-08-2012 at 05:45 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

If statement fails with integer expression expected

Below is what i have in my script. htcount=$(curl -s --user tomcatstatus:tomcatstatus http://`hostname`.mypc.com:887/manager/jmxproxy?qry=Catalina:type=ThreadPool,name=\"http-nio-887\" |grep sBusy | cut -d ' ' -f2) echo $htcount if ; then echo "more than 10" else echo "Less than 10" fi... (6 Replies)
Discussion started by: mohtashims
6 Replies

2. Shell Programming and Scripting

incorporating a regular expression statement in a shell script (.sh)

I do have a shell file where I call many unix commands . I would like to add a regular expression step in that shell file, where a text file, say Test.txt has to be openned and all the :'s should be replaced. Basically apply the follwoing regular expression: :%s/://g to that particular text... (2 Replies)
Discussion started by: Lucky Ali
2 Replies

3. Shell Programming and Scripting

Logical expression in POSIX compliant Korn Shell

Hi, i want to check if a variable var1 is not a or b or c pseudo code: If NOT (var1 = a or var1 = b or var1 = c) then ... fi I want to use POSIX complaint Korn shell, and for string comparison For the following code, logical.sh #!/usr/bin/ksh var="j" echo "Var : $var" if ! { || ||... (12 Replies)
Discussion started by: ysrini
12 Replies

4. Shell Programming and Scripting

nested logical expression in bash shell

Please tell me how to nest logical expressions in bash. I would like to nest logical expressions for arguments of the "test" command on bash. The following pseudo-code shows my intention. // pseudo code if (exp1 AND (exp2 OR exp3)) { Output true; } else { Output false; } ... (11 Replies)
Discussion started by: LessNux
11 Replies

5. Shell Programming and Scripting

Regular expression inside case statement notworking

I have the following script: For catching errors like: But the regular expression ERROR*memory inside case doesn't seem to be working. The output of bash -x scriptname is: Please help (5 Replies)
Discussion started by: proactiveaditya
5 Replies

6. Shell Programming and Scripting

How to use logical operators in multiple if statement

Hi I want to send a status mail if daily or weekly or monthly batch completed or aborted. Here is the code. if && && || Else if && && || Else if && && || then mailx –s “Status Report” sumone@sumthing.com else print ”try again” Plz suggest the changes. (3 Replies)
Discussion started by: Avi
3 Replies

7. Shell Programming and Scripting

Can you use logical operators in a case statement (bash)?

I'm pretty sure I already know the answer to this, but I want to make sure I'm not overlooking anything. I'm working on a log monitoring script and every 10 lines I want to display a summary of events. The thing is, there are a lot of possible events, that likely won't have happened, so I only want... (0 Replies)
Discussion started by: DeCoTwc
0 Replies

8. UNIX for Dummies Questions & Answers

use a mathematical expression in an awk statement

I have some geometric data (X Y) that is in the wrong scale. My raw data is in mills but it needs to be in tenths of a mil I am pretty familiar with awk and sed I want to use awk to divide $1 and $2 by .1 I'm just not sure of the syntax $1 and $2 are variables and .1 is fixed simple... (3 Replies)
Discussion started by: awk_sed_hello
3 Replies

9. Shell Programming and Scripting

Logical AND within a case statement ??

Hi there, probably a really simple question to answer but i cant seem to find it can I use a logical AND (&&) within a CASE statement ie (ps this is useless syntax but youll get the idea case "$var1","$var2" in 'Billy' && 'Bobby') ... (1 Reply)
Discussion started by: hcclnoodles
1 Replies

10. UNIX for Dummies Questions & Answers

multiple Logical statement

hi I have following if condition line_by_line="0000000000000tttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt" if then echo "Exclusion criteria" else echo "Not exclusion criteria" fi above condition works perfectley but if i add one more logical condition... (3 Replies)
Discussion started by: mahabunta
3 Replies
Login or Register to Ask a Question