I am trying to do a multiple pattern search (50 of them) in a large log file (around 300MB size) using the strstr function:
Code:
while( getline( ifstreamobj , line_in_logfile ) )
{
if (strstr(line_in_logfile.c_str(), pattern1) != 0 ) ++Addcounter1 ;
if (strstr(line_in_logfile.c_str(), pattern2) != 0 ) ++Addcounter2 ;
if (strstr(line_in_logfile.c_str(), pattern3) != 0 ) ++Addcounter3 ;
...........
so on, upto 50 patterns
}
I also created a script having multiple greps, like "grep -c pattern1 logfile" which does a similar function.
I timed both the C++ program above and the script. The c++ program above took "twice the time" the script took to execute.
I tried using fopen and fgets instead of fstreams, but only ended up getting the same results.
Its really frustating to be humbled by a shell script
Can someone suggest me an alternated logic in my c++ program.
Thanks