truc PERL pour shell

Mise à jour: 30 septembre 2006
Version: 1.0
Author: Jean-Louis Bicquelet-Salaün
Location: http://jlbicquelet.free.fr
Copyright: (c) 2006 Jean-Louis BICQUELET-SALAÜN

PERL tips for shell

I often fill functions missed me in shell. Here are some PERL commands you can call from shell scripts, to make your shell life more easy.

  1. Here is a way to extract data from files The following command extracts pod documentation from a file.

    perl -ne 'print if /^=pod/ .. /^=cut/' test.pl
    You can remplace if by unless to exclude lines beetween 2 delimitors. To suppres lignes from 1à to 12, type:

    perl -ne 'print unless 10 .. 12'
  2. PERL can converts Unix timestamp Unix (time in seconde since 1970) into a comprehensible date for human being.

    a=`perl -e "use Time::Local;print timelocal(0,10,9,13,8,2006);"`
    order: seconde minute hour day month -1 (attention!) year in the other sens:

    perl -e "use Time::Local;print scalar localtime($a);"
  3. to extract the first column of an array :

    perl -lane 'print $F[0] ' e.csv
    Attention, the first column start atindice 0, seconde one is 1 and so on...
  4. for a 6 sided dice:

    perl -e "print int(rand(6))+1" perl -e "print int(rand(101))"
  5. PERL can remplace a value in a file.

    perl -p -i.bak -e 's/toto/tata/g' test.txt
    The option -i.bak will create a test.txt.bak file.

    You can also modify several files of a blow by using the jokers (* txt for example). You can use expression such as for example (\btoto\b) to scan for the word louse alone. The option (-i) can be also used with the orders of extraction. To keep the first line of a file:

    perl -i.bak -ne -i 'print if 1 .. 1'