2つのファイルを通信なしで1行ずつ比較します。 (ファイル1の順序を維持する必要があります。)

2つのファイルを通信なしで1行ずつ比較します。 (ファイル1の順序を維持する必要があります。)

ファイル1:

happy
sad
calm
palm

ファイル2:

palm
dream
calm

これら2つのファイルを比較して、2つのファイルに共通の行だけを表示したいのですが、ファイル2の順序を維持したいと思います。私の出力は次のようになります

palm
calm

ファイルの並べ替え後に通信を使用できることを知っていますが、順序を維持したいと思います。これを行う方法はありますか?

ベストアンサー1

grepを使用してください:

$ grep -Ff f1 f2
palm
calm

男 grep:

   -F, --fixed-strings
          Interpret PATTERN as a list of fixed strings (instead of regular
          expressions), separated by newlines,  any  of  which  is  to  be
          matched.
   -f FILE, --file=FILE
          Obtain patterns from FILE, one per line.  If this option is used
          multiple times or is combined with  the  -e  (--regexp)  option,
          search  for  all  patterns  given.  The empty file contains zero
          patterns, and therefore matches nothing.

おすすめ記事