- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- range check in awk
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-12-2001 08:35 AM
тАО12-12-2001 08:35 AM
range check in awk
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-12-2001 08:38 AM
тАО12-12-2001 08:38 AM
Re: range check in awk
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 as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-12-2001 08:42 AM
тАО12-12-2001 08:42 AM
Re: range check in awk
{
if ( a > 0 )
if ( a < 46 )
[do stuff with a here]
}
--
mark
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-12-2001 08:46 AM
тАО12-12-2001 08:46 AM
Re: range check in awk
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-12-2001 08:50 AM
тАО12-12-2001 08:50 AM
Re: range check in awk
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-12-2001 09:04 AM
тАО12-12-2001 09:04 AM
Re: range check in awk
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-12-2001 09:39 AM
тАО12-12-2001 09:39 AM
Re: range check in awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-13-2001 02:29 AM
тАО12-13-2001 02:29 AM
Re: range check in awk
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-13-2001 05:45 AM
тАО12-13-2001 05:45 AM
Re: range check in awk
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
}
if (char == "-")
{
r=1
start=number
}
else
{
if (char == "," || i == length(range))
{
if (r == 1)
{
r=0
for (num=start;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