How to merge two files in awk??

Paul Biron pbiron at weber.ucsd.edu
Sat Feb 2 03:15:18 AEST 1991


In article <1070 at mwtech.UUCP> martin at mwtech.UUCP (Martin Weitzel) writes:
>In article <3404 at d75.UUCP> @xlab1.uucp () writes:
[stuff deleted]
>> Finally, how do u specify the "rest of the line" in awk??
>
>I don't quite understand this. Do you mean the following:
>
>	33.5 ZZZ 4564.334 foo bar
>			  ^^^^^^^--- processed as "rest of line"
>	^^^^ ^^^ ^^^^^^^^ ---------- processed as $1, $2, $3
>

>first three fields always occupy the same space, say 18 chars, you
>can access the "rest of line" as substr($0, 19).
>
>If the $1..$3 have no equal witdh, but you are sure that there is
>only one separator between them, you may sum them up and get the rest
>of the line with substr($0, length($1) + length($2) + length($3) + 3).
>
[stuff deleted]

Another, albeit data dependent, way to do "rest of line" in {n,g}awk
is the following:

#!/usr/local/bin/gawk -f
{
        start = index ($0, $3)
        print "first is", $1, $2
        print "rest is", substr ($0, start)
}

This assumes that what you want to process as the "rest of line" does
not occur in the "first" part of the line.

While in general I agree with Martin that structuring your input data
makes your life a lot easier when it comes to writing awk scripts,
however, that is not always possible.  I don't know wheter it *is*
possible in the case of the original poster. 

Hope this helps,

--------------------------------------------------------------------------------
STOP THE WAR IN THE GULF --- NOW !!!!!!!
--------------------------------------------------------------------------------
Paul Biron      pbiron at ucsd.edu        (619) 534-5758



More information about the Comp.unix.questions mailing list