Operating System - HP-UX
1752689 Members
5564 Online
108789 Solutions
New Discussion юеВ

AWK version in hpux 11.11

 
SOLVED
Go to solution
hwwh1999sina.com
New Member

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
20 REPLIES 20

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

I am an HPE Employee
Accept or Kudo
Dennis Handly
Acclaimed Contributor

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?
James R. Ferguson
Acclaimed Contributor

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

Re: AWK version in hpux 11.11

your version of gawk is broken....as the correct answer is 2.
James R. Ferguson
Acclaimed Contributor

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

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.
James R. Ferguson
Acclaimed Contributor

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

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

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"