Comparing column from two different files


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Comparing column from two different files
# 1  
Old 05-28-2015
Comparing column from two different files

I have two files with different size and formal and want to compare all the values of one of the column in file 1 compared with values of one of the column in file 2 and return the common values as output file.

can someone please help if the file size and format are same i am able to get output but not if the files are not same.

file 1
Code:
values  name                                  
100        A                                          
300        B                                         
200        C                   
400        D

file 2
Code:
name     values
  D           400
  B           300
  E           600 
  H           500

output (common values in file1 and file 2 based on column(values)
Code:
name     values
 B           300
 D           400


Last edited by Corona688; 05-28-2015 at 01:22 PM..
# 2  
Old 05-28-2015
Code:
 awk 'NR==FNR {T[$2,$1]; next} $1","$2 in T' SUBSEP="," file1 file2
name     values
  D           400
  B           300


Last edited by RudiC; 05-28-2015 at 01:28 PM..
# 3  
Old 05-28-2015
To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)



Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
# 4  
Old 05-28-2015
Quote:
Originally Posted by RudiC
Code:
 awk 'NR==FNR {T[$2,$1]; next} $1","$2 in T' SUBSEP="," file1 file2
name     values
  D           400
  B           300

There is no need to change SUBSEP if you use parentheses:
Code:
awk 'NR==FNR {T[$2,$1]; next} ($1,$2) in T' file1 file2


Last edited by Scrutinizer; 05-28-2015 at 03:17 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 05-28-2015
Brilliant. That I didn't try. I parenthesized every man and his dog, but that escaped me.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comparing same column from two files, printing whole row with matching values

First I'd like to apologize if I opened a thread which is already open somewhere. I did a bit of searching but could quite find what I was looking for, so I will try to explaing what I need. I'm writing a script on our server, got to a point where I have two files with results. Example: File1... (6 Replies)
Discussion started by: mitabrev83
6 Replies

2. Shell Programming and Scripting

Comparing all lines in a column with another is condition is met

Sorry for this noob question, I have file with 4 columns like where columns 2 and 4 have numbers a 55 k 3 b 59 l 3 c 79 m 277 d 255 n 277 e 257 o 267 f 267 p 287 g 290 q 287 h 290 r 287 i 310 s 900 now i want to select only those rows, where values in column 4 are greater than... (4 Replies)
Discussion started by: amits22
4 Replies

3. Shell Programming and Scripting

Joining the files with comparing the first column

Hi, I have two files in the following format. I am trying to compare the first column of both the files and if the values match the rows in file tst6 should be replaced in tst1. File tst1 S00823295|MIDDL|0|MR|019221521A||RL|STD|0|0||E S00862481|ESSEX|0|MR|018163650A||R|STD|0|0||E... (1 Reply)
Discussion started by: nua7
1 Replies

4. UNIX for Dummies Questions & Answers

Comparing two text files by a column and printing values that do not match

I have two text files where the first three columns are exactly the same. I want to compare the fourth column of the text files and if the values are different, print that row into a new output file. How do I go about doing that? File 1: 100 rs3794811 0.01 0.3434 100 rs8066551 0.01... (8 Replies)
Discussion started by: evelibertine
8 Replies

5. Shell Programming and Scripting

comparing column of two different files and print the column from in order of 2nd file

Hi friends, My file is like: Second file is : I need to print the rows present in file one, but in order present in second file....I used while read gh;do awk ' $1=="' $gh'" {print >> FILENAME"output"} ' cat listoffirstfile done < secondfile but the output I am... (14 Replies)
Discussion started by: CAch
14 Replies

6. 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

7. Shell Programming and Scripting

awk- comparing fields from the same column, finding discontinuities.

Hello, I have a file with two fields. The first field repeats itself for quite a while but the second field changes. What I want to do is to go through the first column until its value changes (and while it doesn't, verify that the second field is in a sequence from 0-15). Example input: ... (13 Replies)
Discussion started by: acsg
13 Replies

8. Shell Programming and Scripting

Comparing two files and printing 2nd column if match found

Hi guys, I'm rather new at using UNIX based systems, and when it comes to scripting etc I'm even newer. I have two files which i need to compare. file1: (some random ID's) 451245 451288 136588 784522 file2: (random ID's + e-mail assigned to ID) 123888 xc@xc.com 451245 ... (21 Replies)
Discussion started by: spirm8
21 Replies

9. Shell Programming and Scripting

Comparing each of the value in a column with the known value - in Shell

I have the column as 2.14% 1.42% 0.64% 0.50% 0.44% 0.38% 0.38% 0.36% 0.35% 0.35% 0.32% 0.32% 0.31% 0.30% 0.30% I want to find out if any of these values is greater than 75%, is yes then print that value. (2 Replies)
Discussion started by: Santosh251982
2 Replies

10. Shell Programming and Scripting

Comparing values in column 1

Hi to all, I have the following text within inputfile data1,value1,value2 data1,value3,value2 data1,value5,value6 data2,value1,value2 data2,value3,value4 data3,value1,value2 data3,value3,value4 data4,value1,value2 data4,value3,value4 data4,value5,value6 I would like to... (4 Replies)
Discussion started by: cgkmal
4 Replies
Login or Register to Ask a Question