AWK version in hpux 11.11 (459 Views)
Reply
Occasional Visitor
hwwh1999sina.com
Posts: 5
Registered: ‎10-09-2008
Message 1 of 21 (459 Views)
Accepted Solution

AWK version in hpux 11.11

Some awk script can run in hpux but not in linux (with gawk).

Where can I get the awk source code of HPUX used?


=======
echo "abcd#123" | awk '{ FS="#";print NF}'

in HPUX output: 2
in linux (gawk) output : 1
Please use plain text.
Honored Contributor
Duncan Edmonstone
Posts: 5,676
Registered: ‎08-05-2000
Message 2 of 21 (459 Views)

Re: AWK version in hpux 11.11

> Where can I get the awk source code of HPUX used?


You can't. It's proprietary code, not open source.

I don't have a Linux system to hand to test this on, but does:

echo "abcd#123" | awk -F'#' '{ print NF }'

work consistently on both platforms?

HTH

Duncan

HTH

Duncan
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,387
Registered: ‎03-06-2006
Message 3 of 21 (459 Views)

Re: AWK version in hpux 11.11

>Duncan: awk -F'#' '{ print NF }'

(Rats, I was going to suggest -F.)
Alternately you could try:
echo "abcd#123" | awk 'BEGIN {FS="#"} { print NF}'

You probably need to have FS set before the cycle begins?
Take a look at gawk man page. There are lots of options, one may make it work?
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 4 of 21 (459 Views)

Re: AWK version in hpux 11.11

Hi:

Either Duncan's or Dennis' version work On HP-UX (11.11) or Linux Fedora 9 and yield output of "2".

You might find this compendium of use:

http://www.shelldorado.com/articles/awkcompat.html

Regards!

...JRF...
Please use plain text.
Honored Contributor
OldSchool
Posts: 3,373
Registered: ‎09-09-2004
Message 5 of 21 (459 Views)

Re: AWK version in hpux 11.11

your version of gawk is broken....as the correct answer is 2.
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 6 of 21 (459 Views)

Re: AWK version in hpux 11.11

Hi (again):

> OldSchool: your version of gawk is broken

Well, on my Fedora 9 running his snippet, I too get "1" :

$ awk --version
GNU Awk 3.1.5

Regards!

...JRF...
Please use plain text.
Honored Contributor
OldSchool
Posts: 3,373
Registered: ‎09-09-2004
Message 7 of 21 (459 Views)

Re: AWK version in hpux 11.11

Hmmm...according to "Sed & Awk" by Dale Doughtry & Arnold Robbins (O'Reilly):

"Typically, the field and record separators are defined in the BEGIN procedure because you want these values set before the first input line is read. However, you can redefine these values anywhere in the script. In POSIX awk, assigning a new value to FS has no effect on the current input line; it only effects the next input line"

it continues on to say:
"NOTE:
Prior to the June 1996 release of Bell Labs awk, versions of awk for UNIX did not follow the POSIX standard in this regard. In those versions, if you have not yet referenced an individual field, and you set the field separator to a different value, the current input line is split into fields using the new value of FS."

....which appears to be the case here.

Both of the suggestions from Duncan and Dennis should produce consistent behaviour in both HP's awk the GNU version, gawk, as the split is defined prior to the first input. IN addition, gawk has a --traditional option which is supposed to make gawk behave as Bell Labs version, which might work as well.
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 8 of 21 (459 Views)

Re: AWK version in hpux 11.11

Hi (again):

Yes, this is interesting. From:

http://www.gnu.org/software/gawk/manual/gawk.html

/* Begin quote */

According to the POSIX standard, awk is supposed to behave as if each record is split into fields at the time it is read. In particular, this means that if you change the value of FS after a record is read, the value of the fields (i.e., how they were split) should reflect the old value of FS, not the new one.

However, many implementations of awk do not work this way. Instead, they defer splitting the fields until a field is actually referenced. The fields are split using the current value of FS! (d.c.) This behavior can be difficult to diagnose. The following example illustrates the difference between the two methods. (The sed18 command prints just the first line of /etc/passwd.)

sed 1q /etc/passwd | awk '{ FS = ":" ; print $1 }'

which usually prints:

root

on an incorrect implementation of awk, while gawk prints something like:

root:nSijPlPhZZwgE:0:0:Root:/:

/* end quote */

On HP-UX 11.11 this prints "root".

By the way, adding the '--traditional' switch to the 'gawk' test I ran failed to make any difference.

Regards!

...JRF...
Please use plain text.
Honored Contributor
OldSchool
Posts: 3,373
Registered: ‎09-09-2004
Message 9 of 21 (459 Views)

Re: AWK version in hpux 11.11

James,

thanks for the info...and testing

--traditional.

somebody pirated my linux boxes for another project..so I can't test till I get home.

did you happen to try the -F and {BEGIN..} methods? I made the assumption they would be consistent across platforms as they should switch the separator before anything else happens.
Please use plain text.
Honored Contributor
Peter Nikitka
Posts: 1,575
Registered: ‎02-10-2003
Message 10 of 21 (459 Views)

Re: AWK version in hpux 11.11

Hi,

some Solaris10 results:

echo "abcd#123" | nawk '{ FS="#";print NF}'
2
echo "abcd#123" | gawk '{ FS="#";print NF}'
1
echo "abcd#123" | gawk -F'#' '{ print NF}'
2
echo "abcd#123" | oawk -F'#' '{ print NF}'
2
echo "abcd#123" | oawk '{ FS="#";print NF}'
1

Where
gawk --version
GNU Awk 3.1.0
really is an old version ...

Using the -F option leads to the same result for all my available awk exes.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 11 of 21 (459 Views)

Re: AWK version in hpux 11.11

Hi (again):

> OldSchool: did you happen to try the -F and {BEGIN..} methods?

Yes, indeed, they are consistent in HP-UX (11.11) and with GNU's awk (gawk) as I noted. Either Duncan's or Dennis' version work On HP-UX (11.11) or Linux Fedora 9 (GNU Awk 3.1.5) and yield output of "2".

I see the same "1" output using 'gawk' and "2" using HP-UX's 'awk' as noted by the OP.

Warmest Regards!

...JRF...
Please use plain text.
Honored Contributor
OldSchool
Posts: 3,373
Registered: ‎09-09-2004
Message 12 of 21 (459 Views)

Re: AWK version in hpux 11.11

all of the above seems to imply that, given file "abc" containing:

123#abc
123#def

running

gawk '{FS="#"; print NF, $FN}' abc'

would get

1 123#abc
2 def

????
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 13 of 21 (459 Views)

Re: AWK version in hpux 11.11

Hi (again):

Using OldSchool's suggested test:

$ cat /tmp/abc
123#abc
123#def

$ gawk '{FS="#"; print NF, $FN}' /tmp/abc
1 123#abc
2 123#def

...by the way, thanks Peter, for posting the '[gon]awk' tests!

Regards!

...JRF...
Please use plain text.
Honored Contributor
Peter Nikitka
Posts: 1,575
Registered: ‎02-10-2003
Message 14 of 21 (459 Views)

Re: AWK version in hpux 11.11

Hi again,

correcting the typo in OldSchool suggested test
<< '{FS="#"; print NF, $FN}'
>> '{FS="#"; print NF, $NF}'

leads - of course - to the expected results under Solaris10 here:

gawk '{FS="#"; print NF, $NF}' /tmp/abc
1 123#abc
2 def
oawk '{FS="#"; print NF, $NF}' /tmp/abc
1 123#abc
2 def
nawk '{FS="#"; print NF, $NF}' /tmp/abc
2 abc
2 def
nawk -F'#' '{print NF, $NF}' /tmp/abc
2 abc
2 def
oawk -F'#' '{print NF, $NF}' /tmp/abc
2 abc
2 def
gawk -F'#' '{print NF, $NF}' /tmp/abc
2 abc
2 def

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 15 of 21 (459 Views)

Re: AWK version in hpux 11.11

Hi (again) Folks:

Yikes, Peter, your eyes are better the mine today. I completely missed the "FN" / "NF" transposition. Here is OldSchool's test (properly):

$ gawk '{FS="#"; print NF, $NF}' /tmp/abc
1 123#abc
2 def

...exactly as he expected it to behave.

Regards!

...JRF...
Please use plain text.
Occasional Visitor
hwwh1999sina.com
Posts: 5
Registered: ‎10-09-2008
Message 16 of 21 (459 Views)

Re: AWK version in hpux 11.11

Thanks to all for the helping me!

[Chinese]:谢谢大家!
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,387
Registered: ‎03-06-2006
Message 17 of 21 (459 Views)

Re: AWK version in hpux 11.11

>Thanks to all for the helping me!

Since you are new to the forums please read the following about assigning points:
http://forums.itrc.hp.com/service/forums/helptips.do?#33

If you have no further questions about awk/FS, you can also close this thread, indicating you got your answers.
Please use plain text.
Occasional Visitor
hwwh1999sina.com
Posts: 5
Registered: ‎10-09-2008
Message 18 of 21 (459 Views)

Re: AWK version in hpux 11.11

Very thanks!

I have motified my awk script, use split instead 'FS' in awk {}.

Thanks for :
=====
"Typically, the field and record separators are defined in the BEGIN procedure because you want these values set before the first input line is read. However, you can redefine these values anywhere in the script. In POSIX awk, assigning a new value to FS has no effect on the current input line; it only effects the next input line"

it continues on to say:
"NOTE:
Prior to the June 1996 release of Bell Labs awk, versions of awk for UNIX did not follow the POSIX standard in this regard. In those versions, if you have not yet referenced an individual field, and you set the field separator to a different value, the current input line is split into fields using the new value of FS.
====
Thanks again !
Please use plain text.
Occasional Visitor
hwwh1999sina.com
Posts: 5
Registered: ‎10-09-2008
Message 19 of 21 (459 Views)

Re: AWK version in hpux 11.11

Thanks all!
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,387
Registered: ‎03-06-2006
Message 20 of 21 (459 Views)

Re: AWK version in hpux 11.11

You have closed the thread without assigning points. You need to make sure push the assign points button first.

You can reopen threads by:
http://forums.itrc.hp.com/service/forums/helptips.do?#41
Please use plain text.
Occasional Visitor
hwwh1999sina.com
Posts: 5
Registered: ‎10-09-2008
Message 21 of 21 (459 Views)

Re: AWK version in hpux 11.11

Thanks again!
:)
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