Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: file parsing

by martin (Friar)
on Jul 11, 2008 at 05:04 UTC ( [id://696890]=note: print w/replies, xml ) Need Help??


in reply to file parsing

While XML parsing might help a lot if you wanted to process the structure of the file, weeding out stuff between certain markers is a lot easier. I see a classical use case for the flip-flop operator here. You want to copy input to output, skipping some of the input. Seeing certain patterns should switch between the two modes. Perl can do that like this:
#!/usr/bin/perl while (<>) { next if m{^\Q<?xml version="1.0" encoding="UTF-8"?>} ... m{</table>}; print; }
Note the use of \Q to protect a literal string in a regex. The three dots are not something to fill in but precisely three dots here. This is (in scalar context) a flip-flop operator evaluating either the left or the right expression according to its state. To catch beginning and end markers on the same line, you'd have to replace the three dots by two dots. Also look up the -n and the -i switch in perlrun if you like to edit your files in-place with a single short command line.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-04-18 06:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found