Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

VVP:Perl oneliners for Unix commands

by vivekvp (Acolyte)
on Mar 13, 2002 at 17:26 UTC ( [id://151450]=perlquestion: print w/replies, xml ) Need Help??

vivekvp has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I am trying this from a Unix prompt:
$ ls Q.PACES* sh: /usr/bin/ls: The parameter list is too long.
Is there a Perl oneliner - that will make this work - provide me of files begining with Q.PACES. And do Perl oneliners work within Unix scripts (ksh)? Thanks V He who laughs last, doesn't get the joke.

Replies are listed 'Best First'.
Re: Perl oneliners for Unix commands
by Kanji (Parson) on Mar 13, 2002 at 18:17 UTC
    If all you got is a hammer, then everthing starts lookin' like a nail.

    A Perl one-liner is complete overkill for something like this, especially when you have so many more appropriate/less obfu-looking tools and solutions at hand ...

    • echo Q.PACES*
    • ls | grep '^Q\.PACES'
    • find . -name 'Q.PACES*' -print

    Updates:

    The echo trick may be Bourne/Korn-centric; buckaduck reports (and I've verified) that it doesn't work under csh.

    Also corrected the search strings for grep and find.

        --k.


      I agree. Overkill.

      BTW, you probably want single (non-interpolating) quotes in your third item, otherwise you run into the same problem as the first one (and the original question) had:

      • find . -name 'Q.PACES*' -print

      dmm

      If you GIVE a man a fish you feed him for a day
      But,
      TEACH him to fish and you feed him for a lifetime
Re: VVP:Perl oneliners for Unix commands
by buckaduck (Chaplain) on Mar 13, 2002 at 18:01 UTC
    Off topic, but a pure UNIX solution might be:
    ls | grep '^Q\.PACES'

    Updated Now I escape the literal period in the regex.

    Update 2: I'm convinced that some of the other solutions will NOT work, including echo Q.PACES* and any Perl solution involving <Q.PACES*>. If the shell can't handle filename completion one way, it won't work another way either.
    Further testing shows that this depends on your shell and your version of Perl. Your mileage may vary...

    buckaduck

      buckaduck,

      The <Q.PACES*> will work in more instances than anything that that uses shell metacharacters. There are possible two limits you can reach here - the size of the command line your shell can read and two, the maximum number of arguments your exec can handle (getconf ARG_MAX). Most shells will bomb out because you reach the max for the command line buffer (expansion first by shell then exec). For those that don't hard code the cl buffer, you're going to hit the exec max.

      The perlfunc:glob function used to use csh to do it's trick but now it uses File::Glob to do it's work. Since that is is based on the bsd glob (man 3 glob) it should work up to your max mem (or there about).

      That being said, you do need to be careful using glob and the solutions that minimize memory (find, ls | grep, etc) are probably much better implementations if you're going to be doing a lot of this thing.

      -derby

      To do it using the shell, try this:
      echo Q.PACES* | xargs ls
      -- O thievish Night, Why should'st thou, but for some felonious end, In thy dark lantern thus close up the stars? --Milton
Re: VVP:Perl oneliners for Unix commands
by $code or die (Deacon) on Mar 13, 2002 at 18:08 UTC

    This should do the trick...

    perl -e 'print $_,$/ for (<Q.PACES*>)'
    ___ Simon Flack ($code or die)
    $,=reverse'"ro_';s,$,\$,;s,$,lc ref sub{},e;$,
    =~y'_"' ';eval"die";print $_,lc substr$@,0,3;

      If you use Perl's -l switch, you can simplify that further to just ...

      perl -le 'print for <Q.PACES*>'

          --k.


      Right, this will work with the newest perls (>= 5.6), at least if you use glob() instead of <>.   The problem is:
      $ ls Hold/* |wc /bin/ksh: /usr/bin/ls: arg list too long 0 0 0 $ ls Hold |wc 169170 169170 2739945 $ perl -lwe'@x=<"Hold/*">;print 0+@x' 0 $ perl -lwe'@x=glob"Hold/*";print 0+@x' 169170 $
      Since 5.6 glob does not use a shell to expand filesystem wildcards.

      update:   Gah! tye, of course, is right <Hold/*> is the same as glob"Hold/*".   (No '"'s inside <>.)

        p

        The following are all the same:

        glob"Hold/*" glob("Hold/*") <Hold/*>
        but <"Hold/*"> is the same as glob('"Hold/*"'), which doesn't do what you want. I expected it to print 1 instead of 0 but it probably notes that there is no file named exactly Hold/* and so returns nothing.

                - tye (but my friends call me "Tye")
Re: VVP:Perl oneliners for Unix commands
by VSarkiss (Monsignor) on Mar 13, 2002 at 17:35 UTC

    If you just want a list: perl -e 'opendir D, "."; print join("\n", grep {/Q\.PACES/} readdir D), "\n";'It's ugly, it doesn't check errors, etc.

    A slight improvement is to sort alphabetically: perl -e 'opendir D, "."; print join("\n",sort grep {/Q\.PACES/} readdir D), "\n";' This gets kinda obfuscated; you may just want to write a small program that checks errors and such.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://151450]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-24 01:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found