linux 如何在bash脚本中只获取模式的第一个示例

pftdvrlh  于 2023-02-15  发布在  Linux
关注(0)|答案(1)|浏览(127)

大家好,请帮助我,我正试图得到以下输出,所以基本上我有2个输入文件,我们只需要共同的:名称从两个输入文件以及下面的行。名称/的/文件行。
到目前为止,我已经尝试过:

awk 'FNR==NR { a[$1]; next }NF<=1   { flag=0   } $1 in a { flag=1   }flag' file1 file2

输出:

:name1
    ./name/of/file [logfile] [ error in file coming since Day : 1 ]
    ./name/of/file [logfile] [ error in file coming since Day : 1 ]
:name3
    ./name/of/file [logfile] [ error in file coming since Day : 24 ]
    ./name/of/file [logfile] [ error in file coming since Day : 24 ]
:name1
    ./name/of/file [logfile] [ error in file coming since Day : 40]
    ./name/of/file [logfile] [ error in file coming since Day : 40 ]
:name4
    ./name/of/file [logfile] [ error in file coming since Day : 3 ]
    ./name/of/file [logfile] [ error in file coming since Day : 3 ]
    ./name/of/file [logfile] [ error in file coming since Day : 3 ]
    ./name/of/file [logfile] [ error in file coming since Day : 3 ]

:name4
./name/of/file [logfile] [ error in file coming since Day : 10 ]
./name/of/file [logfile] [ error in file coming since Day : 10 ]
./name/of/file [logfile] [ error in file coming since Day : 10 ]
./name/of/file [logfile] [ error in file coming since Day : 10 ]

但是它会打印所有出现的:name1和:name4,我们只需要输入文件1中第一次出现的:name1和:name4

    • 输入文件1:**
:name1
./name/of/file [logfile] [ error in file coming since Day : 1 ]
./name/of/file [logfile] [ error in file coming since Day : 1 ]

:name2
./name/of/file [logfile] [ error in file coming since Day : 1 ]

:name3
./name/of/file [logfile] [ error in file coming since Day : 24 ]
./name/of/file [logfile] [ error in file coming since Day : 24 ]

 :name1
./name/of/file [logfile] [ error in file coming since Day : 40]
./name/of/file [logfile] [ error in file coming since Day : 40 ]

:name4
./name/of/file [logfile] [ error in file coming since Day : 3 ]
./name/of/file [logfile] [ error in file coming since Day : 3 ]
./name/of/file [logfile] [ error in file coming since Day : 3 ]
./name/of/file [logfile] [ error in file coming since Day : 3 ]

:name5
./name/of/file [logfile] [ error in file coming since Day : 6 ]
./name/of/file [logfile] [ error in file coming since Day : 6 ]

:name4
./name/of/file [logfile] [ error in file coming since Day : 10 ]
./name/of/file [logfile] [ error in file coming since Day : 10 ]
./name/of/file [logfile] [ error in file coming since Day : 10 ]
./name/of/file [logfile] [ error in file coming since Day : 10 ]
    • 输入文件2:**
:name1
:name3
:name4
    • 所需输出文件**:
:name1
    ./name/of/file [logfile] [ error in file coming since Day : 1 ]
    ./name/of/file [logfile] [ error in file coming since Day : 1 ]
:name3
    ./name/of/file [logfile] [ error in file coming since Day : 24 ]
    ./name/of/file [logfile] [ error in file coming since Day : 24 ]
:name4
    ./name/of/file [logfile] [ error in file coming since Day : 3 ]
    ./name/of/file [logfile] [ error in file coming since Day : 3 ]
    ./name/of/file [logfile] [ error in file coming since Day : 3 ]
    ./name/of/file [logfile] [ error in file coming since Day : 3 ]
kb5ga3dv

kb5ga3dv1#

由于"occurrence"基于a[]数组中作为索引存在的名称,因此从数组中删除该项应禁用任何其他"occurrences"。
我们可以通过在当前的awk代码中添加一个delete来完成此删除操作:

awk '
FNR==NR { a[$1]; next          }
NF<=1   { flag=0               }      # clear flag if zero or one non-white-space fields is present in the current line
$1 in a { flag=1; delete a[$1] }      # set flag if 1st field is an index in the a[] array; delete entry from array to insure no more occurrences of this name ($1)
flag                                  # if flag == 1 then print current line to stdout
' file2 file1

这将产生:

:name1
./name/of/file [logfile] [ error in file coming since Day : 1 ]
./name/of/file [logfile] [ error in file coming since Day : 1 ]
:name3
./name/of/file [logfile] [ error in file coming since Day : 24 ]
./name/of/file [logfile] [ error in file coming since Day : 24 ]
:name4
./name/of/file [logfile] [ error in file coming since Day : 3 ]
./name/of/file [logfile] [ error in file coming since Day : 3 ]
./name/of/file [logfile] [ error in file coming since Day : 3 ]
./name/of/file [logfile] [ error in file coming since Day : 3 ]

如果日志文件行需要缩进(如问题中的4个空格),一个想法是:

awk '
FNR==NR { a[$1]; next }
NF<=1   { flag=0 }
$1 in a { print; flag=1; delete a[$1]; next }
flag    { printf "    %s\n",$0 }
' file2 file1

这将产生:

:name1
    ./name/of/file [logfile] [ error in file coming since Day : 1 ]
    ./name/of/file [logfile] [ error in file coming since Day : 1 ]
:name3
    ./name/of/file [logfile] [ error in file coming since Day : 24 ]
    ./name/of/file [logfile] [ error in file coming since Day : 24 ]
:name4
    ./name/of/file [logfile] [ error in file coming since Day : 3 ]
    ./name/of/file [logfile] [ error in file coming since Day : 3 ]
    ./name/of/file [logfile] [ error in file coming since Day : 3 ]
    ./name/of/file [logfile] [ error in file coming since Day : 3 ]

相关问题