anjas
May 7, 2009, 9:49am
1
Hi power user,
For examples, I have three different files:
file 1: file2: file 3:
AAA CCC ZZZ
BBB BBB CCC
CCC DDD DDD
DDD TTT AAA
EEE AAA XXX
I want to compare those three and want the output to be the conserved pattern of those three files, which is
AAA or CCC
CCC DDD
DDD AAA
How could I do that by using awk, grep, or perl? Tx
what exactly is "conserved" pattern?
anjas
May 7, 2009, 10:46am
3
conserved pattern is the same pattern in those three files...Maybe I'm going to make it clearer now
I have tree files:
file 1:
AAA
BBB
CCC
DDD
file2:
CCC
BBB
DDD
TTT
AAA
file 3:
ZZZ
CCC
DDD
AAA
XXX
I want to compare those three and want the output to be the same pattern of those three files, which is
AAA
CCC
DDD
or
CCC
DDD
AAA
if you have Python, here's an alternative
file1=set(open("file1").read().split())
file2=set(open("file2").read().split())
file3=set(open("file3").read().split())
intersection = file1 & file2 & file3
for items in intersection:
print items
output:
# ./test.py
AAA
CCC
DDD
Ygor
May 7, 2009, 10:31pm
5
Here's an additional alternative...
awk '{a[$1]++}END{for(i in a)if(a==3)print i}' file1 file2 file3
anjas
May 11, 2009, 4:14am
6
Tx for the reply..I have tried all of them, and they work..:). However, I have one question remain...If the total amount of file is 100 or more (file1, file2,.....file100, file101, filen) , Is there any other way to automate the script? I don't know wether I have to start a new thread or not for this.
if you have that many files, then use a loop
Ygor
May 11, 2009, 10:39pm
8
Try...
awk '{a[$1]++}END{for(i in a)if(a==ARGC-1)print i}' file*
anjas
May 12, 2009, 5:15am
9
Thanks..The script is working fine....