bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: lint warning: unnecessary mixing of `>' and `>>'


From: arnold
Subject: Re: lint warning: unnecessary mixing of `>' and `>>'
Date: Sat, 21 Oct 2023 12:04:29 -0600
User-agent: Heirloom mailx 12.5 7/5/10

Hi.

Re this and the follow-on mails:

Hermann Peifer <peifer@gmx.eu> wrote:

> Hi again.
>
> Here a *small* observation.
>
> For me, the lint warning makes sense in #1, but the mixture in #2 is not
> necessarily unnecessary, as the intention of the user could well be to
> append to any pre-existing file.
>
> Hermann
>
> [~]> #1: So far, so good
> [~]> awk --lint 'BEGIN { f="out.dat"; print "Hello" > f; print "World"
>  >> f; close(f) }'
> awk: cmd. line:1: warning: unnecessary mixing of `>' and `>>' for file
> `out.dat'
> [~]>
> [~]> #2: An unnecessary mixture here? Most likely not.
> [~]> awk --lint 'BEGIN { f="out.dat"; print "Hello" >> f; print "World"
>  > f; close(f) }'
> awk: cmd. line:1: warning: unnecessary mixing of `>' and `>>' for file
> `out.dat'
> [~]>

The point is that it's bad style to mix > and >> for the same file,
no matter which one was used first.  I have rewritten the paragraphs
in that section of the manual to make this more clear. New text
is below.  I don't see a need to change the lint warning itself.

Thanks,

Arnold
--------------------------------
In the shell, when you are building up a file a line at a time, you
first use @samp{>} to create the file, and then you use @samp{>>} for
subsequent additions to it, like so:

@example
echo Name: Arnold Robbins > data
echo Street Address: 1234 A Pretty Street, NE >> data
echo City and State: MyTown, MyState 12345-6789 >> data
@end example

In @command{awk}, the @samp{>} and @samp{>>} operators are subtly
different.  The operator you use the @emph{first time} you write to a
file determines how @command{awk} will open (or create) the file.
If you use @samp{>}, the file is truncated, and then all subsequent
output appends data to the file, even if additional @code{print} or
@code{printf} statements continue to use @samp{>}.  If you use @samp{>>}
the first time, then existing data is not truncated, and all subsequent
@code{print} or @code{printf} statements append data to the file.

You should be consistent and always use the same operator for all output
to the same file. (You can mix @samp{>} and @samp{>>}, and nothing bad
will happen, but mixing the operators is considered to be bad style in
@command{awk}. If invoked with the @option{--lint} option, @command{gawk}
issues a warning when it encounters both operators being used for the
same open file.)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]