Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unique elements in each file

I have 4 files and would like to know elements which are non overlapping (per file) compared to the elements in other files.

File A

Vincy
ruby
rome

File B

Vincy
rome
Peter

File C

Vincy
Paul
alex

File D

Vincy
rocky
Willy

Any suggestion for one liner in perl, python, shell, bash. The expected output is:

File A: ruby, File B: Peter, File C: Paul, Alex File D: rocky, Willy.

like image 729
Angelo Avatar asked Sep 19 '26 16:09

Angelo


2 Answers

Edit after question clarified: Unique elements across all files, and the file in which it occurs:

cat File_A File_B File_C File_D |sort | uniq -u | while read line ; do file=`grep -l $line File*` ; echo "$file $line" ; done

Edit:

perly way of doing it, will be faster if the files are large:

#!/usr/bin/perl

use strict;
use autodie;

my $wordHash ;

foreach my $arg(@ARGV){
    open(my $fh, "<", $arg);
    while(<$fh>){
        chomp;
        $wordHash->{$_}->[0] ++;
        push(@{$wordHash->{$_}->[1]}, $arg);
    }
}

for my $word ( keys %$wordHash ){
    if($wordHash->{$word}->[0] eq 1){
        print $wordHash->{$_}->[1]->[0] . ": $word\n"
    }
}

execute as: myscript.pl filea fileb filec ... filezz

stuff from before clarification: Easy enough with shell commands. Non repeating elements across all files

cat File_A File_B File_C File_D |sort | uniq -u

Unique elements across all files

cat File_A File_B File_C File_D |sort | uniq

Unique elements per file (edit thanks to @Dennis Williamson)

for line in File* ; do echo "working on $line" ; sort $line | uniq ; done
like image 119
beresfordt Avatar answered Sep 21 '26 04:09

beresfordt


Here is a quick python script that will do what you ask over an arbitrary number of files:

from sys import argv
from collections import defaultdict

filenames = argv[1:]
X = defaultdict(list)
for f in filenames:
    with open(f,'r') as FIN:
        for word in FIN:
            X[word.strip()].append(f)

for word in X:
    if len(X[word])==1:
        print "Filename: %s word: %s" % (X[word][0], word)

This gives:

Filename: D word: Willy
Filename: C word: alex
Filename: D word: rocky
Filename: C word: Paul
Filename: B word: Peter
Filename: A word: ruby
like image 38
Hooked Avatar answered Sep 21 '26 06:09

Hooked



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!