두 파일을 비교하고 일치하는 항목을 인쇄합니다.

두 파일을 비교하고 일치하는 항목을 인쇄합니다.

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

여기서 abcfileA의 항목은 열 5입니다. FileB의 값 - 따라서 내 결과는 FileB의 26개 값 모두여야 합니다.
마찬가지로 def파일 A의 세 번째 열도 마찬가지입니다. 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개의 값으로 처리할 필요가 없습니다.

관련 정보