-F option for awk

Roger Rohrbach roger at yuba.wrs.com
Tue Aug 14 07:41:03 AEST 1990


rjshaw at ramius.ocf.llnl.gov (Robert Shaw) writes:

>When awk'ing something like a passwd file, where the reasonable choice
>of field separator is something other than whitespace, how do you 
>let a line simply fall through and be printed unchanged?
>print; or print $0; don't do the right thing because the lines come
>out with spaces as the field separators instead of the character given
>to the -F option.

I don't know what version of awk you are using; both the old and GNU versions
of awk print the input line unchanged unless you explicitly reset the OFS
(output field separator) variable.  I.e,

    awk -F: '{ print }' /etc/passwd

prints /etc/passwd unchanged.  As a matter of fact, if you want the behavior
you claim to be experiencing, you not only have to set OFS, but you have to
mess with one of the fields in order to get awk to recompute $0, i.e.,

    awk -F: '
    BEGIN {
        OFS = " "
    }

    {
        $1 = $1	# recompute $0
        print
    }' /etc/passwd

produces the behavior you describe.  Perhaps "new awk" behaves differently;
in that case, try setting OFS to FS and use this trick.

Roger Rohrbach                                  sun!wrs!roger    roger at wrs.com
- Eddie sez: ----------------------------------------------- (c) 1986, 1990 -.
|   {o >o                                                                     |
|    \ -) I'm lurching between the aesthetic sublime and the quotidian grime. |



More information about the Comp.unix.questions mailing list