Common records


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Common records
# 1  
Old 05-09-2012
Common records

Hi, I have the following files,

Code:
A M 2 3
B E 4 5
C I 5 6
D O 4 5


Code:
A M 3 4
B E 5 2
F U 7 9
J K 2 3

Code:
OUTPUT
A M 2 3 3 4
B E 4 5 5 2

thanks in advance,
# 2  
Old 05-09-2012
Code:
awk 'NR==FNR{a[$1"-"$2]=$0;next}$1"-"$2 in a{print a[$1"-"$2],$3,$4}' file1 file2

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 05-15-2012
Hi,

If I use the same command,

Code:
awk 'NR==FNR{a[$1"-"$2]=$0;next}$1"-"$2 in a{print a[$1"-"$2],$3,$4}' file1 file2

I am getting around 293 records. But when I do it

Code:
awk 'NR==FNR{a[$1"-"$2]=$0;next}$1"-"$2 in a{print a[$1"-"$2],$3,$4}' file2 file1

I am having around 370 records.

My file1 has 8219 records and file2 has 762 records.
# 4  
Old 05-15-2012
Post some of your actual input data instead of a mockup sample, it may be different than you expected.
# 5  
Old 05-15-2012
FILE1

Code:
0610009B14Rik	NR_037995	38	0
0610040J01Rik	NM_029554	21	0
1110012J17Rik	NM_001114098	394	0
1110017D15Rik	NM_001048005	95	0
1110032A04Rik	NM_001164210	147	0
1110059M19Rik	NM_026841	53	0
1190003J15Rik	NM_029821	40	0
1300014I06Rik	NM_025831	56	0
1300017J02Rik	NM_027918	3	0
1500009C09Rik	NR_037698	828	0
1500015O10Rik	NM_024283	366	0
1500016L03Rik	NR_038057	414	0
1600029D21Rik	NM_029639	15	0
1600029I14Rik	NR_028123	10	0
1700001C02Rik	NM_029285	24	0
1700001G11Rik	NR_038077	1	0
1700001L19Rik	NM_027035	406	0
1700003E16Rik	NM_027948	27	0
1700003M02Rik	NM_027041	2	0
1700007K13Rik	NM_027040	26	0
1700009J07Rik	NR_015547	4	0

FILE2


Code:
0610010O12Rik	NM_001081365	0	1
1300017J02Rik	NM_027918	0	17
1500015O10Rik	NM_024283	0	1
1700003G18Rik	NR_029433	0	1
1700011H14Rik	NM_025956	0	2
1700016D06Rik	NM_024271	0	3
1700047M11Rik	NR_015458	0	7
1700061J05Rik	NM_028522	0	1
1810010H24Rik	NM_001163473	0	4
2010005H15Rik	NM_029733	0	4
2010107G23Rik	NM_027251	0	23
2200002K05Rik	NM_026955	0	15
2310005G13Rik	NM_183281	0	6
2510049J12Rik	NM_001101431	0	12
2610034M16Rik	NM_027001	0	10
2610528J11Rik	NM_025572	0	6
4632428C04Rik	NR_033631	0	2
4930412F15Rik	NM_175517	0	4
4930511M11Rik	NM_029141	0	9
4930528F23Rik	NM_029197	0	9
4930555I21Rik	NM_030189	0	1
4930579C15Rik	NM_027089	0	1
4930579G22Rik	NM_026916	0	1
4931428L18Rik	NR_033445	0	4

# 6  
Old 05-15-2012
And for those two sample files my code is outputting two lines regardless of whether file1 is first or not. Can you post some sample data for which my code is not working?
# 7  
Old 05-15-2012
Quote:
Originally Posted by bartus11
And for those two sample files my code is outputting two lines regardless of whether file1 is first or not. Can you post some sample data for which my code is not working?
I am not sure which part of the input files are being read by your solution.

My files have around 8K records which is out of bound to be posted.

Thanks anyways.
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 fastq files and outputting common records

I have two files: File_1: @M04961:22:000000000-B5VGJ:1:1101:9280:7106 1:N:0:86 GGCATGAAAACATACAAACCGTCTTTCCAGAAATTGTTCCAAGTATCGGCAACAGCTTTATCAATACCATGAAAAATATCAACCACACCAGAAGCAGCAT + GGGGGGGGGGGGGGGGGCCGGGGGF,EDFFGEDFG,@DGGCGGEGGG7DCGGGF68CGFFFGGGG@CGDGFFDFEFEFF:30CGAFFDFEFF8CAF;;8F ... (3 Replies)
Discussion started by: Xterra
3 Replies

2. Shell Programming and Scripting

Compare multiple files, identify common records and combine unique values into one file

Good morning all, I have a problem that is one step beyond a standard awk compare. I would like to compare three files which have several thousand records against a fourth file. All of them have a value in each row that is identical, and one value in each of those rows which may be duplicated... (1 Reply)
Discussion started by: nashton
1 Replies

3. Shell Programming and Scripting

Two columns-Common records - 20 files

Hi Friends, I have an input file like this cat input1 x 1 y 2 z 3 a 2 b 4 c 6 d 9 cat input2 x 7 h 8 k 9 l 5 m 9 d 12 (5 Replies)
Discussion started by: jacobs.smith
5 Replies

4. UNIX for Dummies Questions & Answers

keeping last record among group of records with common fields (awk)

input: ref.1;rack.1;1 #group1 ref.1;rack.1;2 #group1 ref.1;rack.2;1 #group2 ref.2;rack.3;1 #group3 ref.2;rack.3;2 #group3 ref.2;rack.3;3 #group3 Among records from same group (i.e. with same 1st and 2nd field - separated by ";"), I would need to keep the last record... (5 Replies)
Discussion started by: beca123456
5 Replies

5. Shell Programming and Scripting

Compare two files with different number of records and output only the Extra records from file1

Hi Freinds , I have 2 files . File 1 |nag|HYd|1|Che |esw|Gun|2|hyd |pra|bhe|3|hyd |omu|hei|4|bnsj |uer|oeri|5|uery File 2 |nag|HYd|1|Che |esw|Gun|2|hyd |uer|oi|3|uery output : (9 Replies)
Discussion started by: i150371485
9 Replies

6. Shell Programming and Scripting

Common records after matching on different columns

Hi, I have the following files. cat 1.txt cat 2.txt output.txt The logic is as follows.... (10 Replies)
Discussion started by: jacobs.smith
10 Replies

7. Shell Programming and Scripting

Common records using AWK

Hi, To be honest, I am really impressed and amazed at the pace I find solutions for un-solved coding mysteries in this forum. I have a file like this input1.txt x y z 1 2 3 a b c 4 -3 7 k l m n 0 p 1 2 a b c 4 input2 x y z 9 0 -1 a b c 0 6 9 k l m 8 o p 1 2 a f x 9 Output... (9 Replies)
Discussion started by: jacobs.smith
9 Replies

8. UNIX for Dummies Questions & Answers

Grep specific records from a file of records that are separated by an empty line

Hi everyone. I am a newbie to Linux stuff. I have this kind of problem which couldn't solve alone. I have a text file with records separated by empty lines like this: ID: 20 Name: X Age: 19 ID: 21 Name: Z ID: 22 Email: xxx@yahoo.com Name: Y Age: 19 I want to grep records that... (4 Replies)
Discussion started by: Atrisa
4 Replies

9. Shell Programming and Scripting

Based on num of records in file1 need to check records in file2 to set some condns

Hi All, I have two files say file1 and file2. I want to check the number of records in file1 and if its atleast 2 (i.e., 2 or greater than 2 ) then I have to check records in file2 .If records in file2 is atleast 1 (i.e. if its not empty ) i have to set some conditions . Could you pls... (3 Replies)
Discussion started by: mavesum
3 Replies

10. Shell Programming and Scripting

Count No of Records in File without counting Header and Trailer Records

I have a flat file and need to count no of records in the file less the header and the trailer record. I would appreciate any and all asistance Thanks Hadi Lalani (2 Replies)
Discussion started by: guiguy
2 Replies
Login or Register to Ask a Question