shanjib_
shanjib_

Reputation: 65

Find similar lines in two text files?

I have two sorted text files that are filled with python modules and version numbers. In this format:

Cython==0.21
DataShape==0.3.0
Flask==0.10.1
Jinja2==2.7.3
MarkupSafe==0.23
PIL==1.1.7

I want to compare and see what they have in common.

I know this will give me what they have exactly in common

comm -12 file1 file2

But I also want to know what modules they have in common, even if the version numbers are different. Is there any easy way to do this?

Upvotes: 1

Views: 525

Answers (3)

Revan
Revan

Reputation: 2322

If it does not necessary have to be a one line command, you can use sed

sed 's/=.*//g' file1 > file1.stripped
sed 's/=.*//g' file2 > file2.stripped
comm -12 file1 file2

Upvotes: 0

Taywee
Taywee

Reputation: 1335

Since you are apparently in Linux, you can sed, sort, and uniq them (this depends on bash process subsntitution, you can write the intermittent files out if you need to):

diff <(awk -F= '{ print $1 }' firstfile.txt | sort | uniq) <(awk -F= '{ print $1 }' secondfile.txt | sort | uniq)

Upvotes: 0

shanjib_
shanjib_

Reputation: 65

So I couldn't find a specific command, so I just wrote a quick python script to parse the file and give me only the parts before the "=", and then used comm to compare those new files.

Upvotes: 1

Related Questions