Discard content from the search of a file - Perl

0

I'm currently building a script to find a code in a document that has "x" amount of text, so far I can get the matches or matches but I can not bring only the code because it brings me the whole line where the code is found, I just want to discard all the text and bring me only the code "500 5.1.1" If it is inside the document. Any help would be very grateful!

use strict;
use warnings;

my $resultado;

open (BUSCAR, "<", "/home/lserrano/Documentos/perl/files/devueltos2018ls.txt");

while (<BUSCAR>){
  if(m/([5])(0)/){
    $resultado = $_;
    print $resultado;
  };
}
    
asked by Luis Alfredo Serrano Díaz 03.12.2018 в 19:48
source

1 answer

1

You do not tell us what the codes should look like.

If we assume that the codes are in the form of a three-digit number, followed by a space, followed by a sequence of numbers interspersed with points, we can use this pattern:

while (<BUSCAR>){
  if(m/(\d{3}\s\d[.]\d[.]\d)/){
    $resultado = $1;    # el resultado está en el primer par de paréntesis
    print $resultado;
  };
}
    
answered by 03.12.2018 в 22:48