bug-gawk
[Top][All Lists]
Advanced

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

Re: Is there a way to assign to $0 in command line?


From: arnold
Subject: Re: Is there a way to assign to $0 in command line?
Date: Fri, 09 Jul 2021 01:27:39 -0600
User-agent: Heirloom mailx 12.5 7/5/10

Greetings.

Please be aware of changes in the gawk project mailing lists. See

        https://lists.gnu.org/archive/html/bug-gawk/2021-07/msg00003.html

In particular, this list should ONLY be used for bug reports, not programming
questions of the sort you send.

To answer your question, there is no need to allow assigning $0
on the command line. It can be done trivially, like so:

        gawk -v first_rec="whatever" 'BEGIN { $0 = first_rec } ...'

Please take all further discussion to the help list.

Arnold

Peng Yu <pengyu.ut@gmail.com> wrote:

> Hi,
>
> $ seq 3 | awk -v '$0=abc' -e 'BEGIN { print } { print }'
> awk: fatal: `$0' is not a legal variable name
>
> It seems that it is impossible to assign some value to $0 in the command line.
>
> $ { echo abc; seq 3; } | awk -e 'BEGIN { getline } BEGIN { print }  { print }'
>
> The best walkaround that I came up is the above. But it is a little
> cumbersome, as the content to be assigned to $0 has to be merged with
> the stdin stream. The advantage is that the part of the code `BEGIN {
> print }  { print }` does not have to change, which can be a good thing
> when that part of the code should not be changed.
>
> $ seq 3 | awk -v line=abc  -e 'BEGIN { print line } { print }'
>
> Alternatively, I can assign it to a different variable name. But the
> part of the code of `BEGIN { print }  { print }` has to be modified.
> It is not preferred in certain cases.
>
> Therefore, is there a way to directly modify $0 in the command of awk
> (e.g., via -v)? Or would it make sense to allow $0 be modified in the
> command (similar to the effect of changing $0 in a BEGIN block, but a
> command-line way makes string escaping unnecessary.).
>
> -- 
> Regards,
> Peng



reply via email to

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