use locale; use strict; use Getopt::Std; #Analyse des options our ( $opt_c, $opt_t ); $opt_c = 20; getopts( "c:t:" ); my ( $direction, $distance ); if ( defined ( $opt_t ) ){ if ( $opt_t =~ /^(G|D)([1-9])$/i ){ $direction = uc($1); $distance = $2; } else { die "Le critère de tri doit être (G|D)[distance] !\n"; } } #Analyse des arguments if ( $#ARGV != 0 ) { die "Usage : $0 [-c taille_contexte] [-t tri] mot\n"; } my $mot = quotemeta ( $ARGV[0] ); #Recherche et construction des contextes my @contextes; while ( my $ligne = ) { chomp $ligne; $ligne =~ s/\t/ /g; while ( $ligne =~/\b$mot\b/ig ) { my ( $gauche, $droite ); if ( length($`) < $opt_c ) { $gauche = " " x ( $opt_c - length($`) ) . $`; } else { $gauche = substr( $`, length($`) - $opt_c, $opt_c); } $droite = substr ( $', 0, $opt_c ); push ( @contextes, $gauche."\t".$&."\t".$droite ); } } #Tri et affichage if ( defined( $opt_t ) ){ @contextes = sort { critere_tri( $a ) cmp critere_tri( $b ) } @contextes; } print join ( "\n", @contextes ),"\n"; sub critere_tri { my $contexte = shift @_; if ( $contexte =~ /^(.*?) ?\t.*\t ?(.*)$/ ){ my $c_gauche = $1; my $c_droit = $2; my @tab; if ( $direction eq "G" ) { @tab = reverse ( split ( / /, $c_gauche ) ); } else { @tab = split ( / /, $c_droit ); } if ( $#tab >= $distance - 1 ){ return $tab[$distance - 1]; } else { return ""; } } }