Sujet : Re: "sed" question
De : mortonspam (at) *nospam* gmail.com (Ed Morton)
Groupes : comp.lang.awkDate : 09. Mar 2024, 17:52:31
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <usi44f$2dqiq$1@dont-email.me>
References : 1 2 3 4
User-Agent : Mozilla Thunderbird
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=""'
$