awk behaviour (81 Views)
Reply
Honored Contributor
Wouter Jagers
Posts: 495
Registered: ‎10-01-2002
Message 1 of 11 (81 Views)
Accepted Solution

awk behaviour

Hi all,

Long time no see ;-)
I'm encountering some puzzling awk behaviour.

This is my (example) source file:

---
/home/jagersw > cat tst
021F 021F
0400 0400
04E2 04E2
012A 012A
---

I'd like to ask awk to find me the 2nd field for any given 1st one, using a variable.

However, what seems to work most of the time gives me grief at some occasions.

---
/home/jagersw > R1CHK="021F"

/home/jagersw > awk -v r1chk="$R1CHK" '$1==r1chk{print $2}' tst
021F

/home/jagersw > R1CHK="012A"

/home/jagersw > awk -v r1chk="$R1CHK" '$1==r1chk{print $2}' tst
012A
---

That's obviously what I want. Very nice.

However, with some values it doesn't seem to work:

---
/home/jagersw > R1CHK="04E2"

/home/jagersw > awk -v r1chk="$R1CHK" '$1==r1chk{print $2}' tst
0400
04E2

/home/jagersw > R1CHK="0400"

/home/jagersw > awk -v r1chk="$R1CHK" '$1==r1chk{print $2}' tst
0400
04E2
---

I'm puzzled. Maybe I just didn't get enough sleep last night, but I can't think of an explanation for this.

Anyone up for enlightening me ?

Thanks in advance !

Cheers,
Wout
an engineer's aim in a discussion is not to persuade, but to clarify.
Please use plain text.
Honored Contributor
Wouter Jagers
Posts: 495
Registered: ‎10-01-2002
Message 2 of 11 (81 Views)

Re: awk behaviour

11.31 on ia64
an engineer's aim in a discussion is not to persuade, but to clarify.
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 3 of 11 (81 Views)

Re: awk behaviour

Hi Wout:

You are close:

# awk -v PAT="0400" '$1~PAT {print $2}' file

Regards!

...JRF...
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 4 of 11 (81 Views)

Re: awk behaviour

Hi (again) Wout:

I meant to add that the tilde allows the righthand side to be a constant (a sequence of characters between slashes) or an expression which can be evaulated. Here, we the evaulation to occur.

Regards!

...JRF...
Please use plain text.
Honored Contributor
Wouter Jagers
Posts: 495
Registered: ‎10-01-2002
Message 5 of 11 (81 Views)

Re: awk behaviour

Thanks James !!

# awk -v PAT="0400" '$1~PAT {print $2}' file

Naturally I've tried a bunch of things (even ~/^PAT/), except this.

Works excellent now.

Any idea why I got this output ? Or should we just classify it under 'unexpected behaviour' :-)

Thanks again !

Cheers
Wout
an engineer's aim in a discussion is not to persuade, but to clarify.
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 6 of 11 (81 Views)

Re: awk behaviour

Hi (again) Wout:

> Any idea why I got this output ? Or should we just classify it under 'unexpected behaviour' :-)

Yes, read my second post. Using ~/^PAT/ means to match a constant, not evaulate. Notice how we dropped the '/'.

Regards!

...JRF...
Please use plain text.
Honored Contributor
Wouter Jagers
Posts: 495
Registered: ‎10-01-2002
Message 7 of 11 (81 Views)

Re: awk behaviour

Indeed, that got cross-posted.
Thanks a bunch !

Cheers,
Wout
an engineer's aim in a discussion is not to persuade, but to clarify.
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,381
Registered: ‎03-06-2006
Message 8 of 11 (81 Views)

Re: awk behaviour

>with some values it doesn't seem to work: R1CHK="04E2"
>0400 04E2

My first thought was that you are mixing chars vs numbers. You shouldn't need no stinkin' ~ to be able to get a string exact match.

Looking closely at your output, 0400 and 04E2 ARE equal numerically!

So you need to convert to strings:
'$1 == r1chk "" {print $2}'
Or:
'BEGIN { r1chk = r1chk "" }
$1 == r1chk {print $2}'

>Anyone up for enlightening me?

It looks like nobody did.
Perhaps this is why you should use grep then awk. :-)
Please use plain text.
Honored Contributor
Wouter Jagers
Posts: 495
Registered: ‎10-01-2002
Message 9 of 11 (81 Views)

Re: awk behaviour

Nice, concatenating a quote or zero for conversion was new to me, thanks for pointing this out !

How exactly are these the same, numerically ? I don't seem to experience the same issue with values like 04E4 or 012E ..?

Thanks for the continued enlightenment ;-)
an engineer's aim in a discussion is not to persuade, but to clarify.
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,381
Registered: ‎03-06-2006
Message 10 of 11 (81 Views)

Re: awk behaviour

>concatenating a quote or zero for conversion

I've seen awk scripts where they do "x + 0" to do the opposite.

>How exactly are these the same, numerically?

Scientific notation: 04E2 is 4 * 10**2 == 400
Please use plain text.
Honored Contributor
Wouter Jagers
Posts: 495
Registered: ‎10-01-2002
Message 11 of 11 (81 Views)

Re: awk behaviour

Of course :-)
Thanks again !
an engineer's aim in a discussion is not to persuade, but to clarify.
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation