行全体が一致するファイルを探す

行全体が一致するファイルを探す

次の内容を含むファイルがあります。

$ cat compromised_header.txt
some unique string 1
some other unique string 2
another unique string 3

上記のファイルのすべての行がまったく同じ順序であり、行の間に中間行がないすべてのファイルを見つけたいと思います。

入力ファイルの例:

$ cat a-compromised-file.txt
some unique string 1
some other unique string 2
another unique string 3
unrelated line x
unrelated line y
unrelated line z

私は以下を試しましたgrep

grep -rlf compromised_header.txt dir/

しかし、次のように一致するので、予想されるファイルを提供しているかどうかはわかりません。

some unique string 1
unrelated line x
unrelated line y
unrelated line z

ベストアンサー1

以下をサポートするawkを使用してくださいnextfile

NR == FNR {
  a[++n]=$0; next
}
$0 != a[c+1] && (--c || $0!=a[c+1]) {
  c=0; next
}
++c >= n {
  print FILENAME; c=0; nextfile
}

再帰の場合find

find dir -type f -exec gawk -f above.awk compromised_header.txt {} +

または、次のように機能することもできます。

pcregrep -rxlM "$( perl -lpe '$_=quotemeta' compromised_header.txt )" dir

pcregrep--fixed-strings--multiline

Perlがフルルックモードの場合(メモリに収まらないほど大きなファイルには適していません):

find dir -type f -exec perl -n0777E 'BEGIN {$f=<>} say $ARGV if /^\Q$f/m
' compromised_header.txt {} +

おすすめ記事