H
H
HexUserHex2020-10-21 00:29:21
bash
HexUserHex, 2020-10-21 00:29:21

Print in bash only duplicate lines from files?

Hello, I
need to output from two files only the lines that are in both files

. An example of the contents of the files:

cat 1.txt 
11
22
33
33

cat 2.txt 
33
11
44


Here's how I try, as a result I get only 33, the directory contains only two .txt files:
cat *.txt | uniq -d
33

tell me what I missed ....

Answer the question

In order to leave comments, you need to log in

3 answer(s)
L
Lesha, 2020-10-21
@HexUserHex

tell me what i missed

sort?
$ cat *.txt | uniq -d
33
$ cat *.txt | sort | uniq -d
11
33

Can I have some morecomm -12 <(sort 1.txt) <(sort 2.txt)

S
Saboteur, 2020-10-21
@saboteur_kiev

sort <(sort -u 1.txt) <(sort -u 2.txt) |uniq -d

S
Sergey Pankov, 2020-10-21
@trapwalker

I would also suggest a solution like this: But this is more of a python solution, and py is not shipped out of the box in the standard set. Also, this solution is not efficient for large files.
py "set(open('1'))&set(open('2'))"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question