in reply to Re: searching for multiple lines from outfile and printing them all in the final outfile
in thread searching for multiple lines from outfile and printing them all in the final outfile

thanks for responding

suppose say my output file has 100 lines.but out if 100 lines I just want 10 lines that I need to be printed in my final output file.I want to search for all 10 lines and print it in a saperate final file.

i know how to search for 1 line and print it in final output file but not miltiple lines in my final output file.

my current code only searches for one line and prints it in final output file

could you please advice how to do that/? Thanks!
  • Comment on Re^2: searching for multiple lines from outfile and printing them all in the final outfile

Replies are listed 'Best First'.
Re^3: searching for multiple lines from outfile and printing them all in the final outfile
by McDarren (Abbot) on May 16, 2009 at 04:23 UTC
    The canonical way is:
    open my $in_file, '<', 'some_file.txt' or die "$!\n"; while (my $line = <$in_file>) { chomp($line); # If necessary print if $line =~ /foo/; # Or some other conditional }
    Update:Note that in the above example you wouldn't actually want the chomp, but it's usually a good idea if you are doing more than just printing matching lines.

    Regards,
    Darren

Re^3: searching for multiple lines from outfile and printing them all in the final outfile
by Bloodnok (Vicar) on May 16, 2009 at 11:31 UTC
    If the lines you require are consecutive, then you could use the range operator to do it along the lines of i.e. untested...
    use warnings; use strict; use autodie; open INFILE, "<infile"; open OUTFILE, ">outfile"; while (<INFILE>) { print OUTFILE if /start regex/ .. /end regex/; } close INFILE; close OUTFILE;
    A user level that continues to overstate my experience :-))