HPE GreenLake Administration
- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- awk bug or feature?
Operating System - HP-UX
1836984
Members
1976
Online
110111
Solutions
Forums
Categories
Company
Local Language
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
back
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
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
Topic Options
- 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
10-17-2002 07:00 AM
10-17-2002 07:00 AM
I know squat about awk. A user sent me a note saying he thinks there is a bug that needs a patch.
This is what he wrote. Hopefully I get the punctuation right.
In the awk code:
HOUR = substr($3,1,2)
if (HOUR>=8 && HOUR < 18) {
... code for prime shift
}
The test was always failing, regardless of the assignment to HOUR. It looks like the problem is in the character to integer comparison. I corrected the script with a single line:
HOUR = substr($3,1,2)
HOUR = HOUR + 0
if (HOUR ....
He says that the script works and was wondering if there was a patch that I should throw on.
What should I tell him? Is the script doing what it should? Is this a bug? Is there a better way to code it?
Thanks.
This is what he wrote. Hopefully I get the punctuation right.
In the awk code:
HOUR = substr($3,1,2)
if (HOUR>=8 && HOUR < 18) {
... code for prime shift
}
The test was always failing, regardless of the assignment to HOUR. It looks like the problem is in the character to integer comparison. I corrected the script with a single line:
HOUR = substr($3,1,2)
HOUR = HOUR + 0
if (HOUR ....
He says that the script works and was wondering if there was a patch that I should throw on.
What should I tell him? Is the script doing what it should? Is this a bug? Is there a better way to code it?
Thanks.
Solved! Go to Solution.
3 REPLIES 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-17-2002 07:09 AM
10-17-2002 07:09 AM
Solution
substr returns a string and not a number which ends up foiling some of the "automatic" conversions that take place in awk. This is not really a bug in awk, but a type of logic error in the script.
Your solution of adding "0" to the variable is fine, or you could use something like:
HOUR = int(substr($3, 1, 2))
instead which forces HOUR to be an integer.
Regards,
Rich
Your solution of adding "0" to the variable is fine, or you could use something like:
HOUR = int(substr($3, 1, 2))
instead which forces HOUR to be an integer.
Regards,
Rich
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-17-2002 07:10 AM
10-17-2002 07:10 AM
Re: awk bug or feature?
Awk is working as it should. It is up to the programmer to force numeric context. Substr() is a string function. The 'trick' is to change
HOUR = substr($3,1,2)
to
HOUR = substr($3,1,2) + 0
This will coerce HOUR into numeric context and all will then be well. Sometimes the reverse problem must be dealt with as well to force string context; the 'trick' in that case is to append a null string to a value. Both of these are stand awk idioms and well-disciplined awk programers will always use the '+ 0' to avoid those situations where one's code works (or doesn't work) by accident.
If one doesn't force context, one will find cases where identical awk code will behave differently on different platforms.
HOUR = substr($3,1,2)
to
HOUR = substr($3,1,2) + 0
This will coerce HOUR into numeric context and all will then be well. Sometimes the reverse problem must be dealt with as well to force string context; the 'trick' in that case is to append a null string to a value. Both of these are stand awk idioms and well-disciplined awk programers will always use the '+ 0' to avoid those situations where one's code works (or doesn't work) by accident.
If one doesn't force context, one will find cases where identical awk code will behave differently on different platforms.
If it ain't broke, I can fix that.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-17-2002 07:22 AM
10-17-2002 07:22 AM
Re: awk bug or feature?
Gord,
Of course there is a patch for "awk".
It's called "perl" :-)
-- Rod Hills
Of course there is a patch for "awk".
It's called "perl" :-)
-- Rod Hills
There be dragons...
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
Company
Events and news
Customer resources
© Copyright 2025 Hewlett Packard Enterprise Development LP