Commands using perl (369)

  • changes THIS to THAT in all files matching fileglob* without using secondary files


    2
    perl -pi -e 's/THIS/THAT/g' fileglob*
    elofland · 2009-02-05 19:19:52 60

  • 2
    perl -pe 's/.+;//' ~/.zsh_history | sort | uniq -c | sort -r|head -10
    aoiaoi · 2009-02-06 15:24:32 168

  • 2
    perl -pe 's/\d+/++$n/e' file.txt
    mikeda · 2009-02-17 14:51:31 6
  • Insert a comma where necessary when counting large numbers. I needed to separate huge amounts of packets and after 12+ hours of looking in a terminal, I wanted it in readable form. Show Sample Output


    2
    perl -pe '$_=reverse;s/\d{3}(?=\d)(?!.*?\.)/$&,/g;$_=reverse'
    sil · 2009-02-18 16:34:18 15
  • Finds the string in every file in an entire directory and all its subdirectories and replaces it with a new string. Especially useful when changing a machine's IP address or hostname - run it on /etc.


    2
    perl -pi -e's/<what to find>/<what to replace it with>/g' `grep -Rl <what to find> /<dir>/*`
    adampbell · 2009-02-26 19:14:39 8

  • 2
    find $HOME -type f -print | perl -wnlaF'/' -e 'BEGIN{ print "#EXTM3U"; } /.+\.wmv$|.+\.mpg$|.+\.vob$/i and print "#EXTINF:$F[-1]\nfile://$&";' > movies.m3u
    ishiduca · 2009-02-28 12:17:41 4

  • 2
    perl -ne 'while (/([0-9]+\.){3}[0-9]+/g) {print "$&\n"};' file.txt
    P17 · 2009-04-01 13:49:46 6
  • There was another line that was dependent on having un-named screen sessions. This just wouldn't do. This one works no matter what the name is. A possible improvement would be removing the perl dependence, but that doesn't effect me.


    2
    for i in `screen -ls | perl -ne'if(/^\s+\d+\.([^\s]+)/){print $1, " "}'`; do gnome-terminal -e "screen -x $i"; done
    hank · 2009-04-25 22:39:24 7
  • Replace 'this' with 'that'


    2
    perl -p -i -e 's/this/that/g' filename
    rader5 · 2009-08-09 20:53:50 8
  • When you have one of those (log)files that only has epoch for time (since no one will ever look at them as a date) this is a way to get the human readable date/time and do further inspection. Mostly perl-fu :-/


    2
    perl -F' ' -MDate::Format -pale 'substr($_, index($_, $F[1]), length($F[1]), time2str("%C", $F[1]))' file.log
    coffeeaddict_nl · 2009-08-13 13:57:33 4

  • 2
    perl -e 'print scalar(gmtime(1234567890)), "\n"'
    andrew112358 · 2009-08-25 15:00:52 4

  • 2
    wget 'link of a Picasa WebAlbum' -O - |perl -e'while(<>){while(s/"media":{"content":\[{"url":"(.+?\.JPG)//){print "$1\n"}}' |wget -w1 -i -
    aciancone · 2009-09-27 14:36:27 7
  • Once I wrote a command line calculator program in C, then I found this... and added to it a bit. For ease of use I normally use this in a tiny Perl program (which I call pc for 'Perl Calculator') #!/usr/bin/perl -w die "Usage: $0 MATHS\n" unless(@ARGV);for(@ARGV){s/x/*/g;s/v/sqrt /g;s/\^/**/g}; print eval(join('',@ARGV)),$/; It handles square roots, power, modulus: pc 1+2 (1 plus 2) 3 pc 3x4 (3 times 4) 12 pc 5^6 (5 to the power of 6) 15625 pc v 49 ( square root of 49 ) 7 pc 12/3 (12 divided by 3) 4 pc 19%4 (19 modulus 4) 3 (you can string maths together too) pc 10 x 10 x 10 1000 pc 10 + 10 + 10 / 2 25 pc 7 x v49 49 Show Sample Output


    2
    perl -e 'for(@ARGV){s/x/*/g;s/v/sqrt /g;s/\^/**/g};print eval(join("",@ARGV)),$/;'
    JohnGH · 2009-12-21 21:03:27 9
  • This is a big time saver for me. I often grep source code and need to edit the findings. A single highlight of the mouse and middle mouse click (in gnome terminal) and I'm editing the exact line I just found. The color highlighting helps interpret the data.


    2
    mgc() { grep --exclude=cscope* --color=always -rni $1 . |perl -pi -e 's/:/ +/' |perl -pi -e 's/^(.+)$/vi $1/g' |perl -pi -e 's/:/ /'; }
    stinkerweed999 · 2010-01-26 17:00:01 4
  • Like command #4845, prints score, number of entries, and average score.


    2
    username=bartonski;curl -s http://www.commandlinefu.com/commands/by/$username/json|perl -e 'BEGIN{$s=0;$n=0};END{print "Score: $s\nEntries: $n\nMean: ";printf "%3.2f\n",$s/$n}' -0173 -nae 'foreach $f (@F){if($f =~ /"votes":"(-*\d+)"/){$s += $1; $n++;}}'
    bartonski · 2010-02-16 01:03:29 4
  • Requires Net::Twitter. Just replace the double quoted strings with the appropriate info.


    2
    perl -MNet::Twitter -e '$nt = Net::Twitter->new(traits => [qw/API::REST/], username => "YOUR USERNAME", password => "YOUR PASSWORD"); $ud = $nt->update("YOUR TWEET");'
    dbbolton · 2010-06-16 19:46:05 4
  • **NOTE** Tekhne's alternative is much more succinct and its output conforms to the files actual contents rather than with white space removed My command on the other hand uses bash process substitution (and "Minimal" Perl), instead of files, to first remove leading and trailing white space from lines, before diff'ing the streams. Very useful when differences in indentation, such as in programming source code files, may be irrelevant Show Sample Output


    2
    diff <(perl -wpl -e '$_ =~ s/^\s+|\s+$//g ;' file1) <(perl -wpl -e '$_ =~ s/^\s+|\s+$//g ;' file2)
    jemptymethod · 2010-10-06 19:14:42 5
  • If you are in an environment where you don't have the base64 executable or MIME tools available, this can be very handy for salvaging email attachments when the headers are mangled but the encoded document itself is intact.


    2
    perl -MMIME::Base64 -ne 'print decode_base64($_)' < file.txt > out
    dmmst19 · 2010-12-13 23:35:20 3
  • This is a naive way of finding source code comments in source code files that use C-like comments: // and /*...*/


    2
    perl -e 'my $in_comment = 0; while (<>) { $in_comment = 1 if m{\Q/*\E}; print if $in_comment; $in_comment = 0 if m{\Q*/\E}; }' *.cpp
    doherty · 2011-07-08 00:17:27 4

  • 2
    route -n | perl -ne '$ANY="0.0.0.0"; /^$ANY/ and split /\s+/ and print "Gateway to the World: ",($_[1]!=$ANY)?$_[1]:(`ip address show $_[$#_]`=~/peer ([0-9\.]+)/ and $1),", via $_[$#_].\n"'
    bandie91 · 2011-09-13 08:05:58 5

  • 2
    perl -le 'print$_%3?$_%5?$_:"Buzz":$_%5?"Fizz":"FizzBuzz"for 1..100'
    depesz · 2012-01-10 13:10:30 5

  • 2
    perl -i -ne 'print if $. == 3..5' <filename>
    bashrc · 2012-01-27 23:15:10 4
  • - excel date compatible with a separate hour field - added a fixed 1 for easier request counter aggregation - split URL in directory, filename, fileext, query - used with tomcat valve with response bytes replaced by elapsed time Show Sample Output


    2
    #(see sample) $ cat x | perl -pe 'BEGIN{ print "TIME;...\n"; } s!(\S+) - (\S+) - \[(\d\d)/(\S\S\S)/(\S+):(\d\d):(\d\d:\d\d) \S+\] "(\S+) (.*/)(\S+)(?:\.([^?]*)(\?\S*)?) HTTP/\S+" (\d+) (\S+)!$3-$4-$5 $6:$7;$6;$2;$1;$8;$13;1;$14;$11;$10;$9;$12;!' > x.csv
    hute37 · 2012-02-10 16:58:50 3
  • Nasty perl one-liner that provides a sparkline of ping times. If you want a different history than the last 30, just put that value in. It (ab)uses unicode to draw the bars, inspired by https://github.com/joemiller/spark-ping . It's not the most bug-free piece of code, but what it lacks in robustness it makes up for in capability. :) If anyone has any ideas on how to make it more compact or better, I'd love to hear them. I included a ping to google in the command just as an example (and burned up 10 chars doing it!). You should use it with: $ ping example.com | $SPARKLINE_PING_COMMAND Show Sample Output


    2
    ping g.co|perl -ne'$|=/e=(\S+)/||next;(push@_,$1)>30&&shift@_;print"\r",(map{"\xe2\x96".chr(128+7*$_/(sort{$b<=>$a}@_)[0])." "}@_),"$1ms"'
    bartgrantham · 2012-07-06 22:42:06 3
  • 1.- Enter into the playlist path. 2.- Run the command. 3.- Playlists created!


    2
    wget -q -O - http://listen.di.fm/public2 | sed 's/},{/\n/g' | perl -n -e '/"key":"([^"]*)".*"playlist":"([^"]*)"/; print "$1\n"; system("wget -q -O - $2 | grep -E '^File' | cut -d= -f2 > di_$1.m3u")'
    Zort · 2013-02-20 03:37:41 9
  • ‹ First  < 2 3 4 5 6 >  Last ›

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands


Check These Out

Directory Tree
tree has lots of parms - man is your friend

Get the Volume labels all bitlocker volumes had before being encrypted
Get information of volume labels of bitlocker volumes, even if they are encrypted and locked (no access to filesystem, no password provided). Note that the volume labels can have spaces, but only if you name then before encryption. Renaming a bitlocker partition after being encrypted does not have the same effect as doing it before.

Make info pages much less painful
The pinfo package makes info pages much more bearable. It is a ncurses-based POSIX utility for viewing info and man pages using lynx style keyboard shortcuts and rendering. Links are highlighted blue, the current location of your cursor is red. Navigating and searching are easy. Worth the install.

Copy data using gtar
It copies the entire current working directory to the destination directory with compression enabled.

Measure, explain and minimize a computer's electrical power consumption
Run this command as root to get enough stats. It works on AMD and Intel machines, including desktops. If ran on a laptop it'll give you suggestions on extending your battery life. You'll need to install PowerTOP if you don't have, via 'apt-get install powertop', etc. To grep the output use: sudo powertop -d | grep ... The many command suggestions PowerTOP gives you alone will increase your command-line fu!

Find the package that installed a command

Create a new file

Detect encoding of a text file
This command gives you the charset of a text file, which would be handy if you have no idea of the encoding.

Extract the emoticons regex from a running skype process
Skype has an internal regex which depicts the emoticons it supports. However you cannot simply search the binary file for it. This small 181 character line will do just that, provided skype is running. And of course, only works in linux.

Create a backdoor on a machine to allow remote connection to bash
My netcat (nc-1.84-10.fc6) doesn't have the -e option, so I have to do it like this. Of course, instead of bash, you can use any executable, including scripts.


Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: