Match and Merge two file

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Match and Merge two file
# 1  
Old 05-10-2017
Match and Merge two file

Hi All,

I have two file . I need to merge both file based on a match.

File 1:

Code:
Column1 
column2
column3
column4

File 2:
Code:
column3
column5

I need to combine the two file based on match , Which in my case is column3 and combine the file as below

Combined file

Code:
Column1 
column2
column3
column4
column5

I may have non match record on column3 between file1 and file1. I need to ignore those and combine only matched record.

Any help please

thanks in Advance

Last edited by Don Cragun; 05-10-2017 at 11:42 PM.. Reason: Change QUOTE tags to CODE tags, and add ICODE tags.
# 2  
Old 05-10-2017
I have read through post #1 in this thread several times and still have no idea what you are trying to do.

You have one line between your two input files that match (the line in each file that contains the text column3). And, it appears that when you find that those lines do match, you ignore that fact completely. Then you say:
Quote:
I may have non match record on column3 between file1 and file1.
but there is only one column in file1 and what is a non match between a single file and itself?

If you are trying to produce a sorted list of the lines in two files and remove duplicates, try:
Code:
sort -u file1 file2

That produces the output you said you want, but it doesn't fit the description of the problem you presented.
# 3  
Old 05-11-2017
Hi,
If I have understand your post, you may try with this:

Code:
sort -m file1 file2 > combined_file

It is the merge option of the sort command. It gives the desired output for the particular case indicated.

Regards!
# 4  
Old 05-11-2017
Quote:
Originally Posted by AbelLuis
Hi,
If I have understand your post, you may try with this:

Code:
sort -m file1 file2 > combined_file

It is the merge option of the sort command. It gives the desired output for the particular case indicated.

Regards!
Not quite. That does not give the output requested in post #1; the merge option to sort does not remove duplicates.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 05-11-2017
Quote:
Originally Posted by Don Cragun
Not quite. That does not give the output requested in post #1; the merge option to sort does not remove duplicates.
Yes, I agree. The command must be

Code:
sort -mu file1 file2 > combined_file

I wonder if always is it the same that

Code:
sort -u file1 file2 > combined_file

Regards.

Post Data:

Perhaps the difference lies in performance. While sort -mu only combines the files already ordered, sort -u reorders the union of all, so which it does extra work.

Last edited by AbelLuis; 05-11-2017 at 12:46 AM..
# 6  
Old 05-11-2017
Quote:
Originally Posted by AbelLuis
Yes, I agree. The command must be

Code:
sort -mu file1 file2 > combined_file

I wonder if always is it the same that

Code:
sort -u file1 file2 > combined_file

Regards.
File file1 is in sorted order and file2 is in sorted order, then, and only then, the output from the commands:
Code:
sort -mu file1 file2 > combined_file
sort -u file1 file2 > combined_file

will be identical, but the 1st will run faster. If both input files are not in sorted order; the 1st command may produce unsorted output, may contain duplicated lines, and/or may terminate with a diagnostic message instead of producing the desired output.
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 05-11-2017
Quote:
Originally Posted by Don Cragun
If both input files are not in sorted order; the 1st command may produce unsorted output, may contain duplicated lines, and/or may terminate with a diagnostic message instead of producing the desired output.
Yes, I agree, the merge option requieres previous sorted files.

Regards.
This User Gave Thanks to AbelLuis 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

awk to update file based on partial match in field1 and exact match in field2

I am trying to create a cronjob that will run on startup that will look at a list.txt file to see if there is a later version of a database using database.txt as the source. The matching lines are written to output. $1 in database.txt will be in list.txt as a partial match. $2 of database.txt... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Display match or no match and write a text file to a directory

The below bash connects to a site, downloads a file, searches that file based of user input - could be multiple (all that seems to work). What I am not able to figure out is how to display on the screen match found or no match found" and write a file to a directory (C:\Users\cmccabe\Desktop\wget)... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. Shell Programming and Scripting

Merge lines based on match

I am trying to merge two lines to one based on some matching condition. The file is as follows: Matches filter: 'request ', timestamp, <HTTPFlow request=<GET: Matches filter: 'request ', timestamp, <HTTPFlow request=<GET: Matches filter: 'request ', timestamp, <HTTPFlow ... (8 Replies)
Discussion started by: jamie_123
8 Replies

4. UNIX for Dummies Questions & Answers

Merge rows into one if first 2 columns match

Hi, I wanted to merge the content and below is input and required output info. Input: /hello,a,r /hello,a,L /hello,a,X /hi,b,v /hi,b,c O/p: /hello,a,r:L:X /hi,v,:v:c Use code tags, thanks. (6 Replies)
Discussion started by: ankitas
6 Replies

5. Shell Programming and Scripting

awk match and merge with 2 files

Dear Awk experts! I have been trying to get a one liner for a match and merge operation, but having difficulties as I'm an awk newb. I always get stuck on the match and merge with 2 files. I have 2 files as follows: File 1: field 1,field 2,field 3,field 4,field 5,field 6,field 7,field... (6 Replies)
Discussion started by: landossa
6 Replies

6. Shell Programming and Scripting

Match pattern1 in file, match pattern2, substitute value1 in line

not getting anywhere with this an xml file contains multiple clients set up with same tags, different values. I need to parse the file for client foo, and change the value of tag "64bit" from false to true. cat clients.xml <Client type"FIX"> <ClientName>foo</ClientName>... (3 Replies)
Discussion started by: jack.bauer
3 Replies

7. Shell Programming and Scripting

AWK to match and merge data from 2 files into 1.

Hello, hopefully this is an easy on for the AWK guru's out there. I'm having some trouble figuring out how to match+merge data in 2 files into 1 single report. I've got my 2 files filtered and delimited, just need to MATCH $3 in file1 to $1 in file2, then put $0 from File1 and $2+$3 from File2... (6 Replies)
Discussion started by: right_coaster
6 Replies

8. Shell Programming and Scripting

Match on first and last columns and merge

Hi Friends, I have an input file of this kind input.txt output.txt The logic is as follows, read a row, compare column1 to the next, if there is a match, compare column4. If these two match, grab the minimum of column2 and maximum of column3 and print the output with column1,... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

9. Shell Programming and Scripting

Merge two file data together based on specific pattern match

My input: File_1: 2000_t g1110.b1 abb.1 2001_t g1111.b1 abb.2 abb.2 g1112.b1 abb.3 2002_t . . File_2: 2000_t Ali england 135 abb.1 Zoe british 150 2001_t Ali england 305 g1111.b1 Lucy russia 126 (6 Replies)
Discussion started by: patrick87
6 Replies

10. Shell Programming and Scripting

How to find first match and last match in a file

Hi All, I have a below file: ================== 02:53 pravin-root 02:53 pravin-root 03:05 pravin-root 02:55 pravin1-root 02:59 pravin1-root ================== How do I find the first and last value of column 1. For example, how do I find 02:53 is the first time stamp and 03:05 is... (3 Replies)
Discussion started by: praving5
3 Replies
Login or Register to Ask a Question