Ignore PERL lines

Asked

Viewed 31 times

-1

I made a script that reads the lines of a file, but now I need to ignore the first 3 lines and the last, returning only what is in the middle.

Below is a text file and script template.

text.txt:

H000000000000000000dsadsadsadsa0sad0sa0da0sd0sad0asd0asd0sa0s
H000000000000000000dsadsadsadsa0sad0sa0da0sd0sad0asd0asd0sa0s
H000000000000000000dsadsadsadsa0sad0sa0da0sd0sad0asd0asd0sa0s

FFFFFFFFF TESTE12 N TESTE12
FFFFFFFFF TESTE13 N TESTE13
FFFFFFFFF TESTE14 N TESTE14
FFFFFFFFF TESTE15 N TESTE15

T9999999999999999999esksddjsadsdk

script.:

# ! /usr /bin /perl

use strict;
use warnings;

my $filename = 'data.txt';
open(my $fh, '<:encoding(UTF-8)', $filename);

while (my $row = <$fh>) {
  chomp $row;
  print "$row\n";
}
print "Final\n";

If anyone can give a direction, thank you because I’m looking at some materials on the internet but still I’m having difficulties...

1 answer

0

Below is the code ready. I left it commented as I understood it, in case there is something wrong I apologize, but the code is working perfectly. NOTE: The code eliminates the header and footer and also separates the remaining content.After reading it generates an output file with the information...

        #!/usr/bin/perl 
        use strict; 
        use warnings;

        my $fh; 
        my $fhout; 
        my $filename = 'data.txt';  # Entrada do arquivo 
        if (open($fh, "<:encoding(UTF-8)", $filename)) { # Abre o arquivo    
          open($fhout, ">:encoding(ASCII)", 'saida.txt');  # Arquivo de saída   
          while (my $row = <$fh>) {  # Enquanto retornar algo faça...
             next if ($row =~/^[HT]/); # Desconsidera linhas que comecem com H e T
             $row =~ s/\s+$//;
       #     my ($funcional, $login, $nome) = ($row =~ /^([0-9]{9}) ([^\s]+) N (.+)$/); 
       # Caso a funcional receba um valor numérico de 9 digitos, faz a leitura e separação
             my ($funcional, $login, $nome) = ($row =~ /^(F{9}) ([^\s]+) N (.+)$/); # Faz a leitura e separação
             print "$row\n";
             print $fhout "funcional $funcional\n";
             print $fhout "login $login\n";
             print $fhout "nome $nome\n";   } } print "Fim!\n";
       #     close ($fh);       # Fecha o arquivo de entrada
       #     close ($fhout);    # Fecha o arquivo de saida

Browser other questions tagged

You are not signed in. Login or sign up in order to post.