Re: What one-liners do people actually use?
by rnahi (Curate) on Dec 08, 2005 at 19:18 UTC
|
| [reply] |
|
| [reply] |
Re: What one-liners do people actually use?
by jeffa (Bishop) on Dec 08, 2005 at 19:21 UTC
|
perl -le'for (<*.mas>) {$o=$_;s/mas$/html/; rename $o,$_ }'
I needed to change some .mas extensions to .html extensions.
| [reply] [d/l] |
|
| [reply] [d/l] |
|
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.
| [reply] [d/l] |
|
|
Or rename mas html *.mas...
| [reply] [d/l] |
|
|
You didn't need Perl for that:
for %q in (*.mas) do ren %q %~nq.html
| [reply] [d/l] |
|
| [reply] [d/l] |
|
Re: What one-liners do people actually use?
by exussum0 (Vicar) on Dec 08, 2005 at 19:03 UTC
|
perl -s -p -i -e '~s/\r//g' file.txt
| [reply] [d/l] |
|
unix2dos (when run from "dos")
perl -i.bak -ple1 file.txt
| [reply] [d/l] |
|
The s flag (process switches) is useless here, isn't it?
| [reply] |
|
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. ^^
| [reply] |
Re: What one-liners do people actually use?
by Tanktalus (Canon) on Dec 08, 2005 at 19:21 UTC
|
- Anything with the -i flag - that's how I started using perl (when sed/awk wasn't enough).
- 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.
| [reply] [d/l] |
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 :-)
| [reply] [d/l] |
|
| [reply] |
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}'
| [reply] [d/l] |
|
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.
| [reply] [d/l] |
|
| [reply] |
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"
| [reply] [d/l] [select] |
|
perl -00ne'print if /^objectClass: dxmADsUser/i' file1 > file2
| [reply] [d/l] |
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);'
| [reply] [d/l] [select] |
|
Another way to write that:
perl -e'printf "%3\$02d:%2\$02d:%1\$02d\n", gmtime 974'
:-)
| [reply] [d/l] |
Re: What one-liners do people actually use?
by creamygoodness (Curate) on Dec 08, 2005 at 19:11 UTC
|
| [reply] [d/l] |
|
valgrind perl -e1
| [reply] [d/l] |
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!!!
| [reply] |
Re: What one-liners do people actually use?
by tphyahoo (Vicar) on Dec 09, 2005 at 12:37 UTC
|
| [reply] |
Re: What one-liners do people actually use?
by SolidState (Scribe) on Dec 08, 2005 at 19:47 UTC
|
perl -ne'printf "%012b",hex $_' input.hex > output.bin
| [reply] [d/l] |
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.
| [reply] [d/l] |
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.
| [reply] [d/l] [select] |
|
I would have written that as:
perl -F, -alne '$, = ","; @F == 33 and print @F[0,8,9]' input.txt
| [reply] [d/l] |
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 ... | [reply] [d/l] |
|
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 | [reply] [d/l] [select] |
Re: What one-liners do people actually use?
by Your Mother (Archbishop) 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;
| [reply] [d/l] |
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. | [reply] |
Re: What one-liners do people actually use?
by parv (Parson) on Dec 08, 2005 at 23:24 UTC
|
perl -e 'print length $ARGV[0]' some-string
| [reply] [d/l] |
|
echo 'some-string' | wc -c
isn't?
| [reply] [d/l] |
|
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. | [reply] [d/l] |
|
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.
| [reply] [d/l] [select] |
Re: What one-liners do people actually use?
by ikegami (Patriarch) on Dec 08, 2005 at 20:34 UTC
|
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.
| [reply] [d/l] [select] |
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"'
:)
| [reply] [d/l] |
|
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
| [reply] [d/l] [select] |
|
Why not just use localtime?
perl -le'print localtime =~ /Fri/ ? "w" : "b", "oohoo"'
Or:
perl -le'print 5 == (localtime)[6] ? "w" : "b", "oohoo"'
| [reply] [d/l] [select] |
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
| [reply] [d/l] |
Re: What one-liners do people actually use?
by Perl Mouse (Chaplain) on Dec 08, 2005 at 21:21 UTC
|
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.
| [reply] [d/l] |
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.
| [reply] |
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 | [reply] |
Re: What one-liners do people actually use?
by gordoste (Initiate) on Jan 03, 2006 at 00:39 UTC
|
| [reply] [d/l] |
|
perl -le 'print $!+0, "\t", $!++ for 0..127'
There are some other variations here.
--
John.
| [reply] [d/l] |
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. | [reply] [d/l] [select] |