bug-gawk
[Top][All Lists]
Advanced

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

Re: Input line splitting and OFS


From: Nethox
Subject: Re: Input line splitting and OFS
Date: Sat, 16 Nov 2019 17:09:25 +0100

Do not confuse resplitting $0 with rebuilding $0.
Assignment to $0 causes the record to be resplit with FS.
Assignment to NF or any field, including non-existing ones, causes the
record to be rebuilt with OFS.

As Andrew suggested, concatenation with FS is fragile. It is also
confusing, your example mixes manually inserted FS with automatically
inserted OFS. The later $0 = $0 is useless.

In my opinion, the mawk's man page is clearer than the gawk's one:
"Assignment to $0 causes the fields and NF to be recomputed.
Assignment to NF or to a field causes $0 to be reconstructed by
concatenating the $i's separated by OFS. Assignment to a field with
index greater than NF, increases NF and causes $0 to be
reconstructed."

Regards.

On Fri, Nov 15, 2019 at 11:36 AM address@hidden
<address@hidden> wrote:
>
> Hi,
>
> I have a question about the use of OFS as it relates to field splitting.
>
> In a script I use I sometimes need to insert a field, based on the values in 
> the current record. Subsequently I also need to test the value of that field. 
> When I insert the field, I force gawk to re-evaluate the input line by using 
> $0 = $0.
>
> Up until now this worked fine, but recently I was given a new use case where 
> I sometimes need to set OFS to null, and later set it back to something else, 
> again based on values in the input file. It's therefore impossible to predict 
> in advance if and when this will occur.
>
> The issue is that when I've set OFS = "", the re-evaluation uses that value 
> to re-evaluate the input line, and so all the fields are concatenated into 
> one.
>
> The following code illustrates the issue :
>
> # -------------------------------------
> # fs_vs_ofs.awk
> # -------------------------------------
>
> BEGIN {
>     FS = " "
> #    OFS = ""
>     split("a,b,c,d,e,f,x", tmp, ",")
>     for (i in tmp) {
>         valid_actions[tmp[i]]
>     }
> }
> # -------------------------------------
> {
>     print NR " - before : " $0
>     if ($2 == "b") {
>         $2 = "x" FS $2
>         $0 = $0
>     }
>     print NR " - after  : " $0
>     for (i = 1; i <= NF; i++) {
>         printf "   $%d : %s\n", i, $i
>     }
>     if ($2 in valid_actions) {
>         print NR " - perform action " $2
>     } else {
>         print NR " - unknown action"
>     }
> }
> # -------------------------------------
>
> Sample data :
>
> a b c
> d e f
>
> If line 7 is commented out, the actions are executed as expected, but if OFS 
> is null they're not.
>
> I would have expected that re-evaluating an _input_ line would use FS, not 
> OFS, as it deals with input and not output.
>
> Is this a bug, or is there an explanation for the use of OFS in this 
> situation ?
>
> Thanks for your help.
>
> Kind regards,
> Chris Newell
>
> Chris Newell
> Application Support Engineer
> BAE Systems Applied Intelligence
> ___________________________________________________________
>
> E: address@hidden
>
> BAE Systems Applied Intelligence | Level 5, Block 4 | Dundrum Town Centre | 
> Sandyford Road | Dundrum | Dublin 16 | D16 A4W6 | Ireland
> www.baesystems.com/ai<http://www.baesystems.com/ai>
>
> BAE Systems will collect and process information about you that may be 
> subject to data protection laws. For more information about how we use and 
> disclose your personal information, how we protect your information, our 
> legal basis to use your information, your rights and who you can contact, 
> please refer to the relevant sections of our Privacy note at 
> www.baesystems.com/en/cybersecurity/privacy 
> <http://www.baesystems.com/en/cybersecurity/privacy>
>
> Please consider the environment before printing this email. This message 
> should be regarded as confidential. If you have received this email in error 
> please notify the sender and destroy it immediately. Statements of intent 
> shall only become binding when confirmed in hard copy by an authorised 
> signatory. The contents of this email may relate to dealings with other 
> companies under the control of BAE Systems PLC, details of which can be found 
> at http://www.baesystems.com/Businesses/index.htm.



reply via email to

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