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 58

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

  • 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

Alias TAIL for automatic smart output
Run the alias command, then issue $ps aux | tail and resize your terminal window (putty/console/hyperterm/xterm/etc) then issue the same command and you'll understand. $ ${LINES:-`tput lines 2>/dev/null||echo -n 12`} Insructs the shell that if LINES is not set or null to use the output from `tput lines` ( ncurses based terminal access ) to get the number of lines in your terminal. But furthermore, in case that doesn't work either, it will default to using the default of 80. The default for TAIL is to output the last 10 lines, this alias changes the default to output the last x lines instead, where x is the number of lines currently displayed on your terminal - 7. The -7 is there so that the top line displayed is the command you ran that used TAIL, ie the prompt. Depending on whether your PS1 and/or PROMPT_COMMAND output more than 1 line (mine is 3) you will want to increase from -2. So with my prompt being the following, I need -7, or - 5 if I only want to display the commandline at the top. ( http://www.askapache.com/linux/bash-power-prompt.html ) 275MB/748MB [7995:7993 - 0:186] 06:26:49 Thu Apr 08 [askapache@n1-backbone5:/dev/pts/0 +1] ~ $ In most shells the LINES variable is created automatically at login and updated when the terminal is resized (28 linux, 23/20 others for SIGWINCH) to contain the number of vertical lines that can fit in your terminal window. Because the alias doesn't hard-code the current LINES but relys on the $LINES variable, this is a dynamic alias that will always work on a tty device.

generate a unique and secure password for every website that you login to
usage: sitepass MaStErPaSsWoRd example.com description: An admittedly excessive amount of hashing, but this will give you a pretty secure password, It also eliminates repeated characters and deletes itself from your command history. tr '!-~' 'P-~!-O' # this bit is rot47, kinda like rot13 but more nerdy rev # this avoids the first few bytes of gzip payload, and the magic bytes.

Route outbound SMTP connections through a addtional IP address rather than your primary

View all images
So you are in directory with loads of pictures laying around and you need to quickly scan through them all

Renaming a file without overwiting an existing file name
Sometimes in a hurry you may move or copy a file using an already existent file name. If you aliased the cp and mv command with the -i option you are prompted for a confirmation before overwriting but if your aliases aren't there you will loose the target file! The -b option will force the mv command to check if the destination file already exists and if it is already there a backup copy with an ending ~ is created.

Extract the MBR ID of a device
Useful when you want to know the mbrid of a device - for the purpose of making it bootable. Certain hybridiso distros, for eg the OpenSUSE live ISO uses the mbrid to find the live media. Use this command to find out the mbrid of your USB drive and then edit the /grub/mbrid file to match it.

Convert CSV to JSON
Replace 'csv_file.csv' with your filename.

Find files that were modified by a given command
Traces the system calls of a program. See http://linuxhelp.blogspot.com/2006/05/strace-very-powerful-troubleshooting.html for more information.

Check wireless link quality with dialog box
The variable WIRELESSINTERFACE indicates your wireless interface

Quick screenshot
Requires ImageMagick. Takes a screenshot 5 seconds after it's run and saves it as desktop_screenshot.jpg Particularly handy when made into a menu option or button.


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: