modify awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting modify awk
# 1  
Old 09-27-2012
modify awk

Code:
awk "BEGIN {if($PERCENT<$WARNING)

                {print \"OK\" ; exit 0}

        else if(($PERCENT>=$WARNING) && ($PERCENT<$CRITICAL))

                {print \"WARNING\" ; exit 1}

        else if($PERCENT>=$CRITICAL)

                {print \"CRITICAL\" ; exit 2}

}"

how can i modify the above to include another "else if", if the content of PERCENT is non-numerical or empty?

can someone please modify this for me?

i'm thinking something like this:

Code:
awk "BEGIN {if($PERCENT<$WARNING)

                {print \"OK\" ; exit 0}

        else if(($PERCENT>=$WARNING) && ($PERCENT<$CRITICAL))

                {print \"WARNING\" ; exit 1}

        else if($PERCENT>=$CRITICAL)

                {print \"CRITICAL\" ; exit 2}
        else
                {print \"UNKNOWN\" ; exit 3}
}"

# 2  
Old 09-30-2012
First: What shell are you using?

Second: This is a VERY strange mix of shell and awk (using shell parameter expansion to determine the statements to be executed in awk). Everything that you have in this awk script so far could be done just as easily in a shell function. It is pretty obvious that this awk program has to be embedded in a shell script. Why are youe calling awk rather than doing this directly in your shell?

Third: You need to verify that $PERCENT has a valid value BEFORE you use it in places that assume the value is valid. So, you need to add the tests to verify that $PERCENT is valid as your first tests; not as an added test or as added tests at the end.

Fourth: There is no reason for any of your "else if" constructs. Since each "then" action in your if statements includes an "exit" statement, both of the "else if" constructs in your awk script can be changed to just "if" without changing the behavior of the script.

Fifth: Do you also need to verify that $CRITICAL and $WARNING are non-null numeric strings? I'm assuming that some external source is setting $PERCENT so you need to verify it, but you don't need to verify the other two because your application defines them and, therefore, knows that they will have non-null, numeric values, and that $WARNING is less than $CRITICAL. The following Kornshell script fixes the problems in your awk script using the same strange mix of shell and awk parameter parsing that you used (adding tests to verify the contents of $PERCENT before using it in ways that assume it is non-null and numeric), provides a more conventional awk script that performs the same functions, and provides a shell function that performs the same functions. The script makes it easy to test various values for $PERCENT and runs both awk scripts and the shell function to show that they produce identical results. Hopefully, this example will help you learn a few things about both awk and shell syntax.

The script provided here should work unchanged with any shell that provides the basic shell features required by the POSIX standards and the Single UNIX Specifications. (Your system probably provides at least bash and ksh that meet this requirement, may have a traditional Bourne shell, and may have other POSIX compatible shells. This script will NOT work with csh or tcsh!) To keep it portable, the shell function in this script uses the expr utility to evaluate a regular expression. There are more efficient ways to do this in various shells (e.g., ${var//expr/rep} in ksh), but they are not portable to different shells.

To use this script, save the following code in a file named tester:
Code:
#!/bin/ksh
# shellfunc() -- Verify range
#       This function performs the same checks as the awk script written
#       in-line in this script below.  It prints the same messages, and returns
#       the same values as the awk script.  As with the awk script it gets the
#       values of $PERCENT, $WARNING, and $CRITICAL from the environment.
shellfunc() {
        if [ "$PERCENT" == "" ] || [ $(expr "$PERCENT" : ".*[^0-9]") -gt 0 ]
        then    echo "NULL OR NON-NUMERIC PERCENT"
                return 3
        fi
        if [ "$PERCENT" -lt "$WARNING" ]
        then    echo OK
                return 0
        fi
        if [ "$PERCENT" -ge "$WARNING" ] && [ "$PERCENT" -lt "$CRITICAL" ]
        then    echo WARNING
                return 1
        fi
        if [ "$PERCENT" -ge "$CRITICAL" ]
        then    echo CRITICAL
                return 2
        fi
        echo UNKNOWN
        return 3
}

# Main program follows:
#
#Usage: tester [percent [warning [critical]]]
#   This utility calls a modified version of the awk script given in the thread
#   titled "modify awk", a more traditional version of a similar awk script,
#   and a shell function that behaves similarly.  If no operands are given to
#   this script, PERCENT defaults to "40", WARNING defaults to "50", and
#   CRITICAL defaults to "90".  If warning, or warning and critical are
#   supplied, they override the default values for WARNING or WARNING and
#   CRITICAL (but they won't be overridden by empty strings.  There is no
#   checking for bad values for WARNING and CRITICAL (except that if an empty
#   string is provided for either of these values, the default will still be
#   used.
#
#   Invoking this program as follows will test various featres:
#       tester  (use default values, should print OK with exit codes 0)
#       tester 60 (sets PERCENT=60; should print WARNING with exit codes 1)
#       tester 98 (sets PERCENT=98; should print CRITICAL with exit codes 2)
#       tester 90x (should print NULL OR NON-NUMERIC PERCENT with exit codes 4)
#       tester "" (should print NULL OR NON-NUMERIC PERCENT with exit codes 4)
PERCENT=${1-40}
WARNING=${2:-50}
CRITICAL=${3:-90}
echo "Values in use for this execution:"
printf "PERCENT=\"%s\" WARNING=\"%s\" CRITICAL=\"%s\"\n\n" \
        "$PERCENT" "$WARNING" "$CRITICAL"

# Evaluate parameters using modified awk with shell parameter parsing:
echo "Calling modified awk script:"
awk "BEGIN {
        if((\"$PERCENT\" == \"\") || (\"$PERCENT\" ~ /[^0-9]/))
                {print \"NULL OR NON-NUMERIC PERCENT\"; exit 4}
        if(\"$PERCENT\" < \"$WARNING\") {print \"OK\"; exit 0}
        if((\"$PERCENT\" >= \"$WARNING\") && (\"$PERCENT\" < \"$CRITICAL\"))
                {print \"WARNING\"; exit 1}
        if(\"$PERCENT\" <= \"$CRITICAL\") {print \"CRITICAL\"; exit 2}
        print "UNKNOWN"
        exit 3
}"
printf "Modified mixed awk exit code: %d\n\n" $?

# Evaluate parameters using traditional awk script:
echo "Calling traditional awk script:"
awk -v pct="$PERCENT" -v warn="$WARNING" -v crit="$CRITICAL" 'BEGIN {
        if((pct=="") || (pct~/[^0-9]/))
                {print "NULL OR NON-NUMERIC PERCENT"; exit 4}
        if(pct<warn) {print "OK"; exit 0}
        if((pct >= warn) && (pct < crit))
                {print "WARNING"; exit 1}
        if(pct <= crit) {print "CRITICAL"; exit 2}
        printf "UNKNOWN"
        exit 3
}'
printf "Traditional awk exit code: %d\n\n" $?

# Evaluate parameters using shellfunc():
echo "Calling shellfunc:"
shellfunc
printf "shellfunc() return code: %d\n" $?

Make it executable by:
Code:
chmod +x tester

and then execute it with various values for $PERCENT as follows:
Code:
tester # Use default value for $PERCENT (40)
tester 75 # Set $PERCENT to 75 (generate Warning message)
tester 95 # Set $PERCENT to 95 (generate Critical message)
tester "" # Set $PERCENT to an empty string (generate NULL string error)
tester 12x # Set $PERCENT to 12x (generate non-numeric error)

I hope this helps.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Modify csv-files with awk

Hello everyone! I have thousands of csv files I have to import into a Database table. As usually the files aren't perfect. For example they have a different number of columns and some weird columns. The second problem is, that I have to add 3 parts of the filename into 3 rows in the... (6 Replies)
Discussion started by: elRonaldo
6 Replies

2. Shell Programming and Scripting

awk modify string

Hi Guys, i world like to do the following with awk, i have the the complete username example in a file a have some names Mario Rossi John Doe i would like to convert this name in this format from file with awk Mario,Rossi,"Mario Rossi ",m.rossi_ext@mydomain.com,$TRUE, John,Doe,"John... (7 Replies)
Discussion started by: charli1
7 Replies

3. Shell Programming and Scripting

Modify xml using sed or awk

Hi All, I want to modify(changing the status from "on" to "off" status of Stage-element value from the below xml file using sed or awk: File Name: global.xml <?xml version="1.0" encoding="UTF-8"?> <config> <widget> <name>HTTP-POOL</name> <attributes> ... (5 Replies)
Discussion started by: wamqemail2
5 Replies

4. Shell Programming and Scripting

Modify text file using awk

I have text file with lines as shown here. Each row has 11 columns separated by tab. In each row, i want to split the 8th column such that the output should look like shown below. Here value in the 9th column is DP value and in the 10th column is MQ value followed by the values after resource.EFF=.... (15 Replies)
Discussion started by: mehar
15 Replies

5. Shell Programming and Scripting

Modify awk statement

how do i modify the following: echo "jaba law welcome no jaba law sorry now jaba law" | awk '{s+=gsub(/jaba law/,"jaba law")} END {print s}' so that it shows me the actual phrase it found matching the strings i specified? something like: jaba law jaba law jaba law (4 Replies)
Discussion started by: SkySmart
4 Replies

6. Shell Programming and Scripting

Modify an XLS file with Awk

Hello, I have 2 files. One has a list of serial numbers: 12345_7 2345_9 35454 4759:1 PEP8794 The other is an excel file, with multiple columns, separated by tab: 12345_7 ... ... .. .. .. .. .. 2345_9 ... ... .. .. .. .. .. 35454 ... ... .. .. .. .. .. 4759:1 ...... (4 Replies)
Discussion started by: ad23
4 Replies

7. Shell Programming and Scripting

awk can't modify the input file ??

Hi * I've just wanted to ask you if it's possible to modify the input file by using awk. Let me explain what I need: I need to change the value $4 from "defaults" to "nodev" in my text file. I've tried to use a string function called "sub" and it works. But I can't figure it out how to... (6 Replies)
Discussion started by: martinp111
6 Replies

8. Shell Programming and Scripting

modify and use awk sed program

The following awk script creates a file b.dat. awk '{print substr($0,1,27),substr($2,index($2,"_")+1)," ",substr($0,49)}' a.dat > b.dat I need this script to be modified to also sum $3 values by distinct $1 and $2 fields. Current file W2_2009275 2 8 W2_2009275 2 7 W1_2009275 1... (3 Replies)
Discussion started by: mnnarendra
3 Replies

9. Shell Programming and Scripting

Modify shell variables with AWK

Dear Folks, I have a command output something like: And I want to store PIN0 and SIG0 in two shell variables, now I do a double awk: PIN=`gsmctl -d /dev/ttyS0 pin sig | awk '/PIN0/ { print $2}'` SIG=`gsmctl -d /dev/ttyS0 pin sig | awk '/SIG0/ { print $2}'` It's possible to... (4 Replies)
Discussion started by: Santi
4 Replies

10. Shell Programming and Scripting

modify file using awk

I have a file, a.asc which is generated from a shell script: -----BEGIN PGP MESSAGE----- Version: PGP 6.5.8 qANQR1DBwE4DR5PN6zVjZTcQA/9z5Eg94cwYdTnC7v+JUegQuJwHripqnyjFrEs/ejzKYCNmngbHHmf8V4K3uFkYyp74aFf+CdymA030RKs6ewOwkmqRW19oIXCgVe8Qmfg+/2KTq8XN =0QSP -----END PGP MESSAGE----- I want... (12 Replies)
Discussion started by: nattynatty
12 Replies
Login or Register to Ask a Question