Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optimizing grep (or using AWK) in a shell script

Tags:

grep

shell

awk

In my shell script, I am trying to search using terms found in a $sourcefile against the same $targetfile over and over.

My $sourcefile is formatted as such:

pattern1
pattern2
etc...

The inefficient loop I have to search with is:

for line in $(< $sourcefile);do
    fgrep $line $targetfile | fgrep "RID" >> $outputfile
done

I understand it would be possible to improve this by either loading the whole $targetfile into memory, or perhaps by using AWK?

Thanks

like image 532
Ode Avatar asked Dec 22 '22 03:12

Ode


1 Answers

Am I missing something, or why not just fgrep -f "$sourcefile" "$targetfile"?

like image 197
Arkku Avatar answered Jan 13 '23 23:01

Arkku