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