Deutsch   English   Français   Italiano  
<ustlpc$18o8j$1@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!2.eu.feeder.erje.net!feeder.erje.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Janis Papanagnou <janis_papanagnou+ng@hotmail.com>
Newsgroups: comp.lang.awk
Subject: Re: "sed" question
Date: Thu, 14 Mar 2024 03:01:14 +0100
Organization: A noiseless patient Spider
Lines: 122
Message-ID: <ustlpc$18o8j$1@dont-email.me>
References: <us9vka$fepq$1@dont-email.me> <usa01v$fj5h$1@dont-email.me>
 <usagql$j9bc$1@dont-email.me> <slrnuuolcp.2g4k.naddy@lorvorc.mips.inka.de>
 <usi44f$2dqiq$1@dont-email.me> <usifh6$2gb9r$1@dont-email.me>
 <usqm57$hs0n$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 14 Mar 2024 02:01:16 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e31c4f444108d3680085339a8719c258";
	logging-data="1335571"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18zbMFaeWQm8xLzc4ElRtdP"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
 Thunderbird/45.8.0
Cancel-Lock: sha1:jU1fp2TLS4/LpJ01rP/3T1/s/yw=
X-Enigmail-Draft-Status: N1110
In-Reply-To: <usqm57$hs0n$1@dont-email.me>
Bytes: 5162

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'
>>>
>>
>> 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?

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.)

Without a reference such a statement is just void (and not more
than a rhetorical move).

You should at least elaborate on the details and facts of that
"consensus" - but for the _specific OP context_ (not for made
up cases).

> 
>> 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).

> 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.

> 
> 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".

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.

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
>>
>