Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

FORTRAN is the language of Powerful Computers. -- Steven Feiner


devel / comp.lang.awk / Re: "sed" question

SubjectAuthor
* Re: "sed" questionChristian Weisgerber
+* Re: "sed" questionJulieta Shem
|`- Re: "sed" questionKenny McCormack
+* Re: "sed" questionEd Morton
|+* Re: "sed" questionJanis Papanagnou
||+- Re: "sed" questionKaz Kylheku
||`* Re: "sed" questionEd Morton
|| `* Re: "sed" questionJanis Papanagnou
||  `- Re: "sed" questionEd Morton
|`* Re: "sed" questionMr. Man-wai Chang
| `- Re: "sed" questionKeith Thompson
`- Re: "sed" questionJanis Papanagnou

1
Re: "sed" question

<slrnuuolcp.2g4k.naddy@lorvorc.mips.inka.de>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=1895&group=comp.lang.awk#1895

  copy link   Newsgroups: comp.unix.shell comp.lang.awk
Followup: comp.lang.awk
Path: i2pn2.org!i2pn.org!paganini.bofh.team!weretis.net!feeder8.news.weretis.net!news.szaf.org!inka.de!mips.inka.de!.POSTED.localhost!not-for-mail
From: naddy@mips.inka.de (Christian Weisgerber)
Newsgroups: comp.unix.shell,comp.lang.awk
Subject: Re: "sed" question
Followup-To: comp.lang.awk
Date: Sat, 9 Mar 2024 12:27:05 -0000 (UTC)
Message-ID: <slrnuuolcp.2g4k.naddy@lorvorc.mips.inka.de>
References: <us9vka$fepq$1@dont-email.me> <usa01v$fj5h$1@dont-email.me>
<usagql$j9bc$1@dont-email.me>
Injection-Date: Sat, 9 Mar 2024 12:27:05 -0000 (UTC)
Injection-Info: lorvorc.mips.inka.de; posting-host="localhost:::1";
logging-data="82069"; mail-complaints-to="usenet@mips.inka.de"
User-Agent: slrn/1.0.3 (FreeBSD)
 by: Christian Weisgerber - Sat, 9 Mar 2024 12:27 UTC

On 2024-03-06, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:

>> $ awk '{print $1, "1-1"}' newsrc-news.eternal-september.org-test >
>> newsrc-news.eternal-september.org
>
> In this specific case of regular data you can simplify that to
>
> awk '$2="1-1"' sourcefile > targetfile

That had me scratching my head. You can't have an action without
enclosing braces. But it's still legal syntax because... it's an
expression serving as a pattern. The assignment itself is a side
effect.

Care needs to be taken when using this shortcut so the expression
doesn't evalute as false:

$ printf 'one 1\ntwo 2\nthree 3\n' | awk '$2=4'
one 4
two 4
three 4
$ printf 'one 1\ntwo 2\nthree 3\n' | awk '$2=0'
$

$ printf 'one 1\ntwo 2\nthree 3\n' | awk '$2="4"'
one 4
two 4
three 4
$ printf 'one 1\ntwo 2\nthree 3\n' | awk '$2=""'
$

--
Christian "naddy" Weisgerber naddy@mips.inka.de

Re: "sed" question

<877cibsbja.fsf@yaxenu.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=1896&group=comp.lang.awk#1896

  copy link   Newsgroups: comp.lang.awk
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: jshem@yaxenu.org (Julieta Shem)
Newsgroups: comp.lang.awk
Subject: Re: "sed" question
Date: Sat, 09 Mar 2024 11:52:09 -0300
Organization: A noiseless patient Spider
Lines: 17
Message-ID: <877cibsbja.fsf@yaxenu.org>
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>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="d3697e664b2fe3b42eda67e412b665fb";
logging-data="2501424"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX181u35AYjoDYbmznq9VoJiXzIs1PAYIw+c="
Cancel-Lock: sha1:K8dGCQZ+GIiB3DDABHdi0UDoj5c=
sha1:JBvNrLa/fWKpSZIGT18NmIXXkw4=
 by: Julieta Shem - Sat, 9 Mar 2024 14:52 UTC

Christian Weisgerber <naddy@mips.inka.de> writes:

> On 2024-03-06, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
>
>>> $ awk '{print $1, "1-1"}' newsrc-news.eternal-september.org-test >
>>> newsrc-news.eternal-september.org
>>
>> In this specific case of regular data you can simplify that to
>>
>> awk '$2="1-1"' sourcefile > targetfile
>
> That had me scratching my head. You can't have an action without
> enclosing braces. But it's still legal syntax because... it's an
> expression serving as a pattern. The assignment itself is a side
> effect.

Without braces, the default action takes place, which is ``{print}''.

Re: "sed" question

<ushvsk$2a4o3$1@news.xmission.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=1897&group=comp.lang.awk#1897

  copy link   Newsgroups: comp.lang.awk
Path: i2pn2.org!i2pn.org!news.furie.org.uk!newsfeed.endofthelinebbs.com!panix!weretis.net!feeder6.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gazelle@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.lang.awk
Subject: Re: "sed" question
Date: Sat, 9 Mar 2024 15:40:04 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <ushvsk$2a4o3$1@news.xmission.com>
References: <us9vka$fepq$1@dont-email.me> <usagql$j9bc$1@dont-email.me> <slrnuuolcp.2g4k.naddy@lorvorc.mips.inka.de> <877cibsbja.fsf@yaxenu.org>
Injection-Date: Sat, 9 Mar 2024 15:40:04 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="2429699"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Sat, 9 Mar 2024 15:40 UTC

In article <877cibsbja.fsf@yaxenu.org>, Julieta Shem <jshem@yaxenu.org> wrote:
>Christian Weisgerber <naddy@mips.inka.de> writes:
>
>> On 2024-03-06, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
>>
>>>> $ awk '{print $1, "1-1"}' newsrc-news.eternal-september.org-test >
>>>> newsrc-news.eternal-september.org
>>>
>>> In this specific case of regular data you can simplify that to
>>>
>>> awk '$2="1-1"' sourcefile > targetfile
>>
>> That had me scratching my head. You can't have an action without
>> enclosing braces. But it's still legal syntax because... it's an
>> expression serving as a pattern. The assignment itself is a side
>> effect.
>
>Without braces, the default action takes place, which is ``{print}''.

Somehow, I think Christian knows that (since everybody knows that).

My guess is that he just doesn't like it...

--
"Everything Roy (aka, AU8YOG) touches turns to crap."
--citizens of alt.obituaries--

Re: "sed" question

<usi44f$2dqiq$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=1898&group=comp.lang.awk#1898

  copy link   Newsgroups: comp.lang.awk
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: mortonspam@gmail.com (Ed Morton)
Newsgroups: comp.lang.awk
Subject: Re: "sed" question
Date: Sat, 9 Mar 2024 10:52:31 -0600
Organization: A noiseless patient Spider
Lines: 50
Message-ID: <usi44f$2dqiq$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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 9 Mar 2024 16:52:31 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="a206cc96e2aad182ba2b41de10120794";
logging-data="2550362"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1//VDIy2oFRiDp3daqXj3WK"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:WzW2lqzSQ7oWi7XlH+cr748qHK8=
In-Reply-To: <slrnuuolcp.2g4k.naddy@lorvorc.mips.inka.de>
Content-Language: en-US
X-Antivirus: Avast (VPS 240309-2, 3/9/2024), Outbound message
X-Antivirus-Status: Clean
 by: Ed Morton - Sat, 9 Mar 2024 16:52 UTC

On 3/9/2024 6:27 AM, Christian Weisgerber wrote:
> On 2024-03-06, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
>
>>> $ awk '{print $1, "1-1"}' newsrc-news.eternal-september.org-test >
>>> newsrc-news.eternal-september.org
>>
>> In this specific case of regular data you can simplify that to
>>
>> awk '$2="1-1"' sourcefile > targetfile
>
> That had me scratching my head. You can't have an action without
> enclosing braces. But it's still legal syntax because... it's an
> expression serving as a pattern. The assignment itself is a side
> effect.
>
> Care needs to be taken when using this shortcut so the expression
> doesn't evalute as false:

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'

instead of:

$2="1-1"

unless they NEED the result of the action to be evaluated as a
condition, for that very reason.

Ed.

>
> $ printf 'one 1\ntwo 2\nthree 3\n' | awk '$2=4'
> one 4
> two 4
> three 4
> $ printf 'one 1\ntwo 2\nthree 3\n' | awk '$2=0'
> $
>
> $ printf 'one 1\ntwo 2\nthree 3\n' | awk '$2="4"'
> one 4
> two 4
> three 4
> $ printf 'one 1\ntwo 2\nthree 3\n' | awk '$2=""'
> $
>

Re: "sed" question

<usif44$2g8a3$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=1899&group=comp.lang.awk#1899

  copy link   Newsgroups: comp.lang.awk
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_papanagnou+ng@hotmail.com (Janis Papanagnou)
Newsgroups: comp.lang.awk
Subject: Re: "sed" question
Date: Sat, 9 Mar 2024 21:00:02 +0100
Organization: A noiseless patient Spider
Lines: 64
Message-ID: <usif44$2g8a3$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>
MIME-Version: 1.0
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 9 Mar 2024 20:00:04 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="436083f86b4caab9d993c9487dc033ba";
logging-data="2629955"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+sEscWWfe8oudQSRSA9uLt"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:76CkpPG5dmDOx7KcN7wR4pH2ZWE=
In-Reply-To: <slrnuuolcp.2g4k.naddy@lorvorc.mips.inka.de>
X-Enigmail-Draft-Status: N1110
 by: Janis Papanagnou - Sat, 9 Mar 2024 20:00 UTC

On 09.03.2024 13:27, Christian Weisgerber wrote:
> On 2024-03-06, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
>
>>> $ awk '{print $1, "1-1"}' newsrc-news.eternal-september.org-test >
>>> newsrc-news.eternal-september.org
>>
>> In this specific case of regular data you can simplify that to
>>
>> awk '$2="1-1"' sourcefile > targetfile
>
> That had me scratching my head.

Part of the joy programming in Awk. ;-)

> You can't have an action without
> enclosing braces. But it's still legal syntax because...

> it's an expression serving as a pattern.

This is the key observation!

Here we have only a condition in the general condition { action }

> The assignment itself is a side effect.

Assignments generally have a side effect, inherently. :-)

>
> Care needs to be taken when using this shortcut so the expression
> doesn't evalute as false:

I've carefully formulated "In this specific case of regular data ..."

>
> $ printf 'one 1\ntwo 2\nthree 3\n' | awk '$2=4'
> one 4
> two 4
> three 4
> $ printf 'one 1\ntwo 2\nthree 3\n' | awk '$2=0'
> $
>
> $ printf 'one 1\ntwo 2\nthree 3\n' | awk '$2="4"'
> one 4
> two 4
> three 4
> $ printf 'one 1\ntwo 2\nthree 3\n' | awk '$2=""'
> $
>

Other questions on the data may be whether...
- the article number list may contain spaces
- the space after the colon is always existing
- blank lines may be existing in the file
- comment lines are possible in the file

These all will require a more "complex" awk pattern or action, yet
still simply solvable. Maybe something like

BEGIN { FS=":[[:space:]]*" }
!NF || /^[[:space:]]*#/ || $0=$1": 1-1"

Janis

Re: "sed" question

<usifh6$2gb9r$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=1900&group=comp.lang.awk#1900

  copy link   Newsgroups: comp.lang.awk
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_papanagnou+ng@hotmail.com (Janis Papanagnou)
Newsgroups: comp.lang.awk
Subject: Re: "sed" question
Date: Sat, 9 Mar 2024 21:07:00 +0100
Organization: A noiseless patient Spider
Lines: 28
Message-ID: <usifh6$2gb9r$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>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 9 Mar 2024 20:07:02 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="436083f86b4caab9d993c9487dc033ba";
logging-data="2633019"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/ygZrESzl0bJNeaLfx4fd8"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:Qe9UfR0yswC0KWcwcdalQeghqrE=
In-Reply-To: <usi44f$2dqiq$1@dont-email.me>
 by: Janis Papanagnou - Sat, 9 Mar 2024 20:07 UTC

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". If you want to avoid cryptic code
you'd rather write

'{$2="1-1"; print}'

Don't you think?

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

Re: "sed" question

<20240309124320.907@kylheku.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=1901&group=comp.lang.awk#1901

  copy link   Newsgroups: comp.lang.awk
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 433-929-6894@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.awk
Subject: Re: "sed" question
Date: Sat, 9 Mar 2024 20:49:43 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 40
Message-ID: <20240309124320.907@kylheku.com>
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>
Injection-Date: Sat, 9 Mar 2024 20:49:43 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="698d318672f9255d62d5495c3932d338";
logging-data="2648504"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/d/fZolHi8VHVM2KeFYbOvRTt1lX6/eGk="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:thLY9Sm2TLE0gcAtGL7Iw7lYJ/c=
 by: Kaz Kylheku - Sat, 9 Mar 2024 20:49 UTC

On 2024-03-09, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> 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". If you want to avoid cryptic code
> you'd rather write
>
> '{$2="1-1"; print}'
>
> Don't you think?

I don't remember it either, but it's a no brainer that '$2=expr'
is incorrect if expr is arbitrary, and the intent is that
the implicit print is to be unconditionally invoked.

If expr is a nonblank, nonzero literal term, then the assignment
is obviously true and '$2=literal' as the entire program is a fine
idiom.

I don't agree with putting braces around it and adding 1, or explicit
print.

You are not then using Awk like it was meant to be.

When Awk was conceived, the authors peered into a crystal ball and saw
Perl. After the laughter died down, they got serious and made sure to
provide for idioms like:

awk '!s[$0]++'

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: "sed" question

<usmqg3$3j5ib$3@toylet.eternal-september.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=1904&group=comp.lang.awk#1904

  copy link   Newsgroups: comp.lang.awk
Path: i2pn2.org!i2pn.org!news.nntp4.net!news.gegeweb.eu!gegeweb.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!toylet.eternal-september.org!.POSTED!not-for-mail
From: toylet.toylet@gmail.com (Mr. Man-wai Chang)
Newsgroups: comp.lang.awk
Subject: Re: "sed" question
Date: Mon, 11 Mar 2024 19:38:42 +0800
Organization: A noiseless patient Spider
Lines: 27
Message-ID: <usmqg3$3j5ib$3@toylet.eternal-september.org>
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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 11 Mar 2024 11:38:43 -0000 (UTC)
Injection-Info: toylet.eternal-september.org; posting-host="e10f395b0563ffe3f32c382f0dc3e156";
logging-data="3774027"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/p+fKTqY4P7KBQlNEOiHYT"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:LeqOGjVKQbAPLHAsNcUnm4vKF7g=
In-Reply-To: <usi44f$2dqiq$1@dont-email.me>
Content-Language: en-US
 by: Mr. Man-wai Chang - Mon, 11 Mar 2024 11:38 UTC

On 10/3/2024 12:52 am, 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'
>
> instead of:
>
> $2="1-1"
>
> unless they NEED the result of the action to be evaluated as a
> condition, for that very reason.

You might Google about it, but Google has unplugged its Usenet support.
I dunno whether you could search old Usenet messages. There is still
Wayback Machine archive.

awk '{$2="1-1"} 1' - Google Search
<https://www.google.com/search?q=awk+%27%7B%242%3D%221-1%22%7D+1%27>

Internet Archive: Wayback Machine
<https://archive.org/web/>

Google Groups ditches links to Usenet, the OG social network • The Register
<https://www.theregister.com/2023/12/18/google_ends_usenet_links/>

Re: "sed" question

<87msr4ztyl.fsf@nosuchdomain.example.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=1907&group=comp.lang.awk#1907

  copy link   Newsgroups: comp.lang.awk
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith.S.Thompson+u@gmail.com (Keith Thompson)
Newsgroups: comp.lang.awk
Subject: Re: "sed" question
Date: Mon, 11 Mar 2024 08:09:06 -0700
Organization: None to speak of
Lines: 26
Message-ID: <87msr4ztyl.fsf@nosuchdomain.example.com>
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>
<usmqg3$3j5ib$3@toylet.eternal-september.org>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="803e1d91970c7135fb6b17cffb70db2d";
logging-data="3868047"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18VNff/0gNJs2IL2SIPuIQd"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:ANxtiuTJo8ryepk9jT61oW6toks=
sha1:B1AD2J7cFdsRjw4MInlhdFWIy7o=
 by: Keith Thompson - Mon, 11 Mar 2024 15:09 UTC

"Mr. Man-wai Chang" <toylet.toylet@gmail.com> writes:
> On 10/3/2024 12:52 am, 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'
>> instead of:
>> $2="1-1"
>> unless they NEED the result of the action to be evaluated as a
>> condition, for that very reason.
>
> You might Google about it, but Google has unplugged its Usenet
> support. I dunno whether you could search old Usenet messages. There
> is still Wayback Machine archive.
[...]

You might Google about it yourself before posting.

Google Groups has shut down its Usenet interface, but messages posted
before 2024-02-22 are still available.

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Medtronic
void Void(void) { Void(); } /* The recursive call of the void */

Re: "sed" question

<usqm57$hs0n$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=1922&group=comp.lang.awk#1922

  copy link   Newsgroups: comp.lang.awk
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: mortonspam@gmail.com (Ed Morton)
Newsgroups: comp.lang.awk
Subject: Re: "sed" question
Date: Tue, 12 Mar 2024 17:49:09 -0500
Organization: A noiseless patient Spider
Lines: 77
Message-ID: <usqm57$hs0n$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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 12 Mar 2024 22:49:11 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="259e76a226586613e5049e2673b1ef49";
logging-data="585751"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19LDwJOCeBiBCABCrmMyglX"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:xsmBR9/V84ZHsKC6X9BPQ5jF7c8=
In-Reply-To: <usifh6$2gb9r$1@dont-email.me>
Content-Language: en-US
X-Antivirus-Status: Clean
X-Antivirus: Avast (VPS 240312-6, 3/12/2024), Outbound message
 by: Ed Morton - Tue, 12 Mar 2024 22:49 UTC

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

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

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.

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
>

Re: "sed" question

<ustlpc$18o8j$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=1938&group=comp.lang.awk#1938

  copy link   Newsgroups: comp.lang.awk
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_papanagnou+ng@hotmail.com (Janis Papanagnou)
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>
 by: Janis Papanagnou - Thu, 14 Mar 2024 02:01 UTC

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

Re: "sed" question

<usujjp$1i4ia$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=1941&group=comp.lang.awk#1941

  copy link   Newsgroups: comp.lang.awk
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: mortonspam@gmail.com (Ed Morton)
Newsgroups: comp.lang.awk
Subject: Re: "sed" question
Date: Thu, 14 Mar 2024 05:30:16 -0500
Organization: A noiseless patient Spider
Lines: 199
Message-ID: <usujjp$1i4ia$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> <ustlpc$18o8j$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 14 Mar 2024 10:30:17 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="42ac1681d1342478fe121f06fe65a95c";
logging-data="1643082"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19rVTUhhWbwTY4TnJA/+43/"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:XVUmsgkzHhpBrN183sy0p9RXo3w=
X-Antivirus-Status: Clean
In-Reply-To: <ustlpc$18o8j$1@dont-email.me>
Content-Language: en-US
X-Antivirus: Avast (VPS 240313-16, 3/13/2024), Outbound message
 by: Ed Morton - Thu, 14 Mar 2024 10:30 UTC

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

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor