Home > Archive > PERL Miscellaneous > April 2005 > one-time and one-line programs
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
one-time and one-line programs
|
|
| Michael De Tomaso 2005-04-25, 8:56 pm |
| In reviewing the -p -e -n command line options, I've been trying to
find/understand more examples...
Q:I'd like to know how many perl people keep a stash of one liners and what
are their favorite quickie code concoctions might be ?
- thanks & take care M.D.
<aka>
_ _
| |_ ___ _ __ ___ __ _ | |_ ___ ___
| __| / _ \ | '_ ` _ \ / _` | | __| / _ \ / _ \
| |_ | (_) | | | | | | | | (_| | | |_ | (_) | | __/
\__| \___/ |_| |_| |_| \__,_| \__| \___/ \___|
| |
| peter pilsl 2005-04-25, 8:56 pm |
| Michael De Tomaso wrote:
> In reviewing the -p -e -n command line options, I've been trying to
> find/understand more examples...
>
> Q:I'd like to know how many perl people keep a stash of one liners and what
> are their favorite quickie code concoctions might be ?
>
ly I dont keep them, but I use them every day. Whenever I have to
deep-in analyze a log or convert some files or process a longer output
of find or ls I write a small one-line-filter in perl.
And I use to test regular expressions in a one-liner before writing them
inside the big project.
And I use one-liners to run a command n times, cause I never can
remember the bash-syntax for this.
peter
--
http://www.goldfisch.at/know_list
| |
| xhoster@gmail.com 2005-04-25, 8:57 pm |
| "Michael De Tomaso" <mdetomaso@bak.rr.com> wrote:
> In reviewing the -p -e -n command line options, I've been trying to
> find/understand more examples...
>
> Q:I'd like to know how many perl people keep a stash of one liners and
I very rarely save one liners, other than in my scrollback buffer. If a
one-liner becomes so complex that it would no longer be trivial to recreate
it, I figure it deserves to have more than one line.
> what are their favorite quickie code concoctions might be ?
Here's are a couple I used recently:
time perl -e 'system q{perl -e "use strict;use CGI"} foreach 1..100'
time perl -e 'system q{perl -e "use strict;use DBI"} foreach 1..100'
Also, I have "deparse" aliased to
perl -MO=Deparse,-p
So that I can easily check out constructs for which I'm not sure of the
precedence, associativity, or parsing.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
| |
|
|
| Tad McClellan 2005-04-25, 8:57 pm |
| Michael De Tomaso <mdetomaso@bak.rr.com> wrote:
> In reviewing the -p -e -n command line options, I've been trying to
> find/understand more examples...
>
> Q:I'd like to know how many perl people keep a stash of one liners and what
> are their favorite quickie code concoctions might be ?
perl -n -e 'print' file1 file2... # cat
perl -pe1 file1 file2.... # Tiger Woods cat
perl -n -e 'print if /PATTERN/' file1 file2... # grep -h
perl -n -e 'print "$ARGV: $_" if /PATTERN/' file1... # grep -H
perl -p -i -e 'tr/\r//d' file1 file2... # dos2unix
perl -p -i -e 's/\n/\r\n/' file1 file2... # unix2dos
perl -p -e 'tr/a-zA-Z/n-za-mN-ZA-M/' file1... # rot13
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
| |
| Wes Groleau 2005-04-26, 3:59 am |
| Michael De Tomaso wrote:
> Q:I'd like to know how many perl people keep a stash of one liners and what
> are their favorite quickie code concoctions might be ?
If it's worth keeping, it's worth keeping in a script and
adding a little bit of flexibility to it.
The only one-liners I can remember using in a long time are
search-and-replace, i.e.,
perl -p -i -e 's^old^new^g;' *
--
Wes Groleau
Answer not a fool according to his folly,
lest thou also be like unto him.
Answer a fool according to his folly,
lest he be wise according to his own conceit.
-- Solomon
Are you saying there's no good way to answer a fool?
-- Groleau
| |
| Bart Lateur 2005-04-26, 8:58 am |
| John Bokma wrote:
>Since I rarely use -p, -n, -s, -F, it might be not the best stuff to read.
>
>I use -e often to check something, or do something that's easier with perl
>compared to the cli.
I disagree for -p and -n, they're not in the same league as -e, but stil
lvery useful.
And another one to use is -l (little L), because then a print will add a
newline. Easier than typing "\n".
perl -le "print for 1 .. 3"
--
Bart.
| |
| John Bokma 2005-04-26, 8:58 am |
| Bart Lateur wrote:
> John Bokma wrote:
>
>
> I disagree for -p and -n, they're not in the same league as -e, but
> stil lvery useful.
Ok, should probably have read: I rarely use -p, -n, so the info on my page
might not be the best info to read on the usage of -p and -n. (To be honest
it was actually a refresher to do that, and that's what I like about
Usenet, keeps one sharp).
> And another one to use is -l (little L), because then a print will add
> a newline. Easier than typing "\n".
>
> perl -le "print for 1 .. 3"
I am going to do my best to remember that one, I use too often qq(...) with
-e (cmd.exe). Thanks.
--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
| |
| Michael De Tomaso 2005-04-26, 8:57 pm |
| "Wes Groleau" <groleau+news@freeshell.org> wrote in message
news:d4kfb8$p5g$2@domitilla.aioe.org...
> Michael De Tomaso wrote:
>
> If it's worth keeping, it's worth keeping in a script and
> adding a little bit of flexibility to it.
>
> The only one-liners I can remember using in a long time are
> search-and-replace, i.e.,
>
> perl -p -i -e 's^old^new^g;' *
>
> --
> Wes Groleau
>
> Answer not a fool according to his folly,
> lest thou also be like unto him.
> Answer a fool according to his folly,
> lest he be wise according to his own conceit.
> -- Solomon
>
> Are you saying there's no good way to answer a fool?
> -- Groleau
Thank you all. And in an abstract sense I do keep one-liners, but in an ole
ASCII file that I keep year by year, and DOS-grep it to death as time
marches on, then cut-n-paste the bits of wisdom that have been collected, or
at least I thought it was hot at the time, and occasionally I'll keep an
additional logs for certain subjects, etc, for example:
D:\BAT>grep.exe books d:\data\textfile.(s)\daily\PLOG2005.LOG
file ...
R:\Drive-I.L\internet\uuasc.org\perl_books_UUASC_library-requests.txt
- take care M.D.
The Fool On The Hill
John Lennon and Paul McCartney
Verse 1
Day after day alone on a hill the
man with the fool grin is keeping perfectly still, But
nobody wants to know him, they can see that he's justa fool and
he never gives an answer.
Chorus
But the fool on the hill sees the sun going down and the
eyes in his head see the world spinning round
...---... get in the SUB Jeremy :)
| |
| Abigail 2005-04-26, 8:57 pm |
| Tad McClellan (tadmc@augustmail.com) wrote on MMMMCCLV September MCMXCIII
in <URL:news:slrnd6qo4q.ruu.tadmc@magna.augustmail.com>:
)) Michael De Tomaso <mdetomaso@bak.rr.com> wrote:
)) > In reviewing the -p -e -n command line options, I've been trying to
)) > find/understand more examples...
)) >
)) > Q:I'd like to know how many perl people keep a stash of one liners and what
)) > are their favorite quickie code concoctions might be ?
))
))
)) perl -n -e 'print' file1 file2... # cat
))
)) perl -pe1 file1 file2.... # Tiger Woods cat
))
)) perl -n -e 'print if /PATTERN/' file1 file2... # grep -h
))
)) perl -n -e 'print "$ARGV: $_" if /PATTERN/' file1... # grep -H
))
)) perl -p -i -e 'tr/\r//d' file1 file2... # dos2unix
))
)) perl -p -i -e 's/\n/\r\n/' file1 file2... # unix2dos
))
)) perl -p -e 'tr/a-zA-Z/n-za-mN-ZA-M/' file1... # rot13
perl -pe '}{*_=*.}{' file # wc -l
perl -F: -lane 'print $F [2]' /etc/passwd # print UIDs.
perl -ple '$_=y___c' file1 file2 ... # Print line lengths
Abigail
--
perl -MLWP::UserAgent -MHTML::TreeBuilder -MHTML::FormatText -wle'print +(
HTML::FormatText -> new -> format (HTML::TreeBuilder -> new -> parse (
LWP::UserAgent -> new -> request (HTTP::Request -> new ("GET",
"http://work.ucsd.edu:5141/cgi-bin/http_webster?isindex=perl")) -> content))
=~ /(.*\))[-\s]+Addition/s) [0]'
| |
| Anno Siegel 2005-04-27, 3:58 pm |
| Abigail <abigail@abigail.nl> wrote in comp.lang.perl.misc:
> perl -pe '}{*_=*.}{' file # wc -l
Nice. Why {*_=*.} and not {$_=$.}? Just obfuscation?
Anno
| |
| jl_post@hotmail.com 2005-04-27, 3:58 pm |
| Michael De Tomaso wrote:
>
> In reviewing the -p -e -n command line options, I've been
> trying to find/understand more examples...
>
> Q:I'd like to know how many perl people keep a stash of
> one liners and what are their favorite quickie code
> concoctions might be ?
Dear Michael,
I once posted a message that had many of my favorite Perl
one-liners. You can read it at:
http://groups-beta.google.com/group...fc7836bf86bb143
(or at this link: http://tinyurl.com/9ozm7 ).
Anyway, I recentry wrote one because I needed to see just the first
five lines of a long text file and I didn't want to go through the
trouble of opening a text editor. If I were on Unix, I could have
written this:
head -5 file.txt
But since I was on a Win32 machine (with Perl installed), I used this
instead:
perl -ne "print if 1 .. 5" file.txt
My most two common Perl one-liners don't use the -n or -p switches,
however. They are:
perl -le "print 0xf0"
to find out what the decimal value of a hexadecimal number is (in this
case, 0x00f0), and:
perl -le "print scalar gmtime 1234567890"
to convert an epoch time (in this case, 1234567890) to a human-readable
date. I debug a lot of date-oriented code on Win32 systems, so this
last one-liner is extremely helpful to me.
I hope this has been useful, Michael.
-- Jean-Luc
| |
| Michael De Tomaso 2005-04-27, 8:57 pm |
|
<jl_post@hotmail.com> wrote in message
news:1114614481.800780.106150@g14g2000cwa.googlegroups.com...
> Michael De Tomaso wrote:
>
>
> Dear Michael,
>
> I once posted a message that had many of my favorite Perl
> one-liners. You can read it at:
>
> http://groups-beta.google.com/group...fc7836bf86bb143
>
> (or at this link: http://tinyurl.com/9ozm7 ).
>
> Anyway, I recentry wrote one because I needed to see just the first
> five lines of a long text file and I didn't want to go through the
> trouble of opening a text editor. If I were on Unix, I could have
> written this:
>
> head -5 file.txt
>
> But since I was on a Win32 machine (with Perl installed), I used this
> instead:
>
> perl -ne "print if 1 .. 5" file.txt
>
> My most two common Perl one-liners don't use the -n or -p switches,
> however. They are:
>
> perl -le "print 0xf0"
>
> to find out what the decimal value of a hexadecimal number is (in this
> case, 0x00f0), and:
>
> perl -le "print scalar gmtime 1234567890"
>
> to convert an epoch time (in this case, 1234567890) to a human-readable
> date. I debug a lot of date-oriented code on Win32 systems, so this
> last one-liner is extremely helpful to me.
>
> I hope this has been useful, Michael.
>
> -- Jean-Luc
>
Jean-Luc, WOW, I've hit the JackPOT
> head -5 file.txt
Head for DOS ... Unix-like utilities for DOS
Try Googling this: uxutl23a.zip
D:\BIN\UNIX-ON.DOS>head -10 useful~1.txt
1:13 PM 4/27/2005 ... from http://tinyurl.com/9ozm7 - Jean-Luc
-----------------
Useful one-liners and other short Perl scripts
J. Romano Jul 11 2004, 5:22 pm show options
Newsgroups: comp.lang.perl.misc
From: jl_p...@hotmail.com (J. Romano) - Find messages by this author
Date: 11 Jul 2004 17:22:18 -0700
Local: Sun,Jul 11 2004 5:22 pm
Subject: Useful one-liners and other short Perl scripts
- thanks againe & take care M.D.
<aka>
_ _
| |_ ___ _ __ ___ __ _ | |_ ___ ___
| __| / _ \ | '_ ` _ \ / _` | | __| / _ \ / _ \
| |_ | (_) | | | | | | | | (_| | | |_ | (_) | | __/
\__| \___/ |_| |_| |_| \__,_| \__| \___/ \___|
| |
| Michael De Tomaso 2005-04-27, 8:57 pm |
|
<jl_post@hotmail.com> wrote in message
news:1114614481.800780.106150@g14g2000cwa.googlegroups.com...
> Michael De Tomaso wrote:
>
>
> Dear Michael,
>
> I once posted a message that had many of my favorite Perl
> one-liners. You can read it at:
>
> http://groups-beta.google.com/group...fc7836bf86bb143
>
> (or at this link: http://tinyurl.com/9ozm7 ).
>
> Anyway, I recentry wrote one because I needed to see just the first
> five lines of a long text file and I didn't want to go through the
> trouble of opening a text editor. If I were on Unix, I could have
> written this:
>
> head -5 file.txt
>
> But since I was on a Win32 machine (with Perl installed), I used this
> instead:
>
> perl -ne "print if 1 .. 5" file.txt
>
> My most two common Perl one-liners don't use the -n or -p switches,
> however. They are:
>
> perl -le "print 0xf0"
>
> to find out what the decimal value of a hexadecimal number is (in this
> case, 0x00f0), and:
>
> perl -le "print scalar gmtime 1234567890"
>
> to convert an epoch time (in this case, 1234567890) to a human-readable
> date. I debug a lot of date-oriented code on Win32 systems, so this
> last one-liner is extremely helpful to me.
>
> I hope this has been useful, Michael.
>
> -- Jean-Luc
>
Jean-Luc, WOW, I've hit the JackPOT
> head -5 file.txt
Head for DOS ... Unix-like utilities for DOS
Try Googling this: uxutl23a.zip
D:\BIN\UNIX-ON.DOS>head -10 useful~1.txt
1:13 PM 4/27/2005 ... from http://tinyurl.com/9ozm7 - Jean-Luc
-----------------
Useful one-liners and other short Perl scripts
J. Romano Jul 11 2004, 5:22 pm show options
Newsgroups: comp.lang.perl.misc
From: jl_p...@hotmail.com (J. Romano) - Find messages by this author
Date: 11 Jul 2004 17:22:18 -0700
Local: Sun,Jul 11 2004 5:22 pm
Subject: Useful one-liners and other short Perl scripts
- thanks againe & take care M.D.
<aka>
_ _
| |_ ___ _ __ ___ __ _ | |_ ___ ___
| __| / _ \ | '_ ` _ \ / _` | | __| / _ \ / _ \
| |_ | (_) | | | | | | | | (_| | | |_ | (_) | | __/
\__| \___/ |_| |_| |_| \__,_| \__| \___/ \___|
| |
| Abigail 2005-04-27, 8:57 pm |
| Anno Siegel (anno4000@lublin.zrz.tu-berlin.de) wrote on MMMMCCLVII
September MCMXCIII in <URL:news:d4o4hm$q1v$1@mamenchi.zrz.TU-Berlin.DE>:
,, Abigail <abigail@abigail.nl> wrote in comp.lang.perl.misc:
,,
,, > perl -pe '}{*_=*.}{' file # wc -l
,,
,, Nice. Why {*_=*.} and not {$_=$.}? Just obfuscation?
The $-key already gets worn out a lot....
Abigail
--
perl -swleprint -- -_='Just another Perl Hacker'
| |
| John W. Krahn 2005-04-28, 3:59 am |
| jl_post@hotmail.com wrote:
>
> Anyway, I recentry wrote one because I needed to see just the first
> five lines of a long text file and I didn't want to go through the
> trouble of opening a text editor. If I were on Unix, I could have
> written this:
>
> head -5 file.txt
>
> But since I was on a Win32 machine (with Perl installed), I used this
> instead:
>
> perl -ne "print if 1 .. 5" file.txt
That has to read the whole file just to print the first five lines. This is
more efficient:
perl -pe "exit if $. > 5" file.txt
John
--
use Perl;
program
fulfillment
| |
|
|
| Wes Groleau 2005-04-28, 3:59 am |
| Anno Siegel wrote:
> Abigail <abigail@abigail.nl> wrote in comp.lang.perl.misc:
>
>
>
> Nice. Why {*_=*.} and not {$_=$.}? Just obfuscation?
Speaking of obfuscation, what's "nice" about using
fifteen characters, over half punctuation, to replace five?
Or did I misunderstand the comment?
--
Wes Groleau
When all you have is a perl, everything looks like a string.
|
|
|
|
|