比较两个文件并打印匹配项

vam*_*shi 6 shell scripting text-processing

有没有办法通过 Unix shell 脚本来解决这个问题?我有一个包含一列(1000 行)的 fileA 和一个包含 26 列(13000 行)的 fileB。
我需要用 fileB 搜索 fileA 的每个值,如果匹配,则从 FileB 返回所有 26 个值。搜索值(来自文件 A)可能出现在文件 B 的 26 个值中的任何一个中。该值在 B 文件的任何列中都不是固定的。

文件:

abc
def
ghi
Run Code Online (Sandbox Code Playgroud)

文件B:

drm|fdm|pln|ess|abc|zeh|....|yer (26 values)
fdm|drm|def|ess|yer|zeh|....|pln
Run Code Online (Sandbox Code Playgroud)

在这里,abc来自文件 A 是第 5 列。FileB——所以我的结果应该是来自 FileB 的所有 26 个值。
同样,def从文件A 是第三列。FileB - 所以我的结果应该是来自 FileB 的所有 26 个值。

这样一来,就需要对整个记录集做。

如果不匹配,则忽略该记录。

ter*_*don 13

你可以只使用grep

grep -Fwf fileA fileB
Run Code Online (Sandbox Code Playgroud)

来自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.
Run Code Online (Sandbox Code Playgroud)