I have a very large file of size about 300-500 MB. I need to search for String1 in that file first. Then search for String2 starting from the position of String1. Then again search for String3 starting from the position of String2. For example,
String1 = "abc"
String2 = "123"
String3 = "opq"
File :
def
123
opq
opq
123
opq
abc //come here first
blah blah
123 //come here next
blah
opq //read this finally and print
afg
123
blah blah
123
def
Methods I followed,
I tried reading the file line by line and searching for the matching pattern.
It was a very slow method (had to wait for minutes).
Then I stored the whole file into an array and grepped the matching lines to get the final line.
It was quite fast in searching but slower in loading the file into an array. The memory consumed is also high.
Is there an efficient method to perform such a task?
Using a perl one liner and range operators:
perl -ne 'print("$. $_") && exit if (/abc/ .. 1) && (/123/ .. 1) && /opq/' file
Outputs:
11 opq //read this finally and print
Switches:
-n
: Creates a while(<>){..}
loop for each line in your input file. -e
: Tells perl
to execute the code on command line. Code:
print("$. $_")
: prints the line number $.
followed by the current line $_
exit
: Terminate processing after desired line is found.if (/abc/ .. 1) && (/123/ .. 1) && /opq/
: Find patterns in order. I would advise against shelling out to another perl process to achieve this functionality. Instead just convert this to a non-commandline version:
use strict;
use warnings;
use autodie;
open my $fh, '<', 'file';
while (<$fh>) {
if ((/abc/ .. 1) && (/123/ .. 1) && /opq/) {
print "$. $_";
last;
}
}