Reputation: 11
I have a file with the format bellow and I need to use Perl and regex to get the values to insert a database. I try to use regex, but I have errors. Follow the script I use to get the first value:
# line 1
open(my $fin, "<", "input.txt")
or die "cannot open < input.txt: $!";
my $line1 = <$fin>;
chomp $line1;
print "$line\n";
my ($codigo, $nome) = ($line1 =~ m/^((\d)+)\s[\S]\s(([\s\d\w])+)$/);
print "#$codigo#$nome#\n";
# line 2
$line2 = <$fin>;
chomp $line2;
..
But I don´t see the correct value of "codigo" and "nome".
The format of file is:
84404167 - NAME ONE OF SILVA R NONONONO, 143334, HOUSE - REGION - CITY - 81280400 Res: (22)5555.4543 Cel: (33)5555.8659 Ou: Início: 17/12/2013 - Data de aniv.: 23/02/1955 Crédito: 1440 - Crédito Disponível: 1152 - Status: ATIVA 96071311 - NAME SECOND OF JOSE R SECRET ADDRESS NONONNO, 433, ap 232 b azaleia - fazendinha - CURITIBA - 81320420 Res: Cel: (22)5555.9776 Ou: (33)5555.2352 Início: 22/01/2015 - Data de aniv.: 10/05/1981 Crédito: 764 - Crédito Disponível: 516 - Status: ATIVA
Upvotes: 1
Views: 243
Reputation: 4709
Instead of regex you can use split
function:
#!/usr/bin/perl
use warnings;
use strict;
open my $fin, "<", "abc.txt" or die "cannot open input.txt: $!";
while (my $line1 = <$fin>)
{
chomp $line1;
if ($. == 1) # line 1
{
my ($codigo, $nome) = split (/\s*-\s*/, $line1);
print "codigo: $codigo nome: $nome\n";
}
}
Output:
codigo: 84404167 nome: NAME ONE OF SILVA
Upvotes: 1