- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- awk behaviour
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
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
тАО07-03-2009 05:42 AM
тАО07-03-2009 05:42 AM
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
Solved! Go to Solution.
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-03-2009 05:43 AM
тАО07-03-2009 05:43 AM
Re: awk behaviour
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-03-2009 05:52 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-03-2009 05:55 AM
тАО07-03-2009 05:55 AM
Re: awk behaviour
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-03-2009 05:59 AM
тАО07-03-2009 05:59 AM
Re: awk behaviour
# 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-03-2009 06:03 AM
тАО07-03-2009 06:03 AM
Re: awk behaviour
> 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-03-2009 06:07 AM
тАО07-03-2009 06:07 AM
Re: awk behaviour
Thanks a bunch !
Cheers,
Wout
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-03-2009 10:47 PM
тАО07-03-2009 10:47 PM
Re: awk behaviour
>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. :-)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-04-2009 11:31 AM
тАО07-04-2009 11:31 AM
Re: awk behaviour
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 ;-)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-04-2009 07:13 PM
тАО07-04-2009 07:13 PM
Re: awk behaviour
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