Bash

提取包含模式文件的行並將它們保存在不同的文件中 grep、sed、awk、print

  • September 5, 2020

我有這個文件

-   .   ID  =   tom_fa_10005086
-   0   Parent  =   tom_fa_10005086
-   0   Parent  =   tom_fa_10005086
-   2   Parent  =   tom_fa_10005086
-   1   Parent  =   tom_fa_10005086
-   0   Parent  =   tom_fa_10005086
-   0   Parent  =   tom_fa_10005086
+   .   ID  =   tom_fa_10013928
+   0   Parent  =   tom_fa_10013928
+   0   Parent  =   tom_fa_10013928
+   0   Parent  =   tom_fa_10013928
+   0   Parent  =   tom_fa_10013928
+   2   Parent  =   tom_fa_10013928
+   2   Parent  =   tom_fa_10013928
+   1   Parent  =   tom_fa_10013928
+   1   Parent  =   tom_fa_10013928
+   2   Parent  =   tom_fa_10013928
+   2   Parent  =   tom_fa_10013928
-   .   ID  =   tom_fa_10000024
-   0   Parent  =   tom_fa_10000024
-   .   ID  =   tom_fa_10004587
-   0   Parent  =   tom_fa_10004587
-   1   Parent  =   tom_fa_10004587

而這些模式

COL 1           

tom_fa_10005086
tom_fa_10013928
tom_fa_10000024
tom_fa_10011338
tom_fa_10003474

我想在模式與行匹配時提取行並將其保存在文件中。

但是我有 150 種模式,我需要將每個匹配項保存在不同的文件中嗎?所以我有 150 個模式,我將有 150 個輸出文件。

結果如下所示:

文件 1

  -   .   ID  =   tom_fa_10005086
   -   0   Parent  =   tom_fa_10005086
   -   0   Parent  =   tom_fa_10005086
   -   2   Parent  =   tom_fa_10005086
   -   1   Parent  =   tom_fa_10005086
   -   0   Parent  =   tom_fa_10005086
   -   0   Parent  =   tom_fa_10005086

文件 2

+   .   ID  =   tom_fa_10013928
+   0   Parent  =   tom_fa_10013928
+   0   Parent  =   tom_fa_10013928
+   0   Parent  =   tom_fa_10013928
+   0   Parent  =   tom_fa_10013928
+   2   Parent  =   tom_fa_10013928
+   2   Parent  =   tom_fa_10013928
+   1   Parent  =   tom_fa_10013928
+   1   Parent  =   tom_fa_10013928
+   2   Parent  =   tom_fa_10013928
+   2   Parent  =   tom_fa_10013928

文件 3 ……

我在想:

grep -f 文件 1 文件 1 | ……

grep -E

給定一個patterns文件:

$ cat patterns
tom_fa_10005086
tom_fa_10013928
tom_fa_10000024
tom_fa_10011338
tom_fa_10003474

然後

awk 'NR==FNR{a[$1]=NR; next} $NF in a {print > "outfile" a[$NF]}' patterns file

產生以下文件:

$ head outfile?
==> outfile1 <==
-   .   ID  =   tom_fa_10005086
-   0   Parent  =   tom_fa_10005086
-   0   Parent  =   tom_fa_10005086
-   2   Parent  =   tom_fa_10005086
-   1   Parent  =   tom_fa_10005086
-   0   Parent  =   tom_fa_10005086
-   0   Parent  =   tom_fa_10005086

==> outfile2 <==
+   .   ID  =   tom_fa_10013928
+   0   Parent  =   tom_fa_10013928
+   0   Parent  =   tom_fa_10013928
+   0   Parent  =   tom_fa_10013928
+   0   Parent  =   tom_fa_10013928
+   2   Parent  =   tom_fa_10013928
+   2   Parent  =   tom_fa_10013928
+   1   Parent  =   tom_fa_10013928
+   1   Parent  =   tom_fa_10013928
+   2   Parent  =   tom_fa_10013928

==> outfile3 <==
-   .   ID  =   tom_fa_10000024
-   0   Parent  =   tom_fa_10000024

引用自:https://unix.stackexchange.com/questions/515574