Print lines in one file matching patterns in another file

后端 未结 5 954
-上瘾入骨i
-上瘾入骨i 2020-11-29 06:45

I have a file with more than 40.000 lines (file1) and I want to extract the lines matching patterns in file2 (about 6000 lines). I use grep like this, but it is very slow: <

5条回答
  •  执念已碎
    2020-11-29 07:34

    You could try with this awk:

    awk 'BEGIN{i=0}
    FNR==NR { a[i++]=$1; next }
    { for(j=0;j

    The FNR==NR part specifies that the stuff following it in curly braces is only to be applied when processing the first input file (file2). And it says to save all the words you are looking for in an array a[]. The bit in the second set of curly braces applies to the processing of the second file... as each line is read in, it is compared with all elements of a[] and if any are found, the line is printed. That's all folks!

提交回复
热议问题