Output Row if Third Column is Larger

Hi,

I am fairly new to Unix scripting. We are running Solaris 5.10.

I have the following question: Assume a text file with one text
column, followed by 2 integer columns.

How would I generate a script or, preferably, a command that will output
the rows in which the value of the third columns is greater than
the value of the second column.

For example, assume that file is named File1 and it
consists of the following.

C1  50 43
C2 100 65
C3  45  60
C4  10 5
C5   50 55

The output of the command or script should be:

C3 45 60
C5 50 55

Thanks,
QZ1

Simplest would be:

awk '$3>$2' File1

Even for something this simple, I would still suggest /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk instead of the default awk on a Solaris 5.10 system.

1 Like

Don, are you suggesting OP to use xpg4/xpg6 awk instead of awk because this code will fail or you want OP to avoid using it because it is broken and not recommended?

---------- Post updated at 18:05 ---------- Previous update was at 17:41 ----------

Nevermind, I understood.

The awk in /bin or /usr/bin on Solaris 10 is the awk that was present in 1977-1985 UNIX Systems. On most UNIX and UNIX-like systems, nawk replaced awk in 1985 or shortly thereafter. On UNIX System V Systems (and on Solaris/SunOS 5.1 and later systems) the old awk kept the name awk and the new awk was named nawk .

If you want an historical version of awk to run awk scripts from that era use the Solaris 10 default awk . (On most other systems that have an awk from this era, it is known as oawk .) Otherwise, (including anytime your are writing new awk scripts to run on a Solaris 10 system) I believe you should use nawk , /usr/xpg4/bin/awk , or /usr/xpg6/bin/awk to get a version of awk that conforms to the POSIX and UNIX Standards and includes the updates to awk that Aho, Weinberg, and Kernighan described in their 1988 book The AWK Programming Language (ISBN 020107981X).

When writing new scripts, there is no reason to try to figure out if a script will be processed by the 1975 language interpreter in the same way that a current awk would interpret the script when we know that nawk and /usr/xpg[46]/bin/awk will reliably do what we all expect awk to do today.

PS. I am not suggesting that the default awk on Solaris systems is broken. I am suggesting that it is there to run historical awk scripts (which it does well). But, it should not be used when writing new awk scripts.

1 Like

My paranoid nature would go further and explictly cast one of the operands with +0 .

As far as I know, most awk implementations (if not all of them) will treat $3>$2 as a numeric comparison, but POSIX requires a locale-dependent string comparison.

Excerpts from POSIX awk:

$2 and $3 are both strings. If their string values are valid numbers, they are numeric strings. But, even if both are numeric strings, neither is a numeric. According to POSIX, for a numeric comparison at least one of the operands must be a numeric.

Even if no extant implementation adheres to this, an explicit cast is good future-proofing.

Regards,
Alister

Hi,

Thanks to those who replied.

QZ1