Perl Regex matching multiple lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl Regex matching multiple lines
# 1  
Old 01-07-2011
Perl Regex matching multiple lines

I need a way to extract data from

Code:
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

Code:
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...
# 2  
Old 01-07-2011
Try this,
Code:
 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

# 3  
Old 01-07-2011
My proposal:
Code:
$\ = "\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:
Code:
   date: 21-OCT-2011
account: 12345678
 amount: 37

   date: 11-Sep-2009
account: 87654321
 amount: 248.02

Hope this helps
# 4  
Old 01-07-2011
@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
Code:
$\ = "\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

Code:
#!/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);

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grepping or awking multiple lines in a file - regex

data.txt: hellohellohello mellomello1mello tellotellotellotello bellobellowbellow vellow My attempts: egrep ".*mello1\n.*bellow" data.txt awk '/.*mello1.*\nbellow/' data.txt how can i search for patterns that are on different lines using simple egrep or awk? i only want the... (7 Replies)
Discussion started by: SkySmart
7 Replies

2. Shell Programming and Scripting

Perl Matching multiple variables

I am trying to match mulitple (3) variables. I found the sub given below on the web which works well when all vars are defined. But there are situations where one or two will not be defined (at least one will always be defined.) Example of the variable content possibilities The sub found... (4 Replies)
Discussion started by: popeye
4 Replies

3. Shell Programming and Scripting

Perl script: matching multiple lines error

Dear Perl users, Could somebody help me how to fix my code so I can get my desired output. Here is the data: Pattern Gabriel halo1 halo2 end Pattern Andreas halo1 halo2 endI want to grep multiple lines between the pattern /Pattern Gabriel / and /end/. Then I will store the output into... (6 Replies)
Discussion started by: askari
6 Replies

4. Shell Programming and Scripting

Filter (by max length) only lines not matching regex

I have a large file of many pairs of sequences and their headers, which always begin with '>' I'm looking for help on how to retain only sequences (and their headers) below a certain length. So if min length was 10, output would be I can filter by length, but I'm not sure how to exclude... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

5. Shell Programming and Scripting

Perl: Regex, string matching

Hi, I've a logfile which i need to parse and get the logs depending upon the user input. here, i'm providing an option to enter the string which can be matched with the log entries. e.g. one of the logfile entry reads like this - $str = " mpgw(BLUESOAPFramework):... (6 Replies)
Discussion started by: butterfly20
6 Replies

6. Shell Programming and Scripting

Perl XML, find matching condition and grep lines and put the lines somewhere else

Hi, my xml files looks something like this <Instance Name="New York"> <Description></Description> <Instance Name="A"> <Description></Description> <PropertyValue Key="false" Name="Building A" /> </Instance> <Instance Name="B"> ... (4 Replies)
Discussion started by: tententen
4 Replies

7. Shell Programming and Scripting

pattern matching over multiple lines and deleting the first

I've got a longish log file with content such as Uplink traffic: Downlink traffic: I want to parse the log file and remove any line that contains the string "Uplink traffic:" at the beginning of the line, but only if the line following it beginnings with the string "Downlink traffic:" (in... (7 Replies)
Discussion started by: Yorkie99
7 Replies

8. Shell Programming and Scripting

Perl regex help - matching parentheses

Let's say I'm trying to match potentially multiple sets of parentheses. Is there a way in a regular expression to force a match of closing parentheses specifically in the number of the opening parentheses? For example, if the string is "((foo bar))", I want to be able to say "match any number of... (7 Replies)
Discussion started by: cvp
7 Replies

9. Shell Programming and Scripting

regex to delete multiple blank lines in a file?

can't figure out a way to delete multiple empty lines but keep single empty lines in a file, file is like this #cat file 1 2 3 4 5 6 - What I want is 1 2 (6 Replies)
Discussion started by: fedora
6 Replies

10. Shell Programming and Scripting

find -regex: matching multiple extensions

I want to use find to locate files with two different extensions, and run a grep on the results. The closest I have gotten is incredibly slow and ugly: for i in `ls -laR|egrep -e '(.js|.css)'`; do find . -name $i -print|xargs grep -H searchBg; done; This method makes my eyes bleed. Help! ;) ... (2 Replies)
Discussion started by: r0sc0
2 Replies
Login or Register to Ask a Question