News for hackers. Stuff that matters.
Re:Why Learn Awk? (2016) by shpeedy on Tuesday January 21, 2020 @12:57PM (#22109423)
Skip learning of sed and awk and jump straight to perl instead.

  $ perl --help
  ...
  -F/pattern/       split() pattern for -a switch (//'s are optional)
  -l[octal]         enable line ending processing, specifies line terminator
  -a                autosplit mode with -n or -p (splits $_ into @F)

  -n                assume "while (<>) { ... }" loop around program
  -p                assume loop like -n but print line also, like sed
  -e program        one line of program (several -e's allowed, omit programfile)

Example. List file name and size:

  ls -l | perl -ae 'print "@F[8..$#F], $F[4]"'
Re:Why Learn Awk? (2016) by just_myles on Tuesday January 21, 2020 @01:21PM (#22109737)
Because syntatically as a language/tool it is super easy to remember. Writing one liners with awk feels more intuitive to me.

Awk example:

ls -l | awk '{print $9, $5}' or ls -lh | awk '{print $9, $5}'

Seems a whole lot simpler. To me. I find if you have to write exhaustive shell scripts then maybe you can look for something more verbose like Perl, I guess.

Re:Why Learn Awk? (2016) by shpeedy on Tuesday January 21, 2020 @03:48PM (#22111387)
Yep, but you have bug in your awk one-liner.
Re:Why Learn Awk? (2016) by wahern on Tuesday January 21, 2020 @07:34PM (#22113369)
If you mean the lack of quotations, then the behavior is well-defined and is presumably what was intended. Per POSIX,

> The print statement shall write the value of each expression argument onto the indicated output stream separated by the current output field separator (see variable OFS above), and terminated by the output record separator (see variable ORS above).

The default value for OFS is <space> and for ORS, <newline>.

Re:Why Learn Awk? (2016) by demiol on Wednesday January 22, 2020 @07:33AM (#22116948)
> If you mean the lack of quotations,

No, lack of commas in output and broken filenames with spaces.

Re:Why Learn Awk? (2016) by just_myles on Wednesday January 22, 2020 @03:26PM (#22121628)
I see your point regarding spaces in names. Suppose I could use FILENAME. But I think my point was made.
Re:Why Learn Awk? (2016) by just_myles on Wednesday January 22, 2020 @03:14PM (#22121525)
In my defense I did this fairly quickly (Which was the point.) and was not trying to illustrate proper syntax (I mean it does run and does produces an output.).

ls -l | awk '{print $9 "\t" $5}'

That is about as much as i'm willing to do for this.

Re:Why Learn Awk? (2016) by wbazant on Tuesday January 21, 2020 @04:10PM (#22111609)
Also, because there's a whole culture of one-liners in Perl, you can also conveniently import libraries and call them:

Even though there's frequently value in adding whitespace to programs, many of them are just fine as one liners :)

e.g. this gets the title of a webpage for you: ``` $ perl -Mojo -E 'say g("mojolicious.org")->dom->at("title")->text' ```

Re:Why Learn Awk? (2016) by mongol on Tuesday January 21, 2020 @06:31PM (#22112934)
I don't know much Perl but 10+ years ago I read "Minimal Perl". For these purposes, I think it can act as the go-to tool.