Comparing two files and creating a new file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comparing two files and creating a new file
# 1  
Old 01-25-2013
Comparing two files and creating a new file

Hi,

I want to compare two files based on the data in their first column. Both the files are not equal in their number of columns and number of entries or rows.

The entries (only the first column) of the file1 should be compared with the entries (only the first column) of the file2. If the data in the first column matches in both the files, then that data's corresponding row in the file2 should be printed fully into a new file (file3). This should be done as long as the entries in the file1 has been fully compared with the file2

file1
HTML Code:
1    0.25    0.56    0.56   0   55 
5    0.99    0.44    0.89   0   89
7    0.77    0.45    0.75   0   100
file2

HTML Code:
1    0.25    0.56    0.56   0   6.565    6.555    1.589    7.892   70
2    0.88    0.25    0.77   0   6.458    4.215    1.588    7.222   80  
5    0.99    0.44    0.89   0   7.444    5.444    7.444    9.221   90
7    0.77    0.45    0.75   0   4.225    4.256    7.555    2.222   10
8    0.14    0.44    0.78   0   2.457    4.222    8.777    1.454   20
The required output file (file3) should look like:

HTML Code:
1    0.25    0.56    0.56   0   6.565    6.555    1.589    7.892   70
5    0.99    0.44    0.89   0   7.444    5.444    7.444    9.221   90
7    0.77    0.45    0.75   0   4.225    4.256    7.555    2.222   10

Last edited by begin_shell; 01-25-2013 at 11:38 AM..
# 2  
Old 01-25-2013
Try:
Code:
awk 'NR==FNR{A[$1]; next}$1 in A' file1 file2

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 01-26-2013
Hi Scrutinizer,

Thanks for your reply. It did worked. But i would like to know what does that " A " signify in that awk statement.(just before [$1] - column 1). Could you help with that usefulness ?
# 4  
Old 01-26-2013
Sure:

Code:
awk '
  NR==FNR{         # If the first file is being read (only then are FNR and NR equal)
    A[$1]          # then reate an (associative) array element with index "$1" (the first field) and zero content
    next           # and proceed to the next record (line)
  }
  $1 in A          # ( while reading the second file) if the first field ($1) of a recrod is present as an index in array A then print that record ( to print a record is the default action, so {print $0} can be left out)
' file1 file2      # first read file1 and then file2

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Writing a script on creating and comparing users

Hello Everybody i am a newbie to the bash scripting.please can someone help me out here.i want to write a script on creating 10 users with passwords and comparing the users to the ones in /etc/passwd file.am i gonna use arrays in creating the 10 users or what?and again, how do i compare both... (1 Reply)
Discussion started by: bruno_brunt
1 Replies

2. Shell Programming and Scripting

Comparing two files with numbers and taking difference in third file

Hi All, I have two files in the following format, with numbers being defined under columns(described by a set of headers) and rows(again defined by a set of identifiers) 2013 2013 Make200 Make201 Merc BMW Jpur Del ... (9 Replies)
Discussion started by: dev.devil.1983
9 Replies

3. Shell Programming and Scripting

comparing 2 files and creating third file with uncommon content

I want to compare 2 files and create third file with uncommon content. e.g. file1 ajay suhas tom nisha vijay mahish file2 ajay suhas tom nisha expected output file content vijay mahish Is it possible in single command ? Thanks, Ajay (6 Replies)
Discussion started by: ajaypatil_am
6 Replies

4. UNIX for Dummies Questions & Answers

Comparing a batch of files to a test file

Hi I am writing a script to run a loop through a directory and run a diff on each file against my test file. I then want to time how long each file to process (not sure how time works), as well as how long all the files took as a whole to process. Here is my code #!/bin/bash #Old... (1 Reply)
Discussion started by: ladyAnne
1 Replies

5. Shell Programming and Scripting

Comparing 2 files with awk and updating 2nd file

file1: (unique files) 1 /pub/atomicbk/catalog/catalog.gif 693 2 /pub/atomicbk/catalog/home.gif 813 3 /pub/atomicbk/catalog/logo2.gif 12871 4 /pub/atomicbk/catalog/sleazbk.html 18338 file2: (duplicate filenames allowed) 28/Aug/1995:00:00:38 1 /pub/atomicbk/catalog/home.gif 813... (2 Replies)
Discussion started by: jontjioe
2 Replies

6. Shell Programming and Scripting

Comparing files columnwise and print the differences in third file

Hello Everybody!!!!!!!!! Request you to help me with the below mentioned issue: I have 2 files say, File 1: a|4|7 b|3|2 c|8|8 d|8|9 File 2: a|4|6 b|2|2 c|8|8 d|9|8 The third file(output file) should have: Data mismatch in row 1 column 3 Data mismatch in row 2 coumn 2 Data... (3 Replies)
Discussion started by: abhijeet1409
3 Replies

7. Shell Programming and Scripting

Comparing 2 files and return the unique lines in first file

Hi, I have 2 files file1 ******** 01-05-09|java.xls| 02-05-08|c.txt| 08-01-09|perl.txt| 01-01-09|oracle.txt| ******** file2 ******** 01-02-09|windows.xls| 02-05-08|c.txt| 01-05-09|java.xls| 08-02-09|perl.txt| 01-01-09|oracle.txt| ******** (8 Replies)
Discussion started by: shekhar_v4
8 Replies

8. Shell Programming and Scripting

How can i prepare a file by comparing two other files?

File 1 data: TestA TestB TestC File 2 data: TestA TestD TestE My Output File (pick all from both and create a file without duplicate data) ---------------------------------------------------------------------- TestA TestB TestC (3 Replies)
Discussion started by: manmohanpv
3 Replies

9. Shell Programming and Scripting

comparing files to contents of a file

Hi I have a problem trying to run a while statement. I have files under one directory that i need to compare to a value in filex and update that file with the result files in the directory are DFC1. DFC5. DFC345. DFC344. DFC9. The program i am trying to run will take the number... (3 Replies)
Discussion started by: SummitElse
3 Replies

10. Shell Programming and Scripting

shell script comparing files in a file

There is a text file that contains the data in the following format: COLUMN1 COLUMN2 ABC 1 ABC 2 ABC 3 DEF 4 DEF 5 XYZ 7 We have to create a second text file... (4 Replies)
Discussion started by: raina_nalin
4 Replies
Login or Register to Ask a Question