top | item 22109423

(no title)

shpeedy | 6 years ago

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]"'

discuss

order

just_myles|6 years ago

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.

shpeedy|6 years ago

Yep, but you have bug in your awk one-liner.

wbazant|6 years ago

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' ```

mongol|6 years ago

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.