在shell脚本中优化grep(或使用AWK)

Ode*_*Ode 4 shell awk grep

在我的shell脚本中,我试图使用$ sourcefile中找到的术语一遍又一遍地搜索相同的$ targetfile.

我的$ sourcefile格式如下:

pattern1
pattern2
etc...
Run Code Online (Sandbox Code Playgroud)

我必须搜索的低效循环是:

for line in $(< $sourcefile);do
    fgrep $line $targetfile | fgrep "RID" >> $outputfile
done
Run Code Online (Sandbox Code Playgroud)

我知道可以通过将整个$ targetfile加载到内存中,或者使用AWK来改进这一点吗?

谢谢

Ark*_*kku 7

我错过了什么,或者为什么不fgrep -f "$sourcefile" "$targetfile"呢?