Commands tagged perl (190)

  • Lists all the modules that were installed the "proper way". It also uses Perl 5.10(or higher)'s say command for less typing. Show Sample Output


    0
    perl -MExtUtils::Installed -E 'say for ExtUtils::Installed->new()->modules()'
    kimmel · 2011-11-16 17:26:47 4
  • convert a unix timestamp to a human readable format. Show Sample Output


    0
    perl -le 'print scalar gmtime shift' 1234567890
    kimmel · 2011-11-16 18:37:11 5
  • This will create a new file with proper code formatting and all comments removed.


    0
    perl -MO=Deparse filename.pl | perltidy > new.pl
    kimmel · 2011-11-16 18:54:30 4
  • Better awk example, using only mplayer, grep, cut, and awk. Show Sample Output


    0
    mplayer -endpos 0.1 -vo null -ao null -identify *.avi 2>&1 |grep ID_LENGTH |cut -d = -f 2|awk '{SUM += $1} END { printf "%d:%d:%d\n",SUM/3600,SUM%3600/60,SUM%60}'
    Coderjoe · 2011-12-12 15:49:07 3
  • This is especially useful to get crazy stuff like space characters copied to your pasteboard correctly. Source: https://github.com/mathiasbynens/dotfiles/blob/master/.functions Show Sample Output


    0
    perl -e "binmode(STDOUT, ':utf8'); print \"$@\""; echo # newline
    mathias · 2012-01-02 10:34:51 6
  • Here's a perl version that only considers printable characters. Change the regex /[[:print:]]/ to look for different sets of delimiter characters.


    0
    perl -e '$f = join("", <>); for (0..127) {$_ = chr($_); if (/[[:print:]]/) {print if index($f, $_) < 0}} print "\n"'
    putnamhill · 2012-01-05 23:38:06 25
  • Commandline perl filter for, using a production.log from a rails app, display on realtime the count of requests grouped by "seconds to complete" (gross round, but fair enough for an oneliner) :) Show Sample Output


    0
    tail -f production.log | perl -ne 'if (/^Completed.in.(\d+)/){$d = int($1/1000);print "\n";$f{$d}++;for $t (sort(keys(%f))){print $t."s: ".$f{$t}."\n"}}'
    theist · 2012-02-23 14:37:33 3
  • handels @, ?, whitespaces in names. replace "?" and "add" by "!" and "rm" for svn mass remove. ---> I m looking for a nicer way to write it, perhaps with something using " perl -ne '`blahblah` if /\?(.*)/' " Show Sample Output


    0
    svn st | awk ' {if ( $1 == "?" ){print $1="",$0}} ' | sed -e 's/^[ \t]*//' | sed 's/ /\\ /g' | perl -ne '`svn add ${1}@` if /(.*)(@*)(.*)/'
    JulieCaroline · 2012-02-23 18:42:02 3
  • People are *going* to post the wrong ways to do this. It's one of the most common form-validation tasks, and also one of the most commonly messed up. Using a third party tool or library like exim means that you are future-proofing yourself against changes to the email standard, and protecting yourself against the fact that actually checking whether an email address is valid is *not possible*. Still, perhaps your boss is insisting you really do need to check them internally. OK. Read the RFCs. The bet before the @ is specified by RFC2821 and RFC2822. The domain name part is specified by RFC1035, RFC1101, RFC1123 and RFC2181. Generally, when people say "email address", they mean that part of the address that the RFC terms the "addr-spec": the "blah@domain.tld" address, with no display names, comments, quotes, etc. Also "root@localhost" and "root" should be invalid, as should arbitrary addressing schemes specified by a protocol indicator, like "jimbo@myprotocol:foo^bar^baz". So... With the smallest poetic license for readability (allowing underscores in domain names so we can use "\w" instead of "[a-z0-9]"), the RFCs give us: ^(?:"(?:[^"\\]|\\.)+"|[-^!#\$%&'*+\/=?`{|}~.\w]+)@(?=.{3,255}$)(?:[\w][\w-]{0,62}\.){1,128}[\w][\w-]{0,62}$ Not perfect, but the best I can come up with, and most compliant I've found. I'd be interested to see other people's ideas, though. It's still not going to verify you an address fersure, properly, 100% guaranteed legit, though. What else can you do? Well, you could also: * verify that the address is either a correct dotted-decimal IP, or contains letters. * remove reserved domains (.localhost, .example, .test, .invalid), reserved IP ranges, and so forth from the address. * check for banned domains (whitehouse.gov, example.com...) * check for known TLDs including alt tlds. * see if the domain has an MX record set up: if so, connect to that host, else connect to the domain. * see if the given address is accepted by the server as a recipient or sender (this fails for yahoo.*, which blocks after a few attempts, assuming you are a spammer, and for other domains like rediffmail.com, home.com). But these are moving well out of the realm of generic regex checks and into the realm of application-specific stuff that should be done in code instead - especially the latter two. Hopefully, this is all you needed to point out to your boss "hey, email validation this is a dark pit with no bottom, we really just want to do a basic check, then send them an email with a link in it: it's the industry standard solution." Of course, if you want to go nuts, here's an idea that you could do. Wouldn't like to do it myself, though: I'd rather just trust them until their mail bounces too many times. But if you want it, this (untested) code checks to see if the mail domain works. It's based on a script by John Coggeshall and Jesse Houwing that also asked the server if the specific email address existed, but I disliked that idea for several reasons. I suspect: it will get you blocked as a spambot address harvester pretty quick; a lot of servers would lie to you; it would take too much time; this way you can cache domains marked as "OK"; and I suspect it would add little to the reliability test. // Based on work by: John Coggeshall and Jesse Houwing. // http://www.zend.com/zend/spotlight/ev12apr.php mailRegex = '^(?:"(?:[^"\\\\]|\\\\.)+"|[-^!#\$%&\'*+\/=?`{|}~.\w]+)'; mailRegex .= '@(?=.{3,255}$)(?:[\w][\w-]{0,62}\.){1,128}[\w][\w-]{0,62}$'; function ValidateMail($address) {   global $mailRegex; // Yes, globals are evil. Put it inline if you want.   if (!preg_match($mailRegex)) {     return false;   }   list ( $localPart, $Domain ) = split ("@",$Email);   // connect to the first available MX record, or to domain if no MX record.   $ConnectAddress = new Array();   if (getmxrr($Domain, $MXHost)) {     $ConnectAddress = $MXHost;   } else {     $ConnectAddress[0] = $Domain;   }   // check all MX records in case main server is down - may take time!   for ($i=0; $i < count($ConnectAddress); $i++ ) {     $Connect = fsockopen ( $ConnectAddress[$i], 25 );     if ($Connect){       break;     }   }   if ($Connect) {     socket_set_blocking($Connect,0);     // Only works if socket_blocking is off.     if (ereg("^220", $Out = fgets($Connect, 1024))) {       fclose($Connect); // Unneeded, but let's help the gc.       return true;     }     fclose($Connect); // Help the gc.   }   return false; } Show Sample Output


    0
    perl -e "print 'yes' if `exim -bt $s_email_here | grep -c malformed`;"
    DewiMorgan · 2012-02-28 04:42:41 3
  • Reciprocally, we could get the node name from a give Tor IP address => ip2node() { curl -s -d "QueryIP=$1" http://torstatus.blutmagie.de/tor_exit_query.php | grep -oP "Server name:.*'>\K\w+" ; } ip2node 204.8.156.142 BostonUCompSci Show Sample Output


    0
    curl -s -d "CSField=Name" -d "CSInput=BostonUCompSci" http://torstatus.blutmagie.de/index.php | grep -oP "ip=\K(\d+)(\.\d+){3}"
    JisSey · 2012-03-09 16:52:27 3
  • loop through files in .php extension make a .bak of each of them find the base64_decode function and remove it


    0
    for f in `find . -name "*.php"`; do perl -p -i.bak -e 's/<\?php \/\*\*\/ eval\(base64_decode\(\"[^\"]+"\)\);\?>//' $f; done
    lizuka · 2012-03-12 10:44:33 3
  • Add this line to your ~/.gitconfig for a git alias "git brd" (i.e., brd = (br)anch+(d)ate) which sorts branches by date. Allows you to pass in limited "git branch" options such as "-r" (remote) or "-a" (all). (Note: forum added "$" prefix to command; obviously in gitconfig there is no "$" prefix.) Show Sample Output


    0
    brd = "! f() { for k in $(git branch $@ | sed 's/^..//; s/ .*//'); do echo "$(git log -1 --pretty='%Cgreen%ci %Cblue(%cr)%Creset ' $k) $k" ; done | sort -r; }; f"
    michael_n_1138 · 2012-09-28 10:20:51 5
  • Since none of the systems I work on have readlink, this works cross-platform (everywhere has perl, right?). Note: This will resolve links. Show Sample Output


    0
    FULLPATH=$(perl -e "use Cwd 'abs_path';print abs_path('$0');")
    follier · 2013-02-01 20:09:34 5
  • `pwd` returns the current path `grep -o` prints each slash on new line perl generates the paths sequence: './.', './../.', ... `readlink` canonicalizes paths (it makes the things more transparent) `xargs -tn1` applies chmod for each of them. Each command applied is getting printed to STDERR. Show Sample Output


    0
    pwd|grep -o '/'|perl -ne '$x.="./.";print`readlink -f $x`'|xargs -tn1 chmod 755
    luke_skywalker · 2013-03-14 12:03:44 7
  • Found it on: http://stackoverflow.com/questions/318789/whats-the-best-way-to-open-and-read-a-file-in-perl The yet most simple way to read all the contents of a file to a variable. I used it in a perl script to replace $text="`cat /sys/...`", and stipping down 9 secs of runtime due less forks


    0
    $text = do {local(@ARGV, $/) = $file ; <>; }; [or] sub read_file { local(@ARGV, $/) = @_ ; <>; }
    matya · 2013-06-12 11:41:49 18
  • xmas lights for your terminal - switching the $l value to something like 1200 and zooming out on your terminal gives a great view ... Show Sample Output


    0
    perl -le '$l=80;$l2="!" x $l;substr+($l2^=$l2),$l/2,1,"\xFF";{local $_=$l2;y/\0\xFF/ ^/;print;($lf,$rt)=map{substr $l2 x 2,$_%$l,$l;}1,-1;$l2=$lf^$rt;select undef,undef,undef,.1;redo}'
    l3v3l · 2013-06-21 06:00:24 7
  • Lists directory size up to a maximum traversal depth on systems like IBM AIX, where the du command doesn't have Linux's --max-depth option. AIX's du uses -g to display directory size on gigabytes, -m to use megabytes, and -k to use kilobytes. tr### is a Perl function that replaces characters and returns the amount of changed characters, so in this case it will return how many slashes there were in the full path name. Show Sample Output


    0
    du -g | perl -ne 'print if (tr#/#/# == <maximum depth>)'
    RAKK · 2014-02-15 07:33:36 9

  • 0
    for i in $(find . -regex '.*\/C.*\.cpp'); do svn mv `perl -e 'my $s=$ARGV[0]; $s=~m/(.*\/)C(.*)/; print "$s $1$2"' "$i"`; done
    nbolton · 2014-02-27 17:28:29 6

  • 0
    perldoc -m Some::Module
    fibo · 2014-03-03 16:00:03 6
  • Note that in the command N is, for instance, 37. Show Sample Output


    0
    perl -nle 'print length,"\t",$_ if length > 37' < /path/to/input/file
    fibo · 2014-03-20 09:44:41 6
  • If you have a logfile where some lines start with timestamps like "2014-05-01 12:34:56,123" but other lines are missing the timestamp (like stack traces or object dumps), then use this script to copy the most recent timestamp to any lines that are missing it. This is useful for merging log files, since you can then safely sort by timestamp to merge the files. Show Sample Output


    0
    perl -ne 'if (/^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{3} )/ ) { $t=$1; print $_ } else { print $t . $_ }'
    unixmonkey76059 · 2014-05-21 08:54:27 9
  • Given a bunch of files with "wrong" date naming, it renames them in a "good" format. Show Sample Output


    0
    rename 's/result_([0-9]+)_([0-9]+)_([0-9]+)\.json\.txt/sprintf("%d%02d%02d.txt",$3,$2,$1)/ge' result_*.txt
    sucotronic · 2014-06-13 07:34:32 6

  • 0
    perl -M URI::Escape -lne 'print uri_unescape($_)'
    johnlane · 2014-07-01 10:45:33 7
  • Usage: command | hl 'regex'


    0
    hl() { while read -r; do printf '%s\n' "$(perl -p -e 's/('"$1"')/\a\e[7m$1\e[0m/g' <<< "$REPLY")"; done; }
    nyuszika7h · 2014-08-05 22:29:08 8

  • 0
    perl -MPOSIX -le 'print strftime "%F", localtime 1234567890'
    lysergication · 2014-09-09 21:15:58 8
  • ‹ First  < 4 5 6 7 8 > 

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

Which processes are listening on a specific port (e.g. port 80)
swap out "80" for your port of interest. Can use port number or named ports e.g. "http"

Rename files in batch

Download an Entire website with wget

Set laptop display brightness
Run as root. Path may vary depending on laptop model and video card (this was tested on an Acer laptop with ATI HD3200 video). $ cat /proc/acpi/video/VGA/LCD/brightness to discover the possible values for your display.

Get AWS temporary credentials ready to export based on a MFA virtual appliance
You might want to secure your AWS operations requiring to use a MFA token. But then to use API or tools, you need to pass credentials generated with a MFA token. This commands asks you for the MFA code and retrieves these credentials using AWS Cli. To print the exports, you can use: `awk '{ print "export AWS_ACCESS_KEY_ID=\"" $1 "\"\n" "export AWS_SECRET_ACCESS_KEY=\"" $2 "\"\n" "export AWS_SESSION_TOKEN=\"" $3 "\"" }'` You must adapt the command line to include: * $MFA_IDis ARN of the virtual MFA or serial number of the physical one * TTL for the credentials

a short counter
Maybe you know shorter ?

Log colorizer for OSX (ccze alternative)
Download colorizer by @raszi @ http://github.com/raszi/colorize

add all files not under version control to repository
This should handle whitespaces well and will not get confused if your filenames have "?" in them

Sort netflow packet capture
Sort netflow packet capture by unique connections excluding source port.

back ssh from firewalled hosts
host B (you) redirects a modem port (62220) to his local ssh. host A is a remote machine (the ones that issues the ssh cmd). once connected port 5497 is in listening mode on host B. host B just do a ssh 127.0.0.1 -p 5497 -l user and reaches the remote host'ssh. This can be used also for vnc and so on.


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: