Shell – Compare two files and print matches

scriptingshelltext processing

Is there anyway to get this through Unix shell scripting?
I have a fileA with one column (1000 rows), and fileB with 26 columns(13000 rows).
I need to search each value of fileA with fileB and return all the 26 values from FileB if matches. The search value (from FileA) may present in any of the 26 values in FileB. This value is not fixed in any of the columns in B file.

FILEA:

abc
def
ghi

FILEB:

drm|fdm|pln|ess|abc|zeh|....|yer (26 values)
fdm|drm|def|ess|yer|zeh|....|pln

Here, abc from fileA is 5th col. of FileB—so my result should be all the 26 values from FileB.
Similarly, def from fileA is 3rd col. of FileB -so my result should be all the 26 values from FileB.

This way, need to do for the entire record set.

If unmatched, ignore the record.

Best Answer

You can just use grep:

grep -Fwf fileA fileB

From 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.
Related Question