Visit Our UNIX and Linux User Community


Compare 2 files and match column data and align data from 3 column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare 2 files and match column data and align data from 3 column
# 1  
Old 10-16-2012
Compare 2 files and match column data and align data from 3 column

Hello experts,

Please help me in achieving this in an easier way possible. I have 2 csv files with following data:
File1

Code:
08/23/2012 12:35:47,JOB_5330
08/23/2012 12:35:47,JOB_5330
08/23/2012 12:36:09,JOB_5340
08/23/2012 12:36:14,JOB_5340
08/23/2012 12:36:22,JOB_5350
08/23/2012 12:36:26,JOB_5350

File2

Code:
JOB_5340,121648
JOB_5350,121648
JOB_5360,243215

Output Required
============
Code:
08/23/2012 12:35:47,JOB_5330	
08/23/2012 12:35:47,JOB_5330	
08/23/2012 12:36:09,JOB_5340,JOB_5340, 121648
08/23/2012 12:36:14,JOB_5340	
08/23/2012 12:36:22,JOB_5350,JOB_5350,121648
08/23/2012 12:36:26,JOB_5350

I have tried to construct this code, and it doesn't produces the exact match on the 4th column. Here is the code and the output...

Code:
 awk -F, 'NR==FNR { a[$2]=$0; next  }
>            a[$3] { print $0, a[$3]}
>         ' Aug_23.log.Data_load.csv Aug_23.log.begin_end_times.csv
08/23/2012 12:35:47,JOB_5330 JOB_5490 24
08/23/2012 12:35:47,JOB_5330 JOB_5490 24
08/23/2012 12:36:09,JOB_5340 JOB_5490 24
08/23/2012 12:36:14,JOB_5340 JOB_5490 24
08/23/2012 12:36:22,JOB_5350 JOB_5490 24
08/23/2012 12:36:26,JOB_5350 JOB_5490 24
08/23/2012 13:08:51,JOB_5360 JOB_5490 24
08/23/2012 13:08:58,JOB_5360 JOB_5490 24

# 2  
Old 10-16-2012
Hi

Code:
awk -F, 'NR==FNR{a[$1]=$2;next}{if ($2 in a){$3=$2;$4=a[$2];delete a[$2];}}1' OFS=, file2 file1

Guru.
# 3  
Old 10-16-2012
OR..

Code:
awk -F, 'FNR==NR{a[$1]=$0;next}{print $0,a[$NF];delete a[$NF]}' OFS="," file2 file1

# 4  
Old 10-16-2012
Hello asnandhakumar,

One more option for you.
Code:
awk -F, 'FNR==NR {x[$1]=$0;next} z=($2 in x)?$0","x[$2]:$0 {print z; x[$2]=""}' file2 file1

Regards
This User Gave Thanks to cgkmal For This Post:
# 5  
Old 10-16-2012
hi Guru,

Thanks for your quick response. Appreciate it.

The given command doesn't produce the required output. Actually, I'm looking at the Job_id in the 2nd column (File1) should match with the Job_id in File(2) and show in the output, as referred in my output section.

Please help me its very urgent

thanks,
NK
# 6  
Old 10-16-2012
try
Code:
awk  'NR==FNR {Ar[$1] = $0; next}
      $2 in Ar {$(NF+1)=Ar[$2]
                delete Ar[$2]
               }
      1
     ' OFS="," FS="," file2 file1


Last edited by RudiC; 10-16-2012 at 11:05 AM..
This User Gave Thanks to RudiC For This Post:

Previous Thread | Next Thread
Test Your Knowledge in Computers #414
Difficulty: Easy
JavaScript is a high-level, interpreted scripting language that conforms to the ECMAScript specification.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Change data in one column with data from another file's column

Hello, I have this file outputData: # cat /tmp/outputData __Capacity^6^NBSC01_Licences^L3_functionality_for_ESB_switch __Capacity^2100^NBSC01_Licences^Gb_over_IP __Capacity^1837^NBSC01_Licences^EDGE_BSS_Fnc __Capacity^1816^NBSC01_Licences^GPRS_CS3_and_CS4... (1 Reply)
Discussion started by: nypreH
1 Replies

2. UNIX for Dummies Questions & Answers

Compare data - Match first column and compare second

Hi guys, looking for some help with a way to compare data in two files but with some conditions. example, File 1 consists of site1,10.1.1.1 site2,20.2.2.2 site3,30.3.3.3 File 2 contains site1,l0.1.1.1 site2,50.1.1.1 site3,30.3.3.3 site4,40.1.1.1 I want to be able to match the... (1 Reply)
Discussion started by: mutley2202
1 Replies

3. Shell Programming and Scripting

Compare 2 files of csv file and match column data and create a new csv file of them

Hi, I am newbie in shell script. I need your help to solve my problem. Firstly, I have 2 files of csv and i want to compare of the contents then the output will be written in a new csv file. File1: SourceFile,DateTimeOriginal /home/intannf/foto/IMG_0713.JPG,2015:02:17 11:14:07... (8 Replies)
Discussion started by: refrain
8 Replies

4. Shell Programming and Scripting

[Solved] Compare column data in all the rows

Hi.. In the below sorted input file.. I am comparing the first 3 columns of data one by one row and it is a pipeline delimitter file.. AA|BB|CC|line1 AA|BB|CC|ine4 AA|BB|CC|line2 BB|CC|DD|line3 BB|CC|DD|line5 If first 3 columns of data matches with any record in the file the... (4 Replies)
Discussion started by: NareshN
4 Replies

5. Shell Programming and Scripting

Match words and fetch data in front of it in second column

Hi all, I have 2 files one file contain data like this in one column AST3 GSTY4 JST3 second file containign data like this in 2 columns AST3(PAXXX),GSTY4(PAXXY) it is used in diabetes KST4 it is used in blood... (6 Replies)
Discussion started by: manigrover
6 Replies

6. Shell Programming and Scripting

Match same file column data

File A B07 U51C 4434 L662C 4412 B07 L64U 612 L651B 4434 B07 L11C 4434 R151B 4434 B05 L12Z 612 L51B 4434 B01 651Z 612 L651C 4434 B04 A51Z 612 L51A 4434 L07 B08D 4434 B1B 4434 B07 RU8D 4434 L51A 4434 B07 L58D 4434 B51C 4434 B07 LA8D 4434 L4B 4434 Now i want File B Output B07... (2 Replies)
Discussion started by: asavaliya
2 Replies

7. UNIX for Dummies Questions & Answers

How to match 2 columns where one column has data as a range - extended

Dear all, there is a nice solution for a text merge where the second file has only variables with a numeric range ( sorry, cannot post URL + thread is closed ). The real world is however more complicated than in the earlier example. file1 A 1 A 2 A 3 B 1 B 2 B 3 B 4 C 1 C 2 C 3 C... (4 Replies)
Discussion started by: underscore
4 Replies

8. Ubuntu

How to compare two columns and fetch the common data with additional column

Dear All, I am new to this forum and please ignore my little knowledge :p I have two types of data (a subset is given below) data version 1: 439798 2 1 451209 1 2 508696 2 1 555760 2 1 582757 1 2 582889 1 2 691827... (2 Replies)
Discussion started by: evoll
2 Replies

9. UNIX for Dummies Questions & Answers

How to match 2 columns where one column has data as a range

Hi, I have a query about joining files using data ranges. Example files below - I want to join file1 to file2 with matches where file1 column 1 is equal to file2 column1, and file1 column 2 is within the range of file2 columns 3 and 4. I would like rows which don't match to be printed too. ... (4 Replies)
Discussion started by: auburn
4 Replies

10. Shell Programming and Scripting

Extract data based on match against one column data from a long list data

My input file: data_5 Ali 422 2.00E-45 102/253 140/253 24 data_3 Abu 202 60.00E-45 12/23 140/23 28 data_1 Ahmad 256 7.00E-45 120/235 140/235 22 data_4 Aman 365 8.00E-45 15/65 140/65 20 data_10 Jones 869 9.00E-45 65/253 140/253 18... (12 Replies)
Discussion started by: patrick87
12 Replies

Featured Tech Videos