Commands tagged perl (180)

  • this command example converts to 25 fps subtitles that were originally created for 24 fps movie

    cat subtitles.txt | perl -pe 's/} /}/g; s/{(\d+)}/=1=/; $f1=(24/25*$1); s/{(\d+)}/=2=/; $f2=(24/25*$1); $f1=~s/\..*//; $f2=~s/\..*//; s/=1=/{$f1}/; s/=2=/{$f2}/; ' > subtitles_newfps.txt
    ciekawy · 2011-07-30 15:34:43 0
  • The output of lsof is piped to txt2html which converts it to html. # Perl module HTML::TextToHTML needed Show Sample Output

    lsof -nPi | txt2html > ~/lsof.html
    zlemini · 2011-07-28 14:01:21 4
  • If your contact information was entered when your user account was created (it gets added to /etc/passwd) then this gets that info and creates a QR code for you automatically

    getent passwd $(whoami) | echo "$(perl -ne '/^([^:]+):[^:]+:[^:]+:[^:]+:([^ ]+) ?([^,]+)?,([^,]*),([^,]*),([^:,]*),?([^:,]*)/ and printf "MECARD:N:$3,$2;ADR:$5;TEL:$4;TEL:$6;EMAIL:$1@"')$HOSTNAME;;" | qrencode -o myqr.png
    Strawp · 2011-07-25 12:59:57 0
  • Randomizes a file. The opposite of sort is sort -R!

    sort -R
    RyanM · 2011-07-15 15:35:27 0
  • The same with colors

    tail -f /var/log/squid/access.loc | ccze -CA
    longdrink · 2011-07-15 14:58:53 2

  • 1
    perl -mText::Highlight -E 'say Text::Highlight->new(ansi => 1)->highlight(Perl => do { local (@ARGV,$/) = shift; <> }) ' path/to/
    dbr · 2011-07-12 04:55:12 1
  • more idiomatic version of the same, using the flip-flop-operator; also printing lines with '//'-style comments

    perl -ne 'print if m{\Q/*\E}x .. m{\Q*/\E}x or m{\/\/}x' *.c
    dbr · 2011-07-12 04:41:49 0
  • This is a naive way of finding source code comments in source code files that use C-like comments: // and /*...*/

    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 1

  • 3
    tail -f /var/log/squid/access.log | perl -p -e 's/^([0-9]*)/"[".localtime($1)."]"/e'
    godzillante · 2011-07-06 08:55:27 3
  • Using tail to follow and standard perl to count and print the lps when lines are written to the logfile.

    tail -f /var/log/logfile|perl -e 'while (<>) {$l++;if (time > $e) {$e=time;print "$l\n";$l=0}}'
    madsen · 2011-06-21 10:28:26 0
  • Recursively delete empty directories. Use with care.

    perl -MFile::Find -e"finddepth(sub{rmdir},'.')"
    igorfu · 2011-05-23 08:45:34 0
  • This uses Perl's rename utility (you may have to call it as prename on your box) and won't choke on spaces or other characters in filenames. It will also zero pad a number even in filenames like "vacation-4.jpg".

    rename 's/\d+/sprintf("%04d",$&)/e' *.jpg
    eightmillion · 2011-05-01 20:50:36 2
  • This is from perldoc -q random.*line, which says: This has a significant advantage in space over reading the whole file in. You can find a proof of this method in The Art of Computer Programming, Volume 2, Section 3.4.2, by Donald E. Knuth. Who am I to argue with Don Knuth?

    perl -e 'rand($.) < 1 && ($line = $_) while <>;'
    unixmonkey20842 · 2011-04-25 21:28:26 0
  • Let -p do it's voodoo and do absolutely nothing but set the output record separator :-)

    perl -ple 'BEGIN { $\ = "\r\n" }'
    coffeeaddict_nl · 2011-03-01 09:45:37 0
  • Reads stdin, and outputs each line only once - without sorting ahead of time. This does use more memory than your system's sort utility.

    perl -ne 'print if !$a{$_}++'
    doherty · 2011-02-17 02:18:44 2
  • This works by reading in two lines of input, turning each into a list of one-character matches that are sorted and compared.

    (echo foobar; echo farboo) | perl -E 'say[sort<>=~/./g]~~[sort<>=~/./g]?"anagram":"not anagram"'
    doherty · 2011-02-17 02:15:46 0
  • This uses Text::Highlight to output the specified Perl file with syntax highlighting. A better alternative is my App::perlhl - find it on the CPAN:

    perl -MText::Highlight -E '$h=Text::Highlight->new(ansi=>1); my $text=do{local $/; open my $fh, "<", $ARGV[0]; <$fh>}; say $h->highlight("Perl", $text);' path/to/
    doherty · 2011-01-31 05:52:43 1
  • No need for further filedes or substitution for splitting. Simply use read a b

    grep -i "$*" /usr/lib/perl5/Unicode/ | while read a b; do /usr/bin/printf "\u$a\tU+%s\t%s\n" "$b"; done
    ioggstream · 2011-01-04 11:30:16 2
  • [Update! Thanks to a tip from ioggstream, I've fixed both of the bugs mentioned below.] You, yes, 𝙔𝙊𝙐, can be the terror of the Internet! Why use normal, boring bullet points in your text, when you could use a ROTATED HEAVY BLACK HEART BULLET (❥)!? (Which would also be an awesome band name, by the way).  This script makes it easy to find unusual characters from the command line. You can then cut and paste them or, if you're using a GTK application, type Control+Shift+U followed by the code point number (e.g., 2765) and then a SPACE.  USAGE: Put this script in a file (I called mine "ugrep") and make it executable. Run it from the command line like so,  ugrep heart  The output will look like this,  ☙ U+2619 REVERSED ROTATED FLORAL HEART BULLET ♡ U+2661 WHITE HEART SUIT ♥ U+2665 BLACK HEART SUIT ❣ U+2763 HEAVY HEART EXCLAMATION MARK ORNAMENT ❤ U+2764 HEAVY BLACK HEART ❥ U+2765 ROTATED HEAVY BLACK HEART BULLET ❦ U+2766 FLORAL HEART ❧ U+2767 ROTATED FLORAL HEART BULLET ⺖ U+2E96 CJK RADICAL HEART ONE ⺗ U+2E97 CJK RADICAL HEART TWO ⼼ U+2F3C KANGXI RADICAL HEART  You can, of course, use regular expressions. For example, if you are looking for the "pi" symbol, you could do this:  ugrep '\bpi\b'  REQUIREMENTS: Although this is written in Bash, it assumes you have Perl installed because it greps through the Perl Unicode character name module (/usr/lib/perl5/Unicode/ Note that it would not have made more sense to write this in Perl, since the module doesn't actually include a subroutine for looking up a character based on the description. (Weird.)  BUGS: In order to fit this script in the commandlinefu limits, a couple bugs were added. ① Astral characters beyond the BMP (basic multilingual plane) are not displayed correctly, but see below. ② Perl code from the perl module being grepped is sometimes extraneously matched.  MISFEATURES: Bash's printf cannot, given a Unicode codepoint, print the resulting character to the terminal. GNU's coreutils printf (usually "/usr/bin/printf") can do so, but it is brokenly pedantic about how many hexadecimal digits follow the escape sequence and will actually die with an error if you give the wrong number. This is especially annoying since Unicode code points are usually variable length with implied leading zeros. The file represents BMP characters as 4 hexits, but astral characters as 5. In the actual version of this script that I use, I've kludged around this misfeature by zero-padding to 8 hexits like so,  /usr/bin/printf "\U$(printf "%08x" 0x$hex)"  TIP 1: The author recommends "xsel" for command line cut-and-paste. For example,  ugrep biohazard | xsel  TIP 2: In Emacs, instead of running this command in a subshell, you can type Unicode code points directly by pressing Control-Q first, but you'll likely want to change the default input from octal to hexadecimal. (setq read-quoted-char-radix 16).  TIP 3: Of course, if you're using X, and you want to type one of the more common unusual characters, it's easiest of all to do it with your Compose (aka Multi) key. For example, hitting [Compose] <3 types ♥. Show Sample Output

    egrep -i "^[0-9a-f]{4,} .*$*" $(locate | while read h d; do /usr/bin/printf "\U$(printf "%08x" 0x$h)\tU+%s\t%s\n" $h "$d"; done
    hackerb9 · 2010-12-31 16:47:59 5
  • Nobody wants the boss to notice when you're slacking off. This will fill your shell with random data, parts of it highlighted. Note that 'highlight' is the Perl module App::highlight, not "a universal sourcecode to formatted text converter." You'll also need Term::ANSIColor. Show Sample Output

    cat /dev/urandom | hexdump -C | highlight ca fe 3d 42 e1 b3 ae f8 | perl -MTime::HiRes -pne "Time::HiRes::usleep(rand()*1000000)"
    doherty · 2010-12-29 21:26:18 3
  • range context (-A -B) search, with exclusion of vcs directories Show Sample Output

    pcregrep -r --exclude_dir='.svn' --include='.*jsp$' -A 2 -B 2 --color "pHtmlHome" .
    hute37 · 2010-12-09 15:26:07 0
  • Fun idea! This one adds seconds and keeps running on the same line. Perl's probably cheating though. :)

    perl -e 'for(;;){@d=split("",`date +%H%M%S`);print"\r";for(0..5){printf"%.4b ",$d[$_]}sleep 1}'
    putnamhill · 2010-12-04 00:05:54 0
  • This command turns a multi-line file into a single line joined with <SOMETEXT>. To skip blank lines, use: perl -pe '(eof()||s/^\s*$//)||s/\n/<SOMETEXT>/g' file.txt Show Sample Output

    perl -pe 'eof()||s/\n/<SOMETEXT>/g' file.txt
    eightmillion · 2010-12-02 01:19:27 2
  • In this way it doesn't have problems with filenames with spaces.

    find . -type f -print0 | xargs -0 perl -e 'tr/A-Z/a-z/'
    depesz · 2010-11-25 13:55:34 0

  • 0
    perl -e "tr/[A-Z]/[a-z]/;" $(find . -type f)
    miccaman · 2010-11-25 12:48:39 0
  • ‹ First  < 2 3 4 5 6 >  Last ›

What's this? 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

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.


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: