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

Hi PerlMonks: I am new to Perl and need to ramp up in a hurry. Need to extract data from bunch of text files. Each file has text something like this:
******************************************* junk1 junk2 junk3 x data line1 data line2 data line3 y junk4 junk5 ********************************************
I need to extract the data between x and y into a corresponding file (for each input file). Final data in each file will look like:
********************************** data line1 data line2 data line3 ******************************** The input data file names are: data_0001.txt data_0002.txt data_0003.txt
Any help is greatly appreciated and will be great learning tool for a new user.

Replies are listed 'Best First'.
Re: extract data from text file
by kcott (Archbishop) on Oct 11, 2015 at 04:23 UTC

    G'day ikhan,

    Welcome to the Monastery.

    "I am new to Perl and need to ramp up in a hurry."

    I suggest you read "perlintro -- a brief introduction and overview of Perl". There may be sufficient information there to write your program: it certainly covers all the areas it looks like you need. Each section has links to further documentation: so follow them for more details as required.

    When you post code or data here, please wrap it in <code>...</code> tags. Unfortunately, by posting your data as plain text, we've got no idea what the file structure looks like. This makes it difficult to suggest a solution. I'll take a punt and point you to the examples in "perlop: Range Operators" — I really am just guessing.

    I suspect you may have given us enough information: it's just not in useable format. "How do I post a question effectively?" gives more details about this. "How do I change/delete my post?" tells you how to fix it.

    — Ken

Re: extract data from text file
by Laurent_R (Canon) on Oct 11, 2015 at 08:51 UTC
    Please use <code> and </code> tags for your input data samples and expected output result (as well as code examples), and more broadly, take a look at Markup in the Monastery for formatting your posts.

    It would be nice if you could show what code you have attempted and tell us in which way it fails to meet your requirement, so that we could help overcoming specific difficulties, rather than expecting us monks writing the full code for you, which would be of very little pedagogical benefit for you.

    Here, the basic idea should be to read the file line by line, to set up a flag when you meet the x start criteria and set if off when you find the y end criteria. And of course to print out the lines when the flag is set.

    So the pseudo-code could be something like this:

    flag = false; while (there are lines in the file) { if (line equals "x") { flag = true; } elsif (line equals "y") { flag = false; } else { if (flag) { print line; } } }
    There are many ways of translating this into actual Perl code, and there are shortcuts which could make the actual Perl code shorter, but there are also a few more things to be thought about in actual code, such as dealing with end-of-line characters, etc.

    Please try to convert this pseudo-code into Perl and tell us where you're encountering problems.

Re: extract data from text file
by NetWallah (Canon) on Oct 11, 2015 at 05:33 UTC