Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^7: Regexp matching on a multiline file: dealing with line breaks

by Athanasius (Archbishop)
on Dec 11, 2015 at 09:58 UTC ( [id://1150007]=note: print w/replies, xml ) Need Help??


in reply to Re^6: Regexp matching on a multiline file: dealing with line breaks
in thread Regexp matching on a multiline file: dealing with line breaks

I think choroba is proposing a solution along these lines:

#! perl use strict; use warnings; my $target = 'kitten'; my $count = 0; { local $/ = "\n>"; my $first = 1; while (my $string = <DATA>) { if ($first) { next unless $string =~ /^>/; } (my $header, $string) = split /\n/, $string, 2; printf "Header: '%s%s'\n", ($first ? '' : '>'), $header; $string =~ s/\n//g; print "string is '$string'\n"; $count += () = $string =~ /\Q$target/g; } continue { $first = 0; } } print "The target string '$target' occurs $count times in the file\n"; __DATA__ not a header kittens >Header1 sushikitten ilovethekit tensushithe kittenisthe >Header2 sushikittAn ilovethekit tensushithe kittBnisthe

Output:

19:57 >perl 1474a_SoPW.pl Header: '>Header1' string is 'sushikittenilovethekittensushithekittenisthe>' Header: '>Header2' string is 'sushikittAnilovethekittensushithekittBnisthe' The target string 'kitten' occurs 4 times in the file 19:57 >

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1150007]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-03-19 05:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found