Operating System - HP-UX
1820553 Members
3630 Online
109626 Solutions
New Discussion юеВ

Re: awk: Using substr() to get a value for a loop

 
SOLVED
Go to solution
Simon Abbott
Frequent Advisor

awk: Using substr() to get a value for a loop

Hello everyone,

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
}
}
I'm still working on that one
9 REPLIES 9
Simon Abbott
Frequent Advisor

Re: awk: Using substr() to get a value for a loop

That last bit should read print i, not print 1

Simon.
I'm still working on that one
ian Dennison
Regular Advisor

Re: awk: Using substr() to get a value for a loop

why not use sed to trim out the alphabetics and load into a separate variable?

export NUMBER=`echo $SIMON | sed 's/[A-Za-z]//g'`

Share and Enjoy! Ian
Lets do it to them before they do it to us! www.fred.net.nz
Simon Abbott
Frequent Advisor

Re: awk: Using substr() to get a value for a loop

Cheers Ian,

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.
I'm still working on that one
Rodney Hills
Honored Contributor

Re: awk: Using substr() to get a value for a loop

Try replacing substr with
substr($1,2,999)

-- Rod Hills
There be dragons...
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: awk: Using substr() to get a value for a loop

Hi Simon:

You 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
If it ain't broke, I can fix that.
Simon Abbott
Frequent Advisor

Re: awk: Using substr() to get a value for a loop

Hello,

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.
I'm still working on that one
Simon Abbott
Frequent Advisor

Re: awk: Using substr() to get a value for a loop

Hello Clay,

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.
I'm still working on that one
James R. Ferguson
Acclaimed Contributor

Re: awk: Using substr() to get a value for a loop

Hi Simon:

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...
Simon Abbott
Frequent Advisor

Re: awk: Using substr() to get a value for a loop

Hello James,

Mine is 78.14.1.16 (the old one was 78.14.1.1) so I guess that's why.

Cheers,

Simon.
I'm still working on that one