Sujet : Re: "sed" question
De : janis_papanagnou+ng (at) *nospam* hotmail.com (Janis Papanagnou)
Groupes : comp.lang.awkDate : 09. Mar 2024, 21:00:04
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <usif44$2g8a3$1@dont-email.me>
References : 1 2 3 4
User-Agent : Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0
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