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!
| [reply] [Watch: Dir/Any] |
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 | [reply] [Watch: Dir/Any] [d/l] |
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 :-))
| [reply] [Watch: Dir/Any] [d/l] |