Finding conserved pattern in different files

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 :slight_smile:

what exactly is "conserved" pattern?

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

Here's an additional alternative...

awk '{a[$1]++}END{for(i in a)if(a==3)print i}' file1 file2 file3

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

Try...

awk '{a[$1]++}END{for(i in a)if(a==ARGC-1)print i}' file*

Thanks..The script is working fine....:wink: