Hacker News new | ask | show | jobs
by perlperson 2869 days ago

  $ docker ps | rb drop 1 | rb -l split[1]
  $ docker ps | perl -anE 'say $F[1] if $.>1'

perl solved this problem a long time ago, people
5 comments

for your given sample, you could use awk[0] as well

    awk 'NR>1{print $2}'
or ruby[1]

    ruby -ane 'puts $F[1] if $.>1'
which one to use depends on lots of factor - speed, features, availability, etc as well as whether user already knows perl[2]/ruby/etc

Further reading(disclosure: I wrote these)

[0]: https://github.com/learnbyexample/Command-line-text-processi...

[1]: https://github.com/learnbyexample/Command-line-text-processi...

[2]: https://github.com/learnbyexample/Command-line-text-processi...

Yeah, just like eating dirt solved the problem of world hunger.

Look again at your code. I'm not 100% sure what the Ruby version does, but once I do figure out the exact semantics of drop and split, it's going to be waaaaaaaaaaaaaaaaay easier to remember, understand and modify the Ruby version than the Perl one.

Your post comes off as something from reddit's /r/nottheonion, you're just making the case against Perl for Perl-haters :)

The perl here is not really that crazy to remember.

-a autosplits each line by whitespace and puts each element into the array F (this was inspired by AWK).

-n loops through each line of the file, and -E executes the perl.

$. (NR in AWK) is the line number.

As others have noted, you can write the same thing in ruby on the command line already with `ruby -ane 'puts $F[1] if $.>1'`

(Notice the similarities?)

If you write one liners in AWK, Perl, or Ruby often the "odd" variables look more like useful shortcuts.

*edit You could also write the perl without any of the special variables, but it would be much more verbose, hence the special characters and flags.

Except that in your example, the first line is coherent English, and the second line is just... well... code.
Personally, when I write shell commands, they tend to be write-only code, because the shell isn't really suitable for anything more complex. So it's easier to think in code than it is to add the extra step of translating to English if it's something nobody's gonna see again anyway.
My counter to this would be - if I'm doing a thing, and I need to look back through my shell history a few days or weeks later to figure out how I did it (especially if something went wrong), seeing a more readable version is going to help me figure it out faster.

Granted, anything really nontrivial I'll usually just write a Python script for, but the point stands.

but the ruby code is much more intuitively readable
It is if you know ruby. Me, I know perl and honestly I do not grok most of the ruby examples given here. I am quite sure that I could learn them about as fast as somebody not knowing perl and knowing ruby could learn perl.
I know both languages; the ruby version is better IMO. The verbiage is English... the syntax is a bit odd but so is the perl version's
Sorry, it was solved too long ago, I can't read what it says.