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.
| [reply] |
|
| [reply] [d/l] |
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 | [reply] [d/l] [select] |
|
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
| [reply] [d/l] |
|
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 | [reply] [d/l] |
Re: VVP:Perl oneliners for Unix commands
by $code or die (Deacon) on Mar 13, 2002 at 18:08 UTC
|
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;
| [reply] [d/l] |
|
| [reply] [d/l] |
|
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 | [reply] [d/l] [select] |
|
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") | [reply] [d/l] [select] |
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.
| [reply] [d/l] [select] |