Finding conserved pattern in different files


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding conserved pattern in different files
# 1  
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 Smilie
# 2  
what exactly is "conserved" pattern?
# 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
# 4  
if you have Python, here's an alternative
Code:
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:
Code:
# ./test.py
AAA
CCC
DDD

# 5  
Here's an additional alternative...
Code:
awk '{a[$1]++}END{for(i in a)if(a[i]==3)print i}' file1 file2 file3

# 6  
Tx for the reply..I have tried all of them, and they work..Smilie. 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.
# 7  
if you have that many files, then use a loop
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #989
Difficulty: Medium
OpenBSD includes no third-party components.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[sed] Finding and sticking the pattern to the beginning of successive lines up to the next pattern

I have a file like below. 2018.07.01, Sunday 09:27 some text 123456789 0 21 0.06 0.07 0.00 2018.07.02, Monday 09:31 some text 123456789 1 41 0.26 0.32 0.00 09:39 some text 456789012 1 0.07 0.09 0.09 09:45 some text 932469494 1 55 0.29 0.36 0.00 16:49 some text 123456789 0 48 0.12 0.15 0.00... (9 Replies)
Discussion started by: father_7
9 Replies

2. Shell Programming and Scripting

Finding all files based on pattern

Hi All, I need to find all files in a directory which are containing specific pattern. Thing is that file name should not consider if pattern is only in commented area. all contents which are under /* */ are commented all lines which are starting with -- or if -- is a part of some sentence... (13 Replies)
Discussion started by: Lakshman_Gupta
13 Replies

3. UNIX for Dummies Questions & Answers

Finding the same pattern in three consecutive lines in several files in a directory

I know how to search for a pattern/regular expression in many files that I have in a directory. For example, by doing this: grep -Ril "News/U.S." . I can find which files contain the pattern "News/U.S." in a directory. I am unable to accomplish about how to extend this code so that it can... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

4. Shell Programming and Scripting

Finding the pattern and replacing the pattern inside the file

i have little challenge, help me out.i have a file where i have a value declared and and i have to replace the value when called. for example i have the value for abc and ccc. now i have to substitute the value of value abc and ccc in the place of them. Input File: go to &abc=ddd; if... (16 Replies)
Discussion started by: saaisiva
16 Replies

5. Shell Programming and Scripting

Finding log files that match number pattern

I have logs files which are generated each day depending on how many processes are running. Some days it could spin up 30 processes. Other days it could spin up 50. The log files all have the same pattern with the number being the different factor. e.g. LOG_FILE_1.log LOG_FILE_2.log etc etc ... (2 Replies)
Discussion started by: atelford
2 Replies

6. Shell Programming and Scripting

Finding 4 current files having specific File Name pattern

Hi All, I am trying to find 4 latest files inside one folder having following File Name pattern and store them into 4 different variables and then use for processing in my shell script. File name is fixed length. 1) Each file starts with = ABCJmdmfbsjop letters + 7 Digit Number... (6 Replies)
Discussion started by: lancesunny
6 Replies

7. UNIX for Dummies Questions & Answers

finding all files that do not match a certain pattern

I hope I'm asking this the right way -- I've been sending out a lot of resumes and some of them I saw on Craigslist -- so I named the file as 'Craigslist -- (filename)'. Well I noticed that at least one of the files was misspelled as 'Craigslit.' I want to eventually try to write a shell... (5 Replies)
Discussion started by: Straitsfan
5 Replies

8. Shell Programming and Scripting

Finding Last occurance of another pattern when a pattern is found.

Hi, I have two files viz, rak1: $ cat rak1 rak2: $ cat rak2 sdiff rak1 rak2 returns: I want the lines that got modified, changed, or deleted preceding with the section they are in. I have done this so far: (1 Reply)
Discussion started by: rakeshou
1 Replies

9. Shell Programming and Scripting

finding duplicate files by size and finding pattern matching and its count

Hi, I have a challenging task,in which i have to find the duplicate files by its name and size,then i need to take anyone of the file.Then i need to open the file and find for more than one pattern and count of that pattern. Note:These are the samples of two files,but i can have more... (2 Replies)
Discussion started by: jerome Sukumar
2 Replies

10. Shell Programming and Scripting

Finding a specific pattern from thousands of files ????

Hi All, I want to find a specific pattern from approximately 400000 files on solaris platform. Its very heavy for me to grep that pattern to each file individually. Can anybody suggest me some way to search for specific pattern (alpha numeric) from these forty thousand files. Please note that... (6 Replies)
Discussion started by: aarora_98
6 Replies

Featured Tech Videos