G
googler
Inside my Perl script, I had to check if a particular pattern appears
in a certain file or not (only a yes/no answer). I did it as below:
@matching_lines = grep { /$srchpat/ } <MYFILE>;
print "Pattern found\n" if ($#matching_lines != -1);
I was wondering if there is a more efficient way to do this. Is it
possible to use the Unix "grep" command to do this inside my script?
If so, how? Will that be more efficient (faster)?
I have another question. Is there a way to read a particular line in a
file when I know the line number (without using a loop and reading
each line at a time)? I guess the below code would work.
@lines = <MYFILE>;
$myline = $lines[$linenum-1];
But this will read the entire file into the array @lines and can take
up a lot of memory if the file is huge. Is there a more efficient
solution?
Thanks.
in a certain file or not (only a yes/no answer). I did it as below:
@matching_lines = grep { /$srchpat/ } <MYFILE>;
print "Pattern found\n" if ($#matching_lines != -1);
I was wondering if there is a more efficient way to do this. Is it
possible to use the Unix "grep" command to do this inside my script?
If so, how? Will that be more efficient (faster)?
I have another question. Is there a way to read a particular line in a
file when I know the line number (without using a loop and reading
each line at a time)? I guess the below code would work.
@lines = <MYFILE>;
$myline = $lines[$linenum-1];
But this will read the entire file into the array @lines and can take
up a lot of memory if the file is huge. Is there a more efficient
solution?
Thanks.