Comparing columns in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comparing columns in a file
# 1  
Old 01-02-2012
Comparing columns in a file

I have two files. One a small one and another one is big. The smaller one look like this:

Filename: 1.tmp
Code:
3453 0
326543 1
2321 0
3212 1

The big file looks like this:
Filename 1.res
Code:
0.3232 2321
9.2922 123
0.983 3212
8.373 326543
0.9 3453
1.098 3432

I want to extract those lines from 1.res which have common numbers in the first column of 1.tmp

My output file should be like:
Code:
0.9 3453
8.373 326543
0.3232 2321
0.983 3212

I have tried several things but none could give me the desired output.
Attempt 1:
Code:
grep -f 1.tmp 1.res

The problem with this approach is that it retrieves those values which are there in the first column in 1.res

Attempt 2:
Code:
comm 1.tmp 1.res

Attempt 3:
Code:
more 1.tmp | awk '
     NR == FNR { a[$1]=$2 }
     NR != FNR { print $2, a[$1] }
  ' 1.res

I know this code has some problems. I am using Linux with BASH.
# 2  
Old 01-02-2012
Hi shoaibjameel123,

One way using 'awk':
Code:
$ awk 'FNR == NR { tmp[$1] = 1; next } FNR < NR { if ( tmp[$2] ) { print } }' 1.tmp 1.res 
0.3232 2321
0.983 3212
8.373 326543
0.9 3453

Regards,
Birei
This User Gave Thanks to birei For This Post:
# 3  
Old 01-02-2012
join -1 1 -2 2 <(sort -n -k1 1.tmp) <(sort -n -k2 1.res)
This User Gave Thanks to tarun_agrawal For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comparing Select Columns from two CSV files in UNIX and create a third file based on comparision

Hi , I want to compare first 3 columns of File A and File B and create a new file File C which will have all rows from File B and will include rows that are present in File A and not in File B based on First 3 column comparison. Thanks in advance for your help. File A A,B,C,45,46... (2 Replies)
Discussion started by: ady_koolz
2 Replies

2. Shell Programming and Scripting

Comparing Columns and writing a new file

I have a table with one column File1.txt 1 2 3 4 5 6 7 8 9 10 Another table with two columns; This has got a subset of entries from File 1 but not unique because they have differing values in col 2. File2.txt 1 a 2 d 2 f 6 r 6 e (3 Replies)
Discussion started by: cs_novice
3 Replies

3. UNIX for Dummies Questions & Answers

Comparing the 2nd column in two different files and printing corresponding 9th columns in new file

Dear Gurus, I am very new to UNIX. I appreciate your help to manage my files. I have 16 files with equal number of columns in it. Each file has 9 columns separated by space. I need to compare the values in the second column of first file and obtain the corresponding value in the 9th column... (12 Replies)
Discussion started by: Unilearn
12 Replies

4. Shell Programming and Scripting

Comparing rows and columns

Hi, i've a .csv file with the data as below: - file1.h, 2.0 file2.c, 3.1 file1.h, 2.5 file3.c, 3.3.3 file1.h, 1.2.3 I want to remove the duplicate file names considering only the one with the highest version number.. output should be file1.h, 2.5 file2.c, 3.1 file3.c,... (3 Replies)
Discussion started by: pravsripad
3 Replies

5. UNIX Desktop Questions & Answers

COMPARING COLUMNS IN A TEXT FILE

Hi, Good day. I currently have this data called database.txt and I would like to check if there are no similar values (all unique) on an entire row considering the whole column data is unique. the data is as follows cL1 cL2 cL3 cL4 a12 c13 b13 c15 b11 a15 c19 b11 c15 c17 b13 f14 with... (1 Reply)
Discussion started by: whitecross
1 Replies

6. Shell Programming and Scripting

comparing columns of a file with another file

Hello experts, It has been just one month since I started with perl. I have the following doubt. I have two files File 1 looks like this GKHGGSS0098 PPP.100.F.LE GKHYXDF9081 KKK.100.F.LE GKHSDFT6546 JKL.100.F.LE GKHGGHJ3123 ABC.100.F.LE File 2 looks like this... (2 Replies)
Discussion started by: newbie_perl2011
2 Replies

7. Shell Programming and Scripting

Comparing Columns and printing the difference from a particular file

Gurus, I have one file which is having multiple columns and also this file is not always contain the exact columns; sometimes it contains 5 columns or 12 columns. Now, I need to find the difference from that particular file. Here is the sample file: param1 | 10 | 20 | 30 | param2 | 10 |... (6 Replies)
Discussion started by: buzzusa
6 Replies

8. Shell Programming and Scripting

Comparing two columns

Hi, I want to compare two columns and find out missing entries e:g Column 1 Column 2 1 1 2 2 3 13 4 10 19 234 Results woud be 13. I will appreciate very much if anyone help me :). (12 Replies)
Discussion started by: krabu
12 Replies

9. Shell Programming and Scripting

comparing 2 columns from 2 files

Hey, I have 2 files that have a name and then a number: File 1: dog 21 dog 24 cat 33 cat 27 dog 76 cat 65 File 2: dog 109 dog 248 cat 323 cat 207 cat 66 (2 Replies)
Discussion started by: dcfargo
2 Replies

10. Shell Programming and Scripting

Help with comparing columns from a csv file

Hi there, I have an csv file. I want to compare the 16th and 18th columns. They contain alpha numeric characters. Some are same and some are different. We have to pick the ones which are different. But with certain rules. 16th col. 18th col. ---------- ... (1 Reply)
Discussion started by: sickboy
1 Replies
Login or Register to Ask a Question