[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Input line splitting and OFS
From: |
Peter Brooks |
Subject: |
Re: Input line splitting and OFS |
Date: |
Fri, 15 Nov 2019 10:44:43 +0000 |
Why not just evaluate into another variable, then you've still got the
original line to re-interpret?
On Fri, 15 Nov 2019 at 10:36, 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.
--
Peter Brooks
Skype: Fustbariclation
Twitter: Fustbariclation
Author Page: amazon.com/author/peter_brooks