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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With