Manipulação de arquivos

Publicado por Enzo de Brito Ferber (última atualização em 24/09/2010)

[ Hits: 7.877 ]

Homepage: http://www.maximasonorizacao.com.br

Download file.tar.gz




Bom, resolvi aprender Perl e comecei a brincar, e achei um tutorial que tinha o seguinte exercício.

Faça um programa que leia um arquivo de texto e imprima as linhas (com sua numeração no arquivo) que contenham:

- a letra x
- a palavra 'the' (case-sensitive)
- a palavra 'the' (no-case-sensitive) - ou seja, desconsiderar se as letras são maiúsculas ou minúsculas.

  



Esconder código-fonte

#!/usr/bin/perl
#
# Regular Expressions
#
# Program that counts only the line with:
#  - the letter x
#  - the string the (case-sensitive)
#  - the string the (non-case-sensitive)
#
# Exercice for Perl tutorial found at:
# http://www.comp.leeds.ac.uk/Perl/matching.html
#
# Enzo Ferber : <enzo@veloxmail.com.br>
# sep 2010

$file = "electricity.txt";
open ( FILE, $file );

@lines = <FILE>;
$i = 1;

foreach $_ ( @lines )
{
   if ( /x/ )
   {
      push ( @x, $i );
   }
   elsif ( /the/ )
   {
      push ( @thecs, $i );
   }
   
   # can't be elsif because won't include the(case sensitive)
   if ( /the/i )
   {
      push ( @thencs, $i );
   }

   # line numbers counter
   ++$i;
}

# Printing
print "Letter x\n\n";
foreach $xx ( @x )
{
   print "$xx $lines[$xx - 1]";
}

print "\nthe case-sensitive\n\n";
foreach $tcs ( @thecs )
{
   print "$tcs $lines[$tcs - 1]";
}

print "\nthe non-case-sensitive\n\n";
foreach $tncs ( @thencs )
{
   print "$tncs $lines[$tncs - 1]";
}


Scripts recomendados

Conversão de textos UTF-8 para ISO8859-1

getter

Rsync e Perl: Sincronização de arquivos com gestão de backup incremental integrada

Alterar senha no shadow

Compilar e mover


  

Comentários
[1] Comentário enviado por SamL em 25/09/2010 - 00:23h

Então esse é o script, não tá tão ruim assim.

[2] Comentário enviado por EnzoFerber em 25/09/2010 - 05:02h

Haha!
2 dias de Perl, dá um desconto ai vai... :)


Contribuir com comentário




Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts