- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: awk: Using substr() to get a value for a loop
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
тАО02-04-2002 06:12 AM
тАО02-04-2002 06:12 AM
I am using substr() to get a value from the second to the last character of a string (see code below). for example, I want 321 from A321. I then want to run through a loop that many times.
I ran this happily for months until we moved to another server last week and (I'm guessing) a later version of awk. Now, the for loop only runs the number of times of the first character of the value, so if the value is 321 it runs only 3 times, it it's 421 it runs 4 times. I get the expected result out of an if statement but not a for statement.
If I use split(), everything works fine but substr() doesn't. Is this a bug, and if so is it a bug in the newer version or was I taking advantage of a bug in the old version? If it is a bug in the newer version is there a fix? I it is a bug in the old version is there a better way of extracting this value, other then using split() like this: simon=value[split($1,value,"A")]
Cheers, Simon.
the file awk is processing has one line:
A321
the output I get is:
1
2
3
---
{
simon = substr($1,2)
}
END {
for ( i = 1 ; i <= simon ; i++ )
{
print 1
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-04-2002 06:16 AM
тАО02-04-2002 06:16 AM
Re: awk: Using substr() to get a value for a loop
Simon.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-04-2002 06:17 AM
тАО02-04-2002 06:17 AM
Re: awk: Using substr() to get a value for a loop
export NUMBER=`echo $SIMON | sed 's/[A-Za-z]//g'`
Share and Enjoy! Ian
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-04-2002 06:21 AM
тАО02-04-2002 06:21 AM
Re: awk: Using substr() to get a value for a loop
Unfortunatly however, that file that awk processes is the output of an executable and the awk script does alot more - I've just simplified it here.
Simon.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-04-2002 06:24 AM
тАО02-04-2002 06:24 AM
Re: awk: Using substr() to get a value for a loop
substr($1,2,999)
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-04-2002 06:25 AM
тАО02-04-2002 06:25 AM
SolutionYou need to coerce the value returned by substr to be numeric rather than string. This is a pretty standard awk idiom:
simon = substr($1,2) + 0
The '+ 0' should do the trick for you. You old version was actually working by accident. Remember arrays in awk are associative so that what you you of an numerical indices really aren't. Perl neatly bushwhacks this by having both array (numeric indices) and hashes (associative indices).
Regards, Clay
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-04-2002 06:28 AM
тАО02-04-2002 06:28 AM
Re: awk: Using substr() to get a value for a loop
I tried that but it still doesn't like it. It is taking 321 as a value of some kind because
if ( simon == 321 ) print "hello"
works. I wondered whether it thinks 321 is a string and not an integer?
Simon.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-04-2002 06:37 AM
тАО02-04-2002 06:37 AM
Re: awk: Using substr() to get a value for a loop
Thanks, that worked a treat! (I posted my previous reply before I read yours).
Although I could work round this I was more curious to work out whether I was doing it right or not.
Thanks again,
Simon.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-04-2002 06:38 AM
тАО02-04-2002 06:38 AM
Re: awk: Using substr() to get a value for a loop
What are your awk versions (from the output of 'what /usr/bin/awk')?
On a 10.20 server I have:
$Revision: 78.14.1.12 $
PATCH_10_20: awk.g.o awk.lx.o b.o main.o tran.o lib.o run.o parse.o pro
ctab.o hpux_rel.o 99/05/06
On an 11.0 server I see:
#Revision: 82.2 $
In both cases I get an expected 12-lines of output from this test case:
# echo "A12"|awk '{X=substr($1,2)};END {for (i=1;i<=X;i++) {print i}}'
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-04-2002 06:45 AM
тАО02-04-2002 06:45 AM
Re: awk: Using substr() to get a value for a loop
Mine is 78.14.1.16 (the old one was 78.14.1.1) so I guess that's why.
Cheers,
Simon.