Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Resource for command line "one-liner" interpretation

by graff (Chancellor)
on May 15, 2003 at 01:31 UTC ( #258302=note: print w/ replies, xml ) Need Help??


in reply to Resource for command line "one-liner" interpretation

For me, one of the essential features of command-line facility is the pipe. Notice what sorts of operations you need to use often on command lines (sorting, grepping, extracting one or more columns from a list of lines, doing string substitutions, etc); usually you'll find a unix utility that does just one of these things quite well, with a lot of flexibility in how you can do that one thing.

If you have a particular need that is not easy to do with an existing tool, write an simple tool in perl to make that operation easy on the command line, and put it in your PATH. A typical situation is: I want to locate all files whose names contain "x", and whose size is greater than 10240 bytes, and determine the total space consumed by these files. (This could certainly be done completely (and fairly easily) in perl, but using standard command line tools in combination with perl makes it even easier (a lot less typing) and offers a lot of flexibility, esp. if your command-line-interface is like bash, and makes it possible to recall, edit and re-execute earlier commands:

find . -name '*x*' -type f -printf '%s\n' | perl -ne 'chomp;$s+=$_ if( +$_>10240);END{print "$s\n"}'

I found myself needing to sum columns like this quite often, in many different situations, so I wrote a perl script to do just this (with options for flexibility), reducing the above example to:

find . -name '*x*' -type f -printf '%s\n' | sumcol -min 10240
In general, if I'm writing "perl -e '...'" or "perl -ne '...'" etc on a command line, I'm not using a system call within that perl script -- it's quicker (less typing) to use the command line for running unix tools, and to pipe their output to a perl script when necessary to accomplish other things.


Comment on Re: Resource for command line "one-liner" interpretation
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://258302]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (8)
As of 2014-12-27 01:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls