List of common identifiers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting List of common identifiers
# 1  
Old 07-18-2009
List of common identifiers

Hi all,

I have 4 file and I want to find the common identifier in each file. For example:

FILE1
goat
door
bear
cat

FILE2
goat
moose
dog
cat

FILE3
goat
yak
tiger
cat

FILE4
goat
yak
lion
cat

So its obvious that goal and cat are common for all 4 files so that would get printed out into an output file.

Is there a quick UNIX command for that?

thanks
# 2  
Old 07-18-2009
Code:
$ 
$ cat file1
goat
door
bear
cat
$ cat file2
goat
moose
dog
cat
$ cat file3
goat
yak
tiger
cat
$ cat file4
goat
yak
lion
cat
$ awk '{x[$0]=x[$0]FILENAME} END{for (i in x){if (x[i]=="file1file2file3file4"){print i}}}' file1 file2 file3 file4
cat
goat
$

tyler_durden

---------- Post updated at 10:13 PM ---------- Previous update was at 10:00 PM ----------

Code:
$ 
$ sort file1 >sorted_file1; sort file2 >sorted_file2
$ sort file3 >sorted_file3; sort file4 >sorted_file4
$ join sorted_file1 sorted_file2 >common_12
$ join sorted_file3 sorted_file4 >common_34
$ join common_12 common_34
cat
goat
$

tyler_durden

---------- Post updated at 10:17 PM ---------- Previous update was at 10:13 PM ----------

Code:
$ 
$ sort file1 file2 file3 file4 | uniq -c | awk '$1==4 {print $2}'
cat
goat
$ 
$

tyler_durden
# 3  
Old 07-19-2009
Code:
cat file? | awk ' { word[$1]++ }
END {
       for (i in word) {
           print i,word[i]
           }
      }
' | sort -k 2,2nr

If you like to get top two, then add after sort
Code:
|  head -2

And if you like to save result to file, add to the end of commandline
Code:
 > result.txt

# 4  
Old 07-19-2009
This should work with variable number of input files:

(use gawk, nawk or /usr/xpg4/bin/awk on Solaris)

Code:
awk > outfile 'END { 
  for (W in w) if (w[W] == ARGC - 1) print W 
	}
{  f[$0,FILENAME]++ || w[$0]++ }
' infile1 [infile2 ... infilen]


Last edited by radoulov; 07-19-2009 at 11:57 AM..
# 5  
Old 07-20-2009
A vintage unix approach The first "cat" command is not needed but it leaves the script looking neat!

Code:
cat file1 | \
        cat - file2 | sort | uniq -d | \
        cat - file3 | sort | uniq -d | \
        cat - file4 | sort | uniq -d

cat
goat

# 6  
Old 07-20-2009
Hm,
try your script with a pattern that appears more than once in the same input file, but that is not present in all the input files.
# 7  
Old 07-20-2009
Quote:
Originally Posted by radoulov
Hm,
try your script with a pattern that appears more than once in the same input file, but that is not present in all the input files.
Thanks, that's a good point, radoulov.

This script:

Code:
sort file1 file2 file3 file4 | uniq -c | awk '$1==4 {print $2}'

would not solve the OP's problem in that case.

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Comparing two files and list the difference with common first line content of both files

I have two file as given below which shows the ACL permissions of each file. I need to compare the source file with target file and list down the difference as specified below in required output. Can someone help me on this ? Source File ************* # file: /local/test_1 # owner: own #... (4 Replies)
Discussion started by: sarathy_a35
4 Replies

2. Shell Programming and Scripting

Find common values in python list in ordered format

Hello All, There are various codes available to find the intersection between two sets in python. But my case is the following: I want to find the continual common pattern in different lists compared to list1. (i have underlined the longest common patterns in set a and set b) a = 2, 3, 5,... (1 Reply)
Discussion started by: Zam_1234
1 Replies

3. Shell Programming and Scripting

Common prefix of a list of strings

Is there a simple way to find the longest common prefix of a space-separated list of strings, optionally by field? For example, given input: "aaa_b_cc aaa_b_cc_ddd aaa_b_cc aaa_b_cd"with no field separator, output: aaa_b_cwith _ field separator, output: aaa_bI have an awk solution which... (1 Reply)
Discussion started by: CarloM
1 Replies

4. Shell Programming and Scripting

Remove part of a file based on identifiers

here below is a part of the file cat fileName.txt NAME=APP-VA-va_mediaservices-113009-VA_MS_MEDIA_SERVER_NOT_PRESENT-S FIXED=false DATE= 2013-02-19 03:46:04.4 PRIORITY=HIGH RESOURCE NAME=ccm113 NAME=APP-DS-ds_ha-140020-databaseReplicationFailure-S FIXED=false DATE= 2013-02-19... (4 Replies)
Discussion started by: vivek d r
4 Replies

5. Shell Programming and Scripting

cut a line into different fields based on identifiers

cat fileanme.txt custom1=, custom2=, userPulseId=3005, accountPolicyId=1, custom3=, custom4=, homeLocationId=0, i need to make the fields appear in next line based on identifier (,) ie comma so output should read cat fileanme.txt custom1=, custom2=, userPulseId=3005, ... (8 Replies)
Discussion started by: vivek d r
8 Replies

6. Shell Programming and Scripting

Find common entries in 2 list and write data before it

Hi all, I have 2 files: second file I want if entries in one file will match in other file. It shuld wite approve before it so output shuld be (1 Reply)
Discussion started by: manigrover
1 Replies

7. Shell Programming and Scripting

Join two files with common and range identifiers

I have a problem joining two files. The first file abc.txt has 10k lines and has lots of fields but two fields fff1 and ppp1 to merge by. The second file xyz.txt is a master file with 1k lines and lots of fields but three fields to merge by fff1; rrr1 and qqq1. The two files need to be merged... (9 Replies)
Discussion started by: cfiles2012
9 Replies

8. Shell Programming and Scripting

Print Selection of Line between two Identifiers.

I have a following containing DATA in the following format: DATA....------ --------------- -------------- DATA.....------ -------------------- ------------------ DATA....------ --------------- -------------- I want to extract the selective DATA in between identifiers and ... (4 Replies)
Discussion started by: parshant_bvcoe
4 Replies

9. UNIX for Dummies Questions & Answers

list common name files

hican anyone tell how to list all files starting with "abc" only specific month like august .thanks,Mazhar (4 Replies)
Discussion started by: mazhar99
4 Replies

10. Shell Programming and Scripting

extract lines with a given list of identifiers

Hi All, My question is if the simple but powerful shell scripts can extract data from a big data file by using a list of identifier. I used to put everything in the database and do joining, which sounds stupid but only way I knew. For example, my data file looks like, ... (3 Replies)
Discussion started by: mskcc
3 Replies
Login or Register to Ask a Question