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

Hi Perl Buddies,

I am a beginner.I was trying to extract text from a text file using regex and not able to do it succefully.

I have a text file and i want to extract everything from a particular line starting with a character * till end of file.

the file somewhat looks like this

"Gain immediate access and...

*Check if your claims have been finalized.

*Sign up for alerts about your claim activity.

*Print a temporary ID card.

*View up to 18 months of claim payment details.

*Much, much more!

In addition you, your spouse and children can:

*Search for Doctors and Hospitals in your area.

*Begin a program to stop smoking or lose weight.

*Learn more about a specific disease or condition".

I mainly have to extract text from the first "*" till end of file.

The above example is just a sample ,the actual text file is huge but is in the same format.

Thankyou for the help. Best Regards

  • Comment on Extracting data from a particular line to end of the file

Replies are listed 'Best First'.
Re: Extracting data from a particular line to end of the file
by jwkrahn (Abbot) on Dec 21, 2010 at 02:37 UTC
    i want to extract everything from a particular line starting with a character * till end of file.
    while ( <FILEHANDLE> ) { print if /\A\*particular line/ .. eof FILEHANDLE; }
Re: Extracting data from a particular line to end of the file
by Anonymous Monk on Dec 21, 2010 at 02:10 UTC

      Sorry the code looks like below

      open (file1,"<$dir/seq.results"); while (<file1>){ chomp $_; @lines= split(/\n/,$_); foreach $line(@lines){ if ($line = ~m/^*/){ push (@final,$line); } } } foreach (@final) { open (file2,">$dir/seq.out"); print file2 $_; close file2; } close file1;
        The simple implementation of this is like below. Keep a record of whether the "start" has happened yet. And if it has, then print all the lines after that and including that one.

        code example and output.. click below..

        More specific version of your code:
        open (my $file1,"<$dir/seq.results") or die "cannot open "$dir/seq.res +ults"; open (my $file2,">$dir/seq.out") or die "cannot open $dir/seq.out" for + write"; my $started =0; while (<$file1>) { $started =1 if (m/^\*/); #first line starting with * is seen print $file2 "$_" if $started; }
        Note: '*' has special meaning within a regex and it has to be "escaped" to get the literal value of '*'.

        Yes, there are ways of coding this in a more brief way. But do not confuse brevity with execution efficiency and certainly not clarity.

        Oh, in a very simple program like this (6 lines), there is no need to explicitly close the file handles ($file1 and $file2). The OS will do that for you when the program exits. In longer programs there can be good reasons to do that.

Re: Extracting data from a particular line to end of the file
by CountZero (Bishop) on Dec 21, 2010 at 07:23 UTC
    So in your above example you will print the whole file, as the first line already starts with a '*'?

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      Yes that would print the whole file in the example,but the point was to print everything starting from any character like * or other.* was just an example. Thankyou