Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Store multiple lines

by johngg (Canon)
on Feb 09, 2018 at 14:41 UTC ( [id://1208834]=note: print w/replies, xml ) Need Help??


in reply to Store multiple lines

If it is safe to assume that the "service-name" line immediately follows the "static-route" line, and the file is not huge, then I might read all of the data into a scalar then remove the \nxxxxxxxx- parts globally so that all the information for each item was on one line. That would remove the need for the logic of two separate regex matches simplifying the data extraction. I would then open a reference to the scalar and read that line by line. Something like:-

use strict; use warnings; open my $inFH, q{<}, \ <<EOD or die qq{open: < \ HEREDOC: $!\n}; dnsname1: static-route 1.1.1.1/30 next-hop 2.2.2.2 preferen +ce 200 bfd-enable tag 685 dnsname1- service-name "XYZ12345-s12345" dnsname2: static-route 3.3.3.3/30 next-hop 4.4.4.4 bfd-enab +le tag 635 dnsname2- service-name "XYZ67891-s67890" EOD my $data = do { local $/; <$inFH>; }; close $inFH or die qq{close: < \ HEREDOC: $!\n}; $data =~ s{\n\S+-}{}g; open $inFH, q{<}, \ $data or die qq{open: < \ scalar: $!\n}; while (<$inFH> ) { # Do a single data extraction regex here. ... } close $inFH or die qq{close: < \ scalar: $!\n};

I hope this is helpful.

Cheers,

JohnGG

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2024-03-28 11:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found