Vasia SowSow
Vasia SowSow

Reputation: 31

bash remove the same in file

I have one issue with getting number different strings.

I have two files, for example :

file1 :

aaa1

aaa4

bbb3

ccc2

and

file2:

bbb3

ccc2

aaa4

How from this get value 1 (in this case aaa1 string reason)?

I have one query but it calculates not only different strings, them also takes into account the order of the rows.

diff file1 file2 | grep "<" | wc -l

Thanks.

Upvotes: 1

Views: 31

Answers (2)

Manuel Barbe
Manuel Barbe

Reputation: 2164

As far as I understand your requirements, sorting the files prior to the diff is a quick solution:

sort file1 > file1.sorted
sort file2 > file2.sorted
diff file1.sorted file2.sorted | egrep "[<>]" | wc -l

Upvotes: 1

anubhava
anubhava

Reputation: 785266

You can use grep -v -c with other options as this:

grep -cvwFf file2 file1
1

Options used are:

-c - get the count of matches
-v - invert matches
-w - full word match (to avoid partial matches)
-F - fixed string match
-f - Use a file for matching patterns

Upvotes: 3

Related Questions