I need a way to extract data from
X 4T Solution 21 OCT 2011 37 .00
to account 12345678 User1 user2
X 4T Solution Solution Unlimited 11 Sep 2009 248 .00
to account 87654321 user3 user4
I need it to extract 'X' '37.00' and account number 12345678.
I have extracted above stuff from another data files and need to process on the result.
So what code I have is
if (/START OF STUFF/../End of Stuff/)
{
if ( /^X/../\D+\w\s+20[0-9][0-9]\s\d\.\d\n\d+/ ) {
print $_."\n";
}
}
I can extract the complete lines But i need only the matching one
Any suggestions...
Try this,
perl -nle 'if(/^X(.+?)\s(\d+\s?\.\d+)/) {printf "%s%s ","X ",$2;} if (/^to account\s+(\d+)\s+/){printf " and a/c no %s\n",$1}' inputfile
My proposal:
$\ = "\n";
$, = ': ';
while (<>) {
chomp;
if (my ($d, $m, $y, $w, $f) = m{^X.*?(\d+)\s+(\w+)\s+(\d+)\s+(\d+)\s+(\.\d+)$}) {
my $amount = $w + $f;
$_ = <>;
my ($account) = m{^to account (\d+)};
print "\n date", "$d-$m-$y";
print 'account', $account;
print ' amount', $amount;
}
}
Results in:
date: 21-OCT-2011
account: 12345678
amount: 37
date: 11-Sep-2009
account: 87654321
amount: 248.02
Hope this helps
@pravin27 On windows with perl 5 I get error:
Can't find string terminator "'" anywhere before EOF at -e line 1.
---------- Post updated at 04:54 AM ---------- Previous update was at 04:45 AM ----------
@ludwig
Thanks ... it works
I modified the code a little bit
$\ = "\n";
$, = ': ';
open ( FILE, "input.txt") || die "Unable to open bill_data.txt";
while (<FILE>) {
chomp;
if (my ($d, $m, $y, $w, $f) = m{^X.*?(\d+)\s+(\w+)\s+(\d+)\s+(\d+)\s+(\.\d+)$}) {
my $amount = $w + $f;
$_ = <FILE>;
my ($account) = m{^to account (\d+)};
print "\n date", "$d-$m-$y";
print 'account', $account;
print ' amount', $amount;
}
}
close(FILE);
But when I add to my original code .. .I does not work ... my original code below
#!/bin/perl
$| = 1;
$\ = "\n";
$, = ': ';
open ( FILE, "input.txt") || die "Unable to open txt file";
while(<FILE>) {
chomp;
if (/START OF STUFF/../End of Stuff/)
{
# print "Line:$. $_\n";
chomp;
if (my ($d, $m, $y, $w, $f) = m{^X.*?(\d+)\s+(\w+)\s+(\d+)\s+(\d+)\s+(\.\d+)$}) {
my $amount = $w + $f;
$_ = <FILE>;
my ($account) = m{^to account (\d+)};
print "\n date", "$d-$m-$y";
print 'account', $account;
print ' amount', $amount;
}
}
}
close (FILE);