1752678 Members
5362 Online
108789 Solutions
New Discussion юеВ

Re: range check in awk

 
Kris_5
Occasional Advisor

range check in awk

Hi Folks,

Is there any function (or piece of program developed by guru's like you) available for range check of the given value. For example the value of "a" should be between 0 and 45. I can do the check whether it is a interger or charcter with [0-9]+ or [a-z] string match. Thanks in adv.

Kris
8 REPLIES 8
Kris_5
Occasional Advisor

Re: range check in awk

Hi,

I forgot to mention the value of a=10,20 or a=12-40 or a=10,20-40. I really don't want to use split function for this. Thanks,

Kris
Mark Greene_1
Honored Contributor

Re: range check in awk

There is an "if" function that does numeric comparisions:
{
if ( a > 0 )
if ( a < 46 )
[do stuff with a here]
}

--
mark
the future will be a lot like now, only later
Ian Dennison_1
Honored Contributor

Re: range check in awk

Test file contains a number, followed by that letter of the alphabet,...

cat /tmp/a |awk 'BEGIN { FS=" " } ( $1 > 9 && $1 < 13 ) || ( $1 == 2 ) || ( $1 == 4 ) { print $0 }'

This prints 10 - 12, 2 and 4.

Use BEDMAS (Brackets, Exponents, Division, Multiplication, Addition & Subtraction) to formulate the conditional.

&& = AND
|| = OR

I assume that the use of 'awk' in the title was correct?

Share and Enjoy! Ian
Building a dumber user
Praveen Bezawada
Respected Contributor

Re: range check in awk

HI
If i understand your requirement correctly you can do
if a=10,20
echo a | awk -F, '{ if ( $1 > 0 ) && ( $1 < 45 ) && ( $2 > 0 ) && ( $2 < 45 ) {
print $0
}

...BPK...
Rodney Hills
Honored Contributor

Re: range check in awk

I gather that you just want to validate the sequence of ranges are valid. Since you don't want to use awk's split function for parsing the string, then I would recommend using perl.

Here is an example-

$mins="10,20-30,50";
$ok=1;
while($mins=~/[,]?(\d+)((-)(\d+))?/g) {
$ok=0 if $1 < 0 or $1 > 45;
next unless $4;
$ok=0 if $4 < 0 or $4 > 45;
}
print $ok;

This will print 1 if $mins has a valid range of numbers, otherwise it will print 0.

-- Rod Hills
There be dragons...
Jeff Machols
Esteemed Contributor

Re: range check in awk

Make sure when you use FS you are using FS="[-,]" so you get ranges and individual integers.
Simon Abbott
Frequent Advisor

Re: range check in awk

Hello Kris,

I see you don't want to use the split function but in this instance where you need to sub-divide the , seperated fields into - seperated fields, split really is the simplest way - I don't see a better alternative. Assuming that you want to test each comma seperated range against your >=0 and <=45 criteria the following code should work fine. I wrote this into a shell script for simplicity:

echo $1 | awk '

{
output = ""
x = split($0, cs, ",")
for (a = 1; a <= x; ++a)
{
y = split(cs[a], range, "-")
if ( range[1] <=45 && range[y] >= 0 )
output = "OK"

}

print output

}'

Where $1 is your comma seperated string, such as 3,10-36,47 etc

Regards,

Simon.
I'm still working on that one
Robin Wakefield
Honored Contributor

Re: range check in awk

Hi Kris,

This is an awk script that doesn't use split:

BEGIN{for (i=0;i<10;i++){numbers[i]=1};j=0;r=0;number=0;n=0}
{
for (i=1;i<=length(range);i++)
{
char=substr(range,i,1)
if (char in numbers)
{
number=number*10^(n++)+char
if (i continue
}
if (char == "-")
{
r=1
start=number
}
else
{
if (char == "," || i == length(range))
{
if (r == 1)
{
r=0
for (num=start;num narray[j++]=num
}
narray[j++]=number
}
}
number=0
n=0
}
for (i=0;i {
if ( narray[i] == $0 )
{
print "In range"
exit
}
}
print "Not in range"
}




echo 25 | awk -f /tmp/range.awk range=1-5,7,10-20
Not in range

# echo 15 | awk -f /tmp/range.awk range=1-5,7,10-20
In range

Rgds, Robin