[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: Input line splitting and OFS
From: |
address@hidden |
Subject: |
RE: Input line splitting and OFS |
Date: |
Fri, 15 Nov 2019 11:25:26 +0000 |
Hi Peter,
Thanks for your response.
I'm not sure that what you suggest would be applicable in my script. The
example I provided is extremely simplified just to illustrate the issue.
There are certainly however several possible workarounds. The one I've used is
to loop through fields, shifting them up one, and inserting the new value into
the resulting space :
...
if ($2 == "b") {
# $2 = "x" FS $2
# $0 = $0
for (i = NF; i >= 2; i--) {
$(i+1) = $i
}
$2 = "x"
}
...
But my question is more about the reasoning behind the use of OFS in the
context of an input line. Why is OFS used, when we’re dealing with input ?
Chris
-----Original Message-----
From: Peter Brooks [mailto:address@hidden]
Sent: 15 November 2019 10:45
To: Newell, Christopher (IE Dundrum)
Cc: address@hidden
Subject: Re: Input line splitting and OFS
----------------------------- PHISHING ALERT -----------------------------
This email has been sent from an account outside of the BAE Systems network.
Be aware that this could be a phishing attempt. For more guidance, search
"phishing email" on Connect. If you think this is a phishing email report it
using the "PhishMe" button on Outlook.
------------------------------------------------------------------------------------
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
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.