Home Man
Search
Today's Posts
Register

This forum is closed for new posts. Please post beginner questions to learn unix and learn linux in the UNIX for Beginners Questions & Answers forum.

Extract common data out of multiple files

Tags
awk, beginners, cut, grep, perl, python

 

 
Thread Tools Search this Thread
# 1  
Old 12-24-2012
Extract common data out of multiple files

I am trying to extract common list of Organisms from different files
For example I took 3 files and showed expected result. In real I have more than 1000 files. I am aware about the useful use of awk and grep but unaware in depth so need guidance regarding it.

I want to use awk/ grep/ cut/ perl/ python to get the needful result.
File A:
Pseudomonas stutzeri A1501
Pseudomonas fragi A22
Pseudomonas fluorescens A506
Aeromonas caviae Ae398
Rickettsiella grylli
Aeromonas veronii AMC34
File B:
Rickettsiella grylli
Pseudomonas fulva 12-X
Pseudomonas extremaustralis 14-3 substr. 14-3b
Aeromonas caviae Ae398
Gallaecimonas xiamenensis 3-C-1
Pseudomonas stutzeri A1501
File C:
Pseudomonas extremaustralis
Pseudomonas fulva 12-X
Pseudomonas extremaustralis 14-3 substr. 14-3b
Aeromonas caviae Ae398
Rickettsiella grylli
Pseudomonas stutzeri A1501
Expected Result file : Common organism
Aeromonas caviae Ae398
Pseudomonas stutzeri A1501
Rickettsiella grylli
Hoping for your suggestions and support.
Thank you in advance
# 2  
Old 12-24-2012
If there is maximum of 1 entry per file:
Code:
awk '++A[$0]>=ARGC-1' file*

This would then be a bit more robust:
Code:
awk '{$1=$1} ++A[$0]>=ARGC-1' file*

But 1000 files is probably going to be too many for the command line length.

Otherwise try:
Code:
( 
  set -- file*
  for f
  do
    cat "$f"
  done | awk '{$1=$1} ++A[$0]>=c' c=$# 
)


Last edited by Scrutinizer; 12-24-2012 at 07:14 AM..
The Following User Says Thank You to Scrutinizer For This Useful Post:
dovah (11-15-2015)
# 3  
Old 12-24-2012
Try this
Code:
$ cat file?|sort|uniq -c|sort -rnb|grep "^ *3"| cut -d" " -f8-30
Rickettsiella grylli
Pseudomonas stutzeri A1501
Aeromonas caviae Ae398

# 4  
Old 12-25-2012
A simple while loop will do the work for you

Code:
file_count=$(ls -lrt file? |wc -l)
sort -u file1 > temp;cat temp > file1;rm temp
while read i
do
result_count=$(grep -lw "$i" file? | wc -l)
if [ $result_count -eq $file_count ]; then
 echo $i
fi
done < file1


Last edited by sathyaonnuix; 12-31-2012 at 01:00 AM..
# 5  
Old 12-26-2012
Hi Sathyaonnuix,

The solution shared by you is very impressive..... but in case if file1 has same lines multiple times (which is common to other files as well) then it will result in multiple occurrence of that line in output.
Maybe we can use sort and unique to overcome this little problem somewhat like:
Code:
cat file1|sort|uniq > tmp.tmp

and then apply while loop on this tmp. file
# 6  
Old 12-27-2012
Hello Mukul,
Thanks for your feedback. When grep -l command is used, it suppresses the repetition.

Code:
# cat file
repeat
repeat
repeat
123
456
567

Code:
# grep -lw repeat file
file

# 7  
Old 12-30-2012
Hi sathyaonnuix,
Consider the below scenario :
Code:
cat filea
line1
line2
repeat1
line3
repeat2
line4
repeat1

Code:
cat fileb
1234567
repeat1
repeat2
bbbbbb

Code:
cat filec
line1
repeat1
repeat2
line2
repeat1
line3

Now executing the script for these three files would result in below output
Code:
repeat1
repeat2
repeat1

repetition of repeat1 which i think was not required

P.S. I am running while loop on filea
 

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Merge multiple files with common header msarguru Shell Programming and Scripting 2 09-26-2014 06:52 AM
Extract data in tabular format from multiple files belalr Shell Programming and Scripting 2 04-25-2013 04:47 AM
Want to change common line from multiple files kaleem.adil UNIX for Dummies Questions & Answers 3 04-09-2013 02:04 PM
Compare multiple files, and extract items that are common to ALL files only castrojc Shell Programming and Scripting 7 01-17-2013 04:59 PM
Find common lines between multiple files bibb Shell Programming and Scripting 5 01-08-2013 02:13 PM
Parsing common values across multiple files Lucky Ali Shell Programming and Scripting 6 06-09-2012 02:14 PM
Extract common words from two/more csv files nick2011 Shell Programming and Scripting 1 03-06-2012 06:06 AM
Using AWK: Extract data from multiple files and output to multiple new files Liverpaul09 UNIX for Dummies Questions & Answers 3 10-12-2010 03:59 AM
AWK, extract data from multiple files Liverpaul09 UNIX for Dummies Questions & Answers 8 09-29-2010 08:43 AM
Get common lines from multiple files genehunter Shell Programming and Scripting 9 09-02-2010 03:47 AM


All times are GMT -4. The time now is 08:12 AM.

Unix & Linux Forums Content Copyrightę1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password