Output the common lines (similarities) of two text files (the opposite of diff)?
How about using diff, even though you don't want a diff? Try this:
diff --unchanged-group-format='@@ %dn,%df
%<' --old-group-format='' --new-group-format='' \
--changed-group-format='' a.txt b.txt
Here is what I get with your sample data:
$ cat a.txt
Foo Bar
X
Hello
World
42
$ cat b.txt
Foo Baz
Hello
World
23
$ diff --unchanged-group-format='@@ %dn,%df
%<' --old-group-format='' --new-group-format='' \
--changed-group-format='' a.txt b.txt
@@ 2,3
Hello
World
grep -Fxf file1 file2
-F
means match plain strings (not regexps), -x
means only whole-line matches, -f
means take 'patterns' (i.e. lines) from the file named as its argument
comm
can be used. man comm
for all the options but you'll want to use comm -12 ...
to show only lines that exist in both inputs.
As people have pointed out, you need to pass your input through sort
first.