News for hackers. Stuff that matters.
Re:Awk vs. Perl (2009) by omaranto on Tuesday June 27, 2017 @02:34PM (#14647980)
Could you give some examples of one-liners you feel might be more elegant in Awk than in Perl? It might be fun to get a thread going where people try to give clean Perl versions.
Re:Awk vs. Perl (2009) by majewsky on Tuesday June 27, 2017 @03:49PM (#14648738)
Basically anything that uses $1, $2,... because you save a split().

  awk '$4 ~ /T/ { print $1 }'
  perl -nE '@x = split /\s+/; say $x[0] if $x[3] =~ /T/;'
... stares at this and wonders ... reads `man perlrun` ... Oooh, there's a switch to auto-split-on-whitespace in Perl.

  perl -anE 'say $F[0] if $F[3] =~ /T/;'
Sooo this doesn't quite answer your question anymore, but I'm going to leave this here since it's a nice instance of TIL.
Re:Awk vs. Perl (2009) by mitchty on Tuesday June 27, 2017 @04:06PM (#14648893)
The best part about using the @F array, is you get more than 10 things you can split and dereference. $0-9 only in awk.

That and maybe because i'm an old perl die hard, its just easier to pipe to perl than remember how awk does its shenanigans.

Re:Awk vs. Perl (2009) by kazinator on Tuesday June 27, 2017 @05:50PM (#14649827)
Awk is not limited to $0 to $9; where did you get that??? Maybe you're confusing this with register substitutions in regex: \1 through \9.

The nice thing about the @F array is that @F[0] doesn't represent the whole record.

There is a class of Awk bugs whereby arithmetic is being done on the parameters using $N (where N is some calculated variable containing an integer). Everything is fine when N >= 1, but if there is a bug where N is accidentally zero, then $N refers to the whole record instead of throwing an out-of-bounds error.

And things are accidentally zero quite easily in awk, e.g.

   awk '{print $X}'  # X is undefined so serves as zero; this prints all lines
Re:Awk vs. Perl (2009) by mitchty on Tuesday June 27, 2017 @06:26PM (#14650115)
Solaris awk. :)
Re:Awk vs. Perl (2009) by kazinator on Wednesday June 28, 2017 @01:08AM (#14651821)
I see on my Solaris 10 VM that indeed the "broken old awk" doesn't work past $9.

However, on the same OS, "nawk" does.

Re:Awk vs. Perl (2009) by mitchty on Wednesday June 28, 2017 @10:30AM (#14654411)
Yep, there is also /usr/xpg4/bin/awk vs /usr/ucb/bin/awk etc...

I've long since had to deal with Solaris but for about 10 years of Solaris I always treat awk with a lowest common denominator approach. Which means I tend to avoid it even to this day.

Re:Awk vs. Perl (2009) by kazinator on Wednesday June 28, 2017 @11:24AM (#14654897)

  > perl -anE 'say $F[0] if $F[3] =~ /T/;'
                                        ^
                                        ^
Please tell me that isn't required. Fail!
Re:Awk vs. Perl (2009) by omaranto on Wednesday June 28, 2017 @12:39PM (#14655633)
Don't worry, it's not required.
Re:Awk vs. Perl (2009) by kazinator on Tuesday June 27, 2017 @05:59PM (#14649902)

  txr -e '(awk ([#/T/ [f 3]] (prn [f 0])))'
-e is just "evaluate Lisp expression for side effects; don't print its value"
Re:Awk vs. Perl (2009) by omaranto on Wednesday June 28, 2017 @01:55AM (#14651993)
Oh, that looks nice. I'm guessing this in TXR Lisp (http://www.nongnu.org/txr/), and that TXR is your project.
Re:Awk vs. Perl (2009) by kazinator on Tuesday June 27, 2017 @04:59PM (#14649380)
Awk one-liners don't depend on cryptic combinations of one-letter option flags to change the behavior of something in the language.
Re:Awk vs. Perl (2009) by jwilk on Tuesday June 27, 2017 @04:04PM (#14648875)
You can save one character:

-a implicitly sets -n.