Adding up columns of numbers...

Steven List sml at luke.UUCP
Wed Jun 19 04:48:33 AEST 1985


In article <992 at trwatf.UUCP> root at trwatf.UUCP (Lord Frith) writes:
>Something I have noticed that disturbs me is why awk seems to think
>that there is an object between field seperators when that object is
>null.  In other words; awk will process three consecutive tabs and say
>that there are two fields whose values are null.

Remember that they are indeed `field separators'!  Why should awk NOT
treat two consecutive separators as containing an empty field?  This is
a perfectly legitimate use.  There are times when a field is empty, and
you wouldn't want awk to treat field 3 as field 2, would you?

Take the following example, in which the third and fourth fields are
address lines:

	Smith|John|1234 Jones Street||San Francisco|CA|94111
	Brown|Jack MD|Suite 234|2345 Smith Street|San Francisco|CA|94111

Clearly, it is desirable in the first case to treat the empty field as a
field in order not to process the city as an address line (and so on).
Remember, again, that field separators are field separators, not white
space.  Sort has the same problem/feature.  Any others?

If this continues to be a problem, use something like sed to distill the
file:

	sed 's/[ \t][ \t]*/ /g' | awk ...

Then you won't have the problem/feature to contend with.  Of course, you
might run into OTHER problems.  Another option is to use the `substr'
builtin function if you have fixed format files.



More information about the Comp.unix.wizards mailing list