user3625743
user3625743

Reputation: 1

using awk if condition behaving abnormally for condition check

I working in cshel using awk. I am having a problem that it is not returning me the expected output, awk if condition is not behaving well sometime but behave well other time, need to understand

echo "m='12*mfactor'" | awk '{a="";a2=""; for(i=1;i<=NF;i++) {if($i~/mfactor/) {a=substr($i,3,3); gsub("*", "",a); gsub("\047", "",a); }}; for(i=1;i<=NF;i++) {if($i~/mfactor/&&a~/^[0-9]/&&a>1) {gsub("m=\047","m=\047"a1,$i); for(a2=1;a2<=a;a2++) {print a2","a};if(a2>a) {print a2" is greater than "a};}};  }'

it returns

1,12 2 is greater than 12

echo "m='4*mfactor'" | awk '{a="";a2=""; for(i=1;i<=NF;i++) {if($i~/mfactor/) {a=substr($i,3,3); gsub("*", "",a); gsub("\047", "",a); }}; for(i=1;i<=NF;i++) {if($i~/mfactor/&&a~/^[0-9]/&&a>1) {gsub("m=\047","m=\047"a1,$i); for(a2=1;a2<=a;a2++) {print a2","a};if(a2>a) {print a2" is greater than "a};}};  }'

it returns

1,4 2,4 3,4 4,4 5 is greater than 4

Upvotes: 0

Views: 157

Answers (1)

Tom Fenech
Tom Fenech

Reputation: 74685

The problem you are having is that you are doing string comparison a2<=a, when you want to be doing numeric comparison. 2 is greater than 1, so "2" is greater than "12".

I've believe that the following does what you want. I put it into a script so it was a bit more readable. The key difference is the use of the unary plus operator a = +a, which converts a into a number.

#!/usr/bin/awk -f

{
# no need to initialise a and a2 here
# combine the loops
    for(i=1;i<=NF;i++) {
        if($i~/mfactor/) {
# combine two gsubs - see below for potentially better alternative
            a=substr($i,3,3)
# instead of \047 use '\''
            gsub("[*'\'']", "",a)
            if(a~/^[0-9]/&&a>1) {
# a1 isn't initialised, what you were trying to do here?
# gsub("m=\047","m=\047"a1,$i)
# convert a to a number using unary plus
                a = +a              
                for(a2=1;a2<=a;a2++) {
                    print a2","a
                }
# no need for "if" as the loop has already ended
                print a2" is greater than "a
            }
        }
    }
}

This script can be run by making it executable and running echo "m='12*mfactor'" | ./factor.awk. It produces the desired results for me.


update

I'm not sure how varied your input lines will be but I think that using substr and gsub is probably overkill. You can achieve the same result with the following lines:

split($i, b, /[*\047]/)
a = +b[2]

You can use a regular expression to split on a range of characters, which is one option to get the numerical part out of your string.

Testing it out:

echo "m='4*mfactor'" | awk '
{ 
    for(i=1;i<=NF;i++) { 
        if($i~/mfactor/) { 
            split($i, b, /[*'\'']/); a = +b[2]
            if(a~/^[0-9]/&&a>1) { 
                for(a2=1;a2<=a;a2++) { print a2","a } 
                print a2" is greater than "a 
            } 
        } 
    } 
}'
1,4
2,4
3,4
4,4
5 is greater than 4
echo "m='12*mfactor'" | awk '
{ 
    for(i=1;i<=NF;i++) { 
        if($i~/mfactor/) { 
            split($i, b, /[*'\'']/); a = +b[2]
            if(a~/^[0-9]/&&a>1) { 
                for(a2=1;a2<=a;a2++) { print a2","a } 
                print a2" is greater than "a 
            } 
        } 
    } 
}'
1,12
2,12
3,12
4,12
5,12
6,12
7,12
8,12
9,12
10,12
11,12
12,12
13 is greater than 12

Upvotes: 2

Related Questions