
Unixシェルスクリプトでこれを得る方法はありますか? 1列(1000行)のfileAと26列(13000行)のfileBがあります。
fileBを使用してfileAの各値を取得し、一致する場合は、FileBの26個の値をすべて返す必要があります。ファイルAの検索値は、ファイルBの26個の値のいずれかに表示できます。 Bファイルのどの列にも値が固定されていません。
文書:
abc
def
ghi
ファイルB:
drm|fdm|pln|ess|abc|zeh|....|yer (26 values)
fdm|drm|def|ess|yer|zeh|....|pln
ここで、abc
fileAのエントリは列5です。 FileBの値 - したがって、私の結果はFileBの26の値すべてでなければなりません。
同様にdef
、ファイルAの3番目の列も同じです。 FileBの値 - したがって、私の結果はFileBの26の値すべてでなければなりません。
この方法でレコードセット全体を操作する必要があります。
一致するものがない場合、レコードは無視されます。
答え1
以下を使用できますgrep
。
grep -Fwf fileA fileB
からman grep
:
-F, --fixed-strings
Interpret PATTERN as a list of fixed strings, separated by
newlines, any of which is to be matched. (-F is specified by
POSIX.)
-f FILE, --file=FILE
Obtain patterns from FILE, one per line. The empty file
contains zero patterns, and therefore matches nothing. (-f is
specified by POSIX.)
-w, --word-regexp
Select only those lines containing matches that form whole
words. The test is that the matching substring must either be
at the beginning of the line, or preceded by a non-word
constituent character. Similarly, it must be either at the end
of the line or followed by a non-word constituent character.
Word-constituent characters are letters, digits, and the
underscore.
答え2
fileAの順序は重要ですか?このパターンを使用すると、fileBに複数の行がある可能性がありますか?たとえば、以下はfileAを解析し、fileBのすべてのパターンを検索します。
while read i; do grep "$i" fileB; done < fileA
ただし、より効果的な解決策を得るには、問題をよりよく定義する必要があります。たとえば、行全体を取得するだけで十分なので、26個の値で処理する必要はありません。