BladeSystem Forums have moved here
To make BladeSystem information easier to find, we have moved the BladeSystem forums here, to Servers and Operating Systems.
Showing results for 
Search instead for 
Do you mean 

awk behaviour

SOLVED
Go to Solution
Honored Contributor

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.
10 REPLIES
Honored Contributor

Re: awk behaviour

11.31 on ia64
an engineer's aim in a discussion is not to persuade, but to clarify.
Acclaimed Contributor

Re: awk behaviour

Hi Wout:

You are close:

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

Regards!

...JRF...
Acclaimed Contributor

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...
Honored Contributor

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.
Acclaimed Contributor

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...
Honored Contributor

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.
Acclaimed Contributor

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. :-)
Honored Contributor

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.
Acclaimed Contributor

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
Honored Contributor

Re: awk behaviour

Of course :-)
Thanks again !
an engineer's aim in a discussion is not to persuade, but to clarify.