Sujet : Re: "sed" question
De : naddy (at) *nospam* mips.inka.de (Christian Weisgerber)
Groupes : comp.unix.shell comp.lang.awkSuivi-à : comp.lang.awkDate : 09. Mar 2024, 13:27:05
Autres entêtes
Message-ID : <slrnuuolcp.2g4k.naddy@lorvorc.mips.inka.de>
References : 1 2 3
User-Agent : slrn/1.0.3 (FreeBSD)
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