shampa
shampa

Reputation: 1390

sorting 2 files and applying a command on them

I want to find non-unique lines from 2 unsorted files(say file1 and file2). I have to run 3 commands for it.

sort file1 > file1_sort
sort file2 > file2_sort
comm -3 file1_sort file2_sort

Can I do it without making temporary files and in a single command?

Thanks,

Upvotes: 2

Views: 89

Answers (4)

Kent
Kent

Reputation: 195039

you don't have to sort at all.

awk 'NR==FNR{a[$0]=1}NR>FNR{if($0 in a)print $0}' file1 file2 > result.file

Upvotes: 0

camh
camh

Reputation: 42438

Your description does not match the commands you have. You say you want non-unique lines, but have comm -3 which removes the non-unique lines.

sort file1 file2 | uniq -d
sort file1 file2 | uniq -u

The first line gives you the non-unique lines (i.e. those that are in both file1 and file2). The second line gives you the unique lines (i.e. those that appear only in file1 or file2 but not both).

Upvotes: 1

ztank1013
ztank1013

Reputation: 7255

If you are interested in non unique lines try this:

cat file1 file2 | sort | uniq -c | sort -n

Upvotes: 0

Prince John Wesley
Prince John Wesley

Reputation: 63688

comm -3 <(sort file1) <(sort file2)

Upvotes: 4

Related Questions