Sujet : Re: "sed" question
De : mortonspam (at) *nospam* gmail.com (Ed Morton)
Groupes : comp.lang.awkDate : 14. Mar 2024, 11:30:16
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <usujjp$1i4ia$1@dont-email.me>
References : 1 2 3 4 5 6 7 8
User-Agent : Mozilla Thunderbird
On 3/13/2024 9:01 PM, Janis Papanagnou wrote:
On 12.03.2024 23:49, Ed Morton wrote:
On 3/9/2024 2:07 PM, Janis Papanagnou wrote:
On 09.03.2024 17:52, Ed Morton wrote:
>
About 20 or so years ago we had a discussion in this NG (which I'm not
going to search for now) and, shockingly, a consensus was reached that
we should encourage people to always write:
>
'{$2="1-1"} 1'
>
You snipped the important end of my statement. What I said was:
----
we should encourage people to always write:
'{$2="1-1"} 1'
instead of:
$2="1-1"
unless they NEED the result of the action to be evaluated as a condition
----
The "unless they NEED to" is important given your statement below about $2="1-1" being an awk idiom which I'll address below.
>
I don't recall such a "consensus".
>
I do, I have no reason to lie about it, but I can't be bothered
searching through 20-year-old usenet archives for it (I did take a very
quick shot at it but I don't even know how to write a good search for it
- you can't just google "awk '1'" and I'm not even sure if it was in
comp.lang.awk or comp.unix.shell).
I didn't say anything about "lying"; why do you insinuate so?
I don't insinuate so but I'm not sure if you're arguing that what I said is not good general advice or that it is good advice but you don't believe the discussion I referred to happened so I'm just eliminating one of the possibilities for why I'd say it happened = either it happened and I remember it, or it didn't happen and I think it did, or it didn't happen and I'm lying about it. I can rule out that I'm lying about it and I'd like to think it's more likely it happened and I remember it than that it didn't happen and I dreamed it up.
But your memory may mislead you. (Or mine, or Kaz', of course.)
(And no, I don't do the search for you; since you have been the
one contending something here.)
Finding the discussion wouldn't be for me. I know it happened, I know how to write such code, I've provide an example of why it's good advice, and everyone else can do whatever they like with that information. I'd have liked to provide the discussion for reference but couldn't find it. Oh well.
Without a reference such a statement is just void (and not more
than a rhetorical move).
It maybe would be if I didn't have a good reputation for awk knowledge on this and other forums over the past 30 years or so and/or hadn't provided an example of why it's good general advice.
You should at least elaborate on the details and facts of that
"consensus" - but for the _specific OP context_ (not for made
up cases).
I have nothing to elaborate with. I remember a discussion about 20 years ago and I remember the conclusion. That's all. Asking for a statement related to the OPs specific code is like if I had said "we should encourage people to always quote their shell variables unless they NEED the shell to perform globbing, etc. on it" and you asked for an impact statement of not doing so for `var=7; echo $var`. A specific piece of code not breaking doesn't invalidate good general advice.
>
If you want to avoid cryptic code you'd rather write
>
'{$2="1-1"; print}'
>
Don't you think?
>
>
If I'm writing a multi-line script I use an explicit `print` but it just
doesn't matter for a tiny one-line script like that.
Actually, for the given case, the yet better solution is what the
OP himself said (in CUS, where his question was initially posted):
Grant Taylor on alt.comp.software.thunderbird suggested [...]:
$ awk '{print $1, "1-1"}'
Since this suggestion doesn't overwrite fields and is conceptually
clear. It inherently also handles (possible?) cases where there's
more than two fields in the data (e.g. by spurious blanks).
Of course.
Everyone using awk
needs to know the `1` idiom as it's so common and once you've seen it
once it's not hard to figure out what `{$2="1-1"} 1` does.
The point is that $2="1-1" as condition is also an Awk idiom.
That's like saying `echo $var` is a shell idiom. There are times when you need to do it, i.e. when you want the shell to perform globbing, word splitting, and filename generation on `$var` but it wouldn't invalidate the good general advice that "we should encourage people to always quote their shell variables unless they NEED the shell to perform globbing, etc. on it"
>
By changing `condition` to `{condition}1` we just add 3 chars to remove
the guesswork from anyone reading it in future and protect against
unconsidered values so we don't just make it less cryptic but also less
fragile.
Your examples below are meaningless since you make up cases that have
nothing to do with the situation here, and especially in context of
my posting saying clearly: "In this specific case of regular data".
That's again like arguing that if I had said "we should encourage people to always quote their shell variables unless they NEED the shell to perform globbing, etc. on it" it'd be meaningless if the OPs sample code in this specific case didn't break without quotes.
The more problematic issue is that $2="1-1" and also {$2="1-1"}
both overwrite fields and thus a reorganization of the fields is
done which has - probably unexpected by a newbie coder - side effects.
That's not an issue if the OPs intent is to replace all strings that match FS with OFS, which I've no idea if they want to do or not, but if it is an issue it's completely different one unrelated to by statement I made, and which we've been discussing, that we should encourage people to always write `{$2="1-1"} 1` instead of just `$2="1-1"` unless they NEED the result of the action to be evaluated as a condition.
Ed.
But YMMV, of course.
Janis
>
For example, lets say someone wants to copy the $1 value into $3 and
print every line:
>
$ printf '1 2 3\n4 5 7\n' | awk '{$3=$1}1'
1 2 1
4 5 4
>
$ printf '1 2 3\n0 5 7\n' | awk '{$3=$1}1'
1 2 1
0 5 0
>
$ printf '1 2 3\n4 5 7\n' | awk '$3=$1'
1 2 1
4 5 4
>
$ printf '1 2 3\n0 5 7\n' | awk '$3=$1'
1 2 1
>
Note the 2nd line is undesirably (because I wrote the requirements)
missing from that last output.
>
It happens ALL the time that people don't consider all possible input
values so it's safer to just write the code that reflects your intent
and if you intend for every line to be printed then write code that will
print every line.
>
Ed.
>
>
And of course add more measures in case the data is not as regular as
the sample data suggests. (See my other postings what may be defined
as data, line missing or spurious blanks in the data, comment lines
or empty lines that have to be preserved, etc.)
>
instead of:
>
$2="1-1"
>
>
Janis
>
>