peeeerld has asked for the wisdom of the Perl Monks concerning the following question:

I need to find data in a file and print it out in a neat fashion. I believe I can use regex to do this but I am unsure of exactly how. I need it to parse data in this format: example.txt "unimportant1" "unimportant2" "unimportant3" "time" "data1" "data2" I would like it to print out the data1 and data2 such as: Data1: data1 Data2: data2 I appreciate your help! Thank you.
  • Comment on Reading and Separating Data From a File Using Regex

Replies are listed 'Best First'.
Re: Reading and Separating Data From a File Using Regex
by moritz (Cardinal) on Jan 20, 2009 at 07:35 UTC
Re: Reading and Separating Data From a File Using Regex
by ww (Archbishop) on Jan 20, 2009 at 14:10 UTC

    Perhaps you have a good reason for eschewing use of a module, but comments like "I am trying to do it in regex and not use a specific module." are often ill-founded unless rooted in exercising/expanding your knowledge.

    Nonetheless, split and a regex will do the job (for your limited sample data):

    use warnings; use strict; # 737495 my $text = ' example.txt "unimportant1" "unimportant2" "unimportant3" +"time" "data1" "data2" '; my @words = split/ /,$text; for my $word(@words) { if ( $word =~ /(data)(\d)/ ) { print "Data" . $2 . ": $1$2 "; } } =head Execution: perl 737495.pl Data1: data1 Data2: data2 =cut
Re: Reading and Separating Data From a File Using Regex
by 1Nf3 (Pilgrim) on Jan 20, 2009 at 09:38 UTC

    Could you provide your input and desired output in the <code> tags?

    Right now, I'm unsure why you wrote that time, data1 and data2 are significant, but there is no time in your output.

    As moritz suggested, Text::CSV is probably your best friend here.

    Regards,
    Luke

      Thanks for your suggestions, but I am trying to do it in regex and not use a specific module. Any idea on how to get it done like that? Thanks again!

        If this is not just a learning exercise in how to use regular expressions, see Yes, even you can use CPAN for how you, yes, you can incorporate code from the CPAN into your programs.

        If this is a learning exercise in regular expressions, we will need to see the code. When we see your code, we can best advise you where to look and what documentation to read to improve your code. Please help us to help you better.

Re: Reading and Separating Data From a File Using Regex
by AnomalousMonk (Archbishop) on Jan 20, 2009 at 18:05 UTC
    Another approach using regexes: extract quoted groups:
    use warnings; use strict; MAIN: { my $qc = q{"}; # quoting character my $quoted_body = qr{ [^$qc\\]* (?: \\. [^$qc\\]* )* }xms; while (<DATA>) { my (undef, undef, undef, $time, $data1, $data2) = m{ $qc ($quoted_body) $qc }xmsg; print "time: <$time> data1: <$data1> data2: <$data2> \n"; } } __DATA__ example.txt "unimportant1" "unimportant2" "unimportant3" "timeA" "data +1A" "data2A" other.txt "ignore1" "ignore2" "ign\"ore3" "timeB" "data1B" "dat\"a2B" foo.txt "ignore1" "" "ign\"ore3" " timeC " "" "dat\"a2C"
    Output:
    time: <timeA> data1: <data1A> data2: <data2A> time: <timeB> data1: <data1B> data2: <dat\"a2B> time: < timeC > data1: <> data2: <dat\"a2C>
      Thanks for the great replys. I am afraid the idea was a bit skewed, though, and instead of example.txt being part of the separation data, I will be reading from example.txt then separating the rest of the data. This is indeed both a learning experience and a problem I came across writing a solution for a task that needs to be done, but I feel like if these could read from a file and split the data they should be golden! Could the revisions be made to suite reading from a file? Thanks so much!

        Perhaps your concept of the Monastery is a "bit skewed." This is not a code-writing machine, nor are the Monks your free-labor pool.

        Please pay respect to their willingness to provide free help (at those times you get stuck after making diligent efforts) by reviewing The Perl Monks Guide to the Monastery and PerlMonks FAQ.

        Then, perhaps you should learn about "read(ing) from a file" and try using your own labor to adapt or adopt the answers you've already been given. A good introductory text (Learning Perl leaps to mind); the Tutorials here; or perhaps even Super Search will give you lots to work with.

        Had you read such nodes as On asking for help and How do I post a question effectively?, you might not be skating so close to multiple downvotes: for the lack of effort displayed (or lack of displayed effort); for posting a question so readily answered with minimal effort; and just BTW for misspelling "suit."

        :-)