Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

What one-liners do people actually use?

by TomDLux (Vicar)
on Dec 08, 2005 at 18:53 UTC ( #515336=perlquestion: print w/ replies, xml ) Need Help??
TomDLux has asked for the wisdom of the Perl Monks concerning the following question:

People talk about one-liners, but it's usually just to say that someone's usefull little utility could be collapsed into an unreadable glumph of line noise .... wait, that's the deinition of Perl, isn't it?

But what one-liners do people actually use? I regularly alter Autosys meta-scripts to run a new version of a set of scripts, or to target a new database, or to run on a new host, using perl as a super-sed:

co -l *np40* perl -pi -e"m/host:/ && s/paias167/pa167c1n1/" ci -u -m'move scripts to new hosts' *np40*

--
TTTATCGGTCGTTATATAGATGTTTGCA

Comment on What one-liners do people actually use?
Download Code
Re: What one-liners do people actually use?
by exussum0 (Vicar) on Dec 08, 2005 at 19:03 UTC
    dos2unix
    perl -s -p -i -e '~s/\r//g' file.txt
      unix2dos (when run from "dos")
      perl -i.bak -ple1 file.txt
      The s flag (process switches) is useless here, isn't it?
        Prolly so, it's just such a habit.. like doing ~s///g is for some. Yes, I've accidentally written stuff like ~s/a$//g; now and then just 'cause I do /g so much. ^^
Re: What one-liners do people actually use?
by creamygoodness (Curate) on Dec 08, 2005 at 19:11 UTC
      How verbose!
      valgrind perl -e1
      Perl --((8:>*
Re: What one-liners do people actually use?
by rnahi (Curate) on Dec 08, 2005 at 19:18 UTC

      Missed that one when I did my search ... thanks.

      --
      TTTATCGGTCGTTATATAGATGTTTGCA

Re: What one-liners do people actually use?
by Tanktalus (Canon) on Dec 08, 2005 at 19:21 UTC
    1. Anything with the -i flag - that's how I started using perl (when sed/awk wasn't enough).
    2. perl -MData::Dumper -MSome::Module -e "print Dumper [Some::Module->new()->mytest(@ARGV)]" parm1 parm2 etc to test either how some other module works, or to test how my new code is working - a little bit easier/faster than doing full tests when I'm still developing. Or I use it to show my subordinate how to use an API and what it produces when we're sharing a desktop via VNC.
Re: What one-liners do people actually use?
by jeffa (Chancellor) on Dec 08, 2005 at 19:21 UTC

    Here is one i used today:

    perl -le'for (<*.mas>) {$o=$_;s/mas$/html/; rename $o,$_ }'
    I needed to change some .mas extensions to .html extensions.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    

      You could have just used BASH for this trivial task ;)

      for F in *.mas; do mv $F ${F/.mas/.html}; done

      Update: stupid typo fixed... Thanks jeffa

      --
      b10m

      All code is usually tested, but rarely trusted.

        HERETIC!!! >:(

        Just kidding! ;) I actually did pull off a BASH for loop:

        for i in `ls *.mas`; do tidy -asxhtml -clean -i -wrap 200 $i > $i.new; + done
        And then a Perl one-liner to strip off the .new ... and then i realized that tidy as a -m option to modify the file in place!

        UPDATE: Why Tanktalus? Because you weren't here at the time to show me a better way. :P Thanks for the tips, guys.

        jeffa

        L-LL-L--L-LL-L--L-LL-L--
        -R--R-RR-R--R-RR-R--R-RR
        B--B--B--B--B--B--B--B--
        H---H---H---H---H---H---
        (the triplet paradiddle with high-hat)
        
        Or rename mas html *.mas...
      You didn't need Perl for that:
      for %q in (*.mas) do ren %q %~nq.html
        ren *.mas *.html

            --k.


Re: What one-liners do people actually use?
by Kanji (Parson) on Dec 08, 2005 at 19:29 UTC

    A lot of my one-liners tend to be cheap knock-offs of everyday Unix commands to make my Windows use more productive without having to install Cygwin, Win32 ports, pure-Perl ports, etc.

    Seen particularly often...

    perl -ne "END { print $. }" # wc -l perl -de 1 # used as bc but could be anything :-)

        --k.


      I used to use perl -d -e 0or perl -d -e 1 but once I found out about perl -demo, I decided that was a lot cooler.

      --
      TTTATCGGTCGTTATATAGATGTTTGCA

Re: What one-liners do people actually use?
by ghenry (Vicar) on Dec 08, 2005 at 19:42 UTC

    See Favourite One-liners?

    HTH.

    Walking the road to enlightenment... I found a penguin and a camel on the way.....
    Fancy a yourname@perl.me.uk? Just ask!!!
Re: What one-liners do people actually use?
by SolidState (Scribe) on Dec 08, 2005 at 19:47 UTC

    Yesterday I wrote a hex2bin one-liner for someone at work:

    perl -ne'printf "%012b",hex $_' input.hex > output.bin
Re: What one-liners do people actually use?
by calin (Deacon) on Dec 08, 2005 at 19:51 UTC

    You don't "use" them, in the sense that you design them for an useful purpose, store them in a nice library etc. You have a problem, cook an on-liner, hope it works (fingers crossed) and fire it away :-) .

    This is how I do. Flame away.

Re: What one-liners do people actually use?
by vek (Prior) on Dec 08, 2005 at 19:52 UTC

    Over the years I've found that I'm far too lazy to keep typing one liners so they usually end up as a tiny script in my $HOME/bin. I found myself typing the following one liner this week and because of its length, it immediately became $HOME/bin/sectostr.

    perl -e 'use POSIX qw(strftime);print strftime("%H:%M:%S\n",974,0,0,0, +0,0);'

    -- vek --
      Another way to write that:
      perl -e'printf "%3\$02d:%2\$02d:%1\$02d\n", gmtime 974' :-)
Re: What one-liners do people actually use?
by ikegami (Pope) on Dec 08, 2005 at 20:34 UTC

    I gotta download rename.pl so I can stop typing the following all the time:

    dir /b | perl -ne "chomp; $o=$_; s/.../.../; $n=$_; rename($o,$n) unle +ss -e $n"

    I added unless -e $n because I've renamed an entire directory to one file name once, leaving me only one file total. Thankfully, I had a copy of the files elsewhere.

Re: What one-liners do people actually use?
by blokhead (Monsignor) on Dec 08, 2005 at 20:55 UTC
    I sometimes use these oneliners to add or average a list of numbers from STDIN.
    perl -lne'$x+=$_}{print$x' # sum perl -lne'$x+=$_}{print$x/$.' # avg
    There may be standard unix tools to do the same thing, but this is just as easy for me.

    blokhead

Re: What one-liners do people actually use?
by Perl Mouse (Chaplain) on Dec 08, 2005 at 21:21 UTC
    From my aliases file:
    alias lssize="ls | perl -e 'print for sort {length (\$a) <=> length (\ +$b)} <>'" alias L="ls -l | perl -pe 's/^(.{78})...*/\$1>/;' | page"
    But I write more one-liners in the shell, or in AWK.
    Perl --((8:>*
Re: What one-liners do people actually use?
by crashtest (Curate) on Dec 08, 2005 at 21:31 UTC

    I often find myself making use of the autosplit functionality on Windows (and even on UNIX, because I don't know much about awk).

    Just last month, a cow-worker asked me for a Perl script to extract and print three specific columns from a 33-column .csv file.

    perl -F, -ane "print join(',' => @F[0,8,9]), qq[\n] if (@F == 33)" inp +ut.txt

    My favorite perldoc has got to be perlrun.

      I would have written that as:
      perl -F, -alne '$, = ","; @F == 33 and print @F[0,8,9]' input.txt
      Perl --((8:>*
Re: What one-liners do people actually use?
by kscaldef (Pilgrim) on Dec 08, 2005 at 22:06 UTC
    My most frequent use of one-liners is for one-off reports / data analysis. The sort of thing where someone comes to your desk with a question; you fold, spindle, and multilate an apache access log, then give them a rough answer.
Re: What one-liners do people actually use?
by l3v3l (Monk) on Dec 08, 2005 at 23:20 UTC
    here is a liner I use to find reverse complementary / palindromic D|RNA sequences (thought this would be up your alley because of your sig.) :
    perl -nle 'tr/A-Z/a-z/;$m=length();reverse(substr($_,($m/2)+($m%2))) e +q substr($_,0,$m/2) ? print : next' /file/of/sequences/one_per_line
    works on any list of "words" tho ... /usr/share/dict/words, etc.

    update: ... this node_id has 2X \d{3} palindromes and my username is a palindrome ...

      and here is another I put together to show me :

      (line|entry) #, org.string, st. position of pal., length of pal., palindrome found

      perl -nle 'tr/a-z/A-Z/;$l=length();for $m(3..$l){for $c(0..($l-3)){las +t if $m+$c>$l;$f=substr($_,$c,$m);($r=reverse($f))=~s/s+//g;print "$. +\t$_\t$c\t$m\t$f" if ($f eq $r);}}' example | more
      so for your sig : "TTTATCGGTCGTTATATAGATGTTTGCA" I get the following output:
      1 TTTATCGGTCGTTATATAGATGTTTGCA 0 3 TTT 1 TTTATCGGTCGTTATATAGATGTTTGCA 2 3 TAT 1 TTTATCGGTCGTTATATAGATGTTTGCA 12 3 TAT 1 TTTATCGGTCGTTATATAGATGTTTGCA 13 3 ATA 1 TTTATCGGTCGTTATATAGATGTTTGCA 14 3 TAT 1 TTTATCGGTCGTTATATAGATGTTTGCA 15 3 ATA 1 TTTATCGGTCGTTATATAGATGTTTGCA 17 3 AGA 1 TTTATCGGTCGTTATATAGATGTTTGCA 20 3 TGT 1 TTTATCGGTCGTTATATAGATGTTTGCA 22 3 TTT 1 TTTATCGGTCGTTATATAGATGTTTGCA 12 5 TATAT 1 TTTATCGGTCGTTATATAGATGTTTGCA 13 5 ATATA 1 TTTATCGGTCGTTATATAGATGTTTGCA 16 5 TAGAT 1 TTTATCGGTCGTTATATAGATGTTTGCA 21 5 GTTTG
      again works with any list passed as $ARGV[0] or with individual strings ala:
      # echo "TTTATCGGTCGTTATATAGATGTTTGCA" | perl -ne ...
      etc. and it does not match whitespace(s) as part of a reverse complementary match
Re: What one-liners do people actually use?
by parv (Priest) on Dec 08, 2005 at 23:24 UTC
    perl -e 'print length $ARGV[0]' some-string
      That's just the same as
      echo 'some-string' | wc -c
      isn't?
      Perl --((8:>*

        Practically, speaking, yes. Literally, however, no. It's more like:

        length_of() { echo $1 | wc -c } length_of some-string
        The only time this is important is if some-string has spaces, wildcards, etc. You put single quotes around the string, which would prevent any of this from happening, while the OP didn't use quotes, thus shell metacharacters would be expanded.

        That said, I'm betting that the OP wasn't counting on any of this, and that your shell code gets closer to what the OP really wanted anyway.

        Also, the OP's code does work on Windows while yours, well, doesn't, unless you install the Cygwin tools or something. ;-) Not sure if that's important to parv or not.

        Yes, that it is & whole lot shorter, not to mention i need to press Shift only once not 4+ times. It's just that i have not used wc quite enough or fail to remember about its -c option to count the bytes.

Re: What one-liners do people actually use?
by Siddartha (Curate) on Dec 09, 2005 at 00:13 UTC
    I sometimes pipe stuff through the following one liner for utf8 conversion:
    cat my_file | perl -e 'use Encode "from_to"; while (<>) {my $s = $_; f +rom_to($s,"iso-8859-1","utf8"); print $s}'

      Nice++. I was going to ask about something like this on SOPW the other day but was a bit embarrassed b/c I knew there must be a super simple way that I ought to be able to figure out myself.

      You can save yourself a bit of typing there:

      perl -MEncode=from_to -pe"from_to( $_, 'iso-8859-1','utf8' )" infile > + outfile

      Switch the quotes on *nix.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
Re: What one-liners do people actually use?
by Your Mother (Canon) on Dec 09, 2005 at 07:06 UTC

    The sleep is there because I can be a bit fast on the trigger with tools like this and like a second to reconsider the wisdom and the current dir. I have the "-i" aliased into rm, mv, and cp too.

    alias kill-scratch-files "sleep 5; find . -name '*#*' -print | perl -nle unlink;
Re: What one-liners do people actually use?
by strat (Canon) on Dec 09, 2005 at 08:37 UTC

    My most important one liner: if I set up some coffee and continue working, I often forget to fetch it, and it gets cold... so:

    perl -le 'sleep(300); print qq~\a~ x 10'

    reminds me that I must not forget to fetch the fresh coffee; this is the oneliner I use most often

    other cases, for filtering or replacing data in files, e.g. printing objects in LDIF files which have the auxiliary object class dxmADsUser:

    perl -e 'BEGIN { $/="\n\n" }' -ne 'print if /^objectClass: dxmADsUser/si' file1 > file2

    or replacing domain components on-the-fly:

    perl -i.bak -pe 's/dc=domain1,dc=tld$/dc=domain2,dc=tld/' file

    or counting something, e.g. how often each attribute is existing in an ldif file:

    perl -MData::Dumper -ane '$count_of{$F[0]}++' -e 'END { print Dumper(\%count_of) }' file

    Best regards,
    perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"

      Learn the -0 switch. :-)
      perl -00ne'print if /^objectClass: dxmADsUser/i' file1 > file2
Re: What one-liners do people actually use?
by tirwhan (Abbot) on Dec 09, 2005 at 08:52 UTC

    I agree with some of the posts above, any one-liner of even moderate complexity that I use regularly will be turned into a script and/or alias. I'm lazy that way :-). Partly because of that, my most often used perl-related one-liner has to be

    prove -vl t/

    Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan
Re: What one-liners do people actually use?
by contradev (Monk) on Dec 09, 2005 at 09:27 UTC
    perl -le 'print `date`=~/Fri/ ? "woohoo" : "boohoo"'

    :)
      perl -v
      Other than that the only one I use is search and replace.
      perl -pi -e 's/somestring/otherstring/g' *.htm

      cut, grep, xargs and find ususally do instead

      Why not just use localtime?
      perl -le'print localtime =~ /Fri/ ? "w" : "b", "oohoo"'
      Or:
      perl -le'print 5 == (localtime)[6] ? "w" : "b", "oohoo"'
Re: What one-liners do people actually use?
by tphyahoo (Vicar) on Dec 09, 2005 at 12:37 UTC
None, anymore!
by Anonymous Monk on Dec 09, 2005 at 18:24 UTC
    For the most part, I don't use one liners anymore; they typically cost me more time than they save.

    For me, there are usually only two cases:

    1) It's something I've done before -- in which case, it's nice to have it in a script, so that I can just type the name of the script, and worry less about typos or forgetting a step. Plus I can check the perldoc to find out what it does (when I write a script, I have a stronger incentive to write documentation than if I'm pulling a line out a command history).

    2) It's something I've never done before -- in which case, it's nice to put it into a script, so that when I screw up the typing for the 11th time, I don't have to retype the whole thing. Sure, a decent command line can substitute, but it's still nicer to have a copy with the final version of what I've done, just in case I wonder in two weeks "how did I do that?", or worse yet, "What *exactly* did I *do*?".

    That's just my experience; I used to like one liners, but in the longer term, I found the "time savings" by using them to be rather deceptive; I save more time in the long term by writing a clean script in the first place.

    --
    Ytrew

Re: What one-liners do people actually use?
by SamCG (Hermit) on Dec 13, 2005 at 15:01 UTC
    Note: on Windows. . . I don't use dbm's extensively, but it does come up. . .
    perl -e "use SDBM_File;use Fcntl;tie %h, 'SDBM_File', 'H:\dbm\mydbmfil +e', O_RDWR, 0600;for (sort keys %h){print \"$_=$h{$_}\n\";}"
    or
    H:\>perl -e "use SDBM_File;use Fcntl;tie %h, 'SDBM_File', 'H:\dbm\mydb +mfile', O_RDWR, 0600;map {print \"$_=$h{$_}\n\";} sort keys %h;"
    I may eventually put one of these into a script, I guess, but it's kind of short. I use a similar sort of technique for some other small manipulations (such as when I want to change the value of one key or something).

    They're also the first one liners I've really used besides ultra-simple ones like perl -v.
Re: What one-liners do people actually use?
by gordoste (Initiate) on Jan 03, 2006 at 00:39 UTC
    perl -lpe '$_=$!=$_'
    Translates UNIX error codes to English.

    Sufficiently advanced Perl is indistinguishable from garbage.
      Nice. ++

      I use this for a static list:

      perl -le 'print $!+0, "\t", $!++ for 0..127'
      There are some other variations here.

      --
      John.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://515336]
Approved by ww
Front-paged by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2014-08-30 23:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (294 votes), past polls