Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: Split file using perl and regexp

by AnomalousMonk (Chancellor)
on Jan 17, 2013 at 22:37 UTC ( #1013903=note: print w/replies, xml ) Need Help??

in reply to Split file using perl and regexp

Here's an approach based on the observation of certain similarities (common prefix characters) in the data fields of interest in the three different types of data files. No discrimination between the three data file types is needed in the code.

Some notes of caution:

  • The code shown assumes the data being fed to it is valid. It is intended only as an example of a regex-based approach.
  • The code is critically dependent on the definition of the  $rx_oct regex. (I gave it this name because it superficially suggests an IP octet.) The OP shows only limited examples of this sub-field in the range (10 .. 13). You (brad_nov) will have to change this regex to reflect the real data – or else maybe reveal an actual spec!

>perl -wMstrict -le "my @records = ( '1|1212|34353|56575|||||4|~some~~pi=[],uid=[11]}~', '1|1212|34353|56575|||||4|~som~~390=,391=222,394~', '1|1212|34353|56575|||||4|~somedata~', ); ;; my $rx_oct = qr{ \d{1,3} }xms; my $rx_quint = qr{ $rx_oct (?: \. $rx_oct){4} }xms; ;; my $rx_dotted = qr{ (?<! \d) $rx_quint (?! \d) }xms; my $rx_int = qr{ \d+ }xms; ;; for my $record (@records) { print qq{'$record'}; my ($const, $var) = $record =~ m{ ( \A .+) \| ( .* \z) }xms; my (undef, $dotted, $int) = $var =~ m{ (\D) ($rx_dotted) .*? \1 ($rx_int) }xms; my $new_record = join '|', $const, $dotted, $int; print qq{'$new_record' \n}; } " '1|1212|34353|56575|||||4|~some~~pi=[],uid=[11]}~' '1|1212|34353|56575|||||4||11' '1|1212|34353|56575|||||4|~som~~390=,391=222,394~' '1|1212|34353|56575|||||4||222' '1|1212|34353|56575|||||4|~somedata~' '1|1212|34353|56575|||||4||3333'

Update: After playing around with this a bit and doing a little, um, testing, I think I would change the definition of  $rx_dotted as follows (change to final look-ahead):
    my $rx_dotted = qr{ (?<! \d) $rx_quint (?! [.\d]) }xms;
This change does not affect behavior for valid records.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1013903]
[choroba]: megalag :-(
[erix]: ok, I looked it up, should have been: "Haribo macht Kinder froh und Erwachsene ebenso"
[Discipulus]: also mixing pizza (fermented/levited ) with juices is not so good
[erix]: D.: yeah, I am in the low countries. To be precise, I am 0.5m above sea level
[erix]: going for a run in the dunes - unless the storm (well, Beaufort 7) gets fiercer
Discipulus had stew with tomato sauce, onions, carrots and potatoes + bottle of red wine and still warm bred.. slept as dynosaur

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (10)
As of 2017-11-22 09:10 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (316 votes). Check out past polls.