文件一:
happy
sad
calm
palm
文件2:
palm
dream
calm
我想比较这两个文件并仅显示这两个文件中共有的行,但我想保持文件 2 的顺序。我的输出应该是:
palm
calm
我知道我可以在对文件进行排序后使用 comm,但我想保持顺序。有什么办法可以做到这一点吗?
答案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.