How to combine two files based on fields?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to combine two files based on fields?
# 1  
Old 08-24-2011
Question How to combine two files based on fields?

I have two files which are as follows:
File 1:
Code:
1 abc 250
2 pqr 300
3 xyz 100


File 2:
Code:
1 abc 230
2 pqr 700
3 xyz 500

Now I need output File, File 3as:

Code:
S.No Name Count1 Count2
1 abc 250 230
2 pqr 300 700
3 xyz 100 500

NOTE:
It will be good if you combine based on first field, which are serial numbers, i.e; Compare S.nos from both files, Print Name column from file2 (or file 1, only one), Count 1 from file1, count 2 from file2.

(Because may be there is a little difference in Name fields, but the purpose of both Name fileds are same)

Can any one help me in getting the unix Script for this?

Actually my list will be of 14.


Thanks.

Last edited by Franklin52; 08-25-2011 at 01:59 PM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 08-24-2011
The easiest way to do this is by using, join. Join joins the 2 files based on a specified field, in your case field 1. This is the first time that I am using join myself and this may be one way to do this:
Code:
join -j 1 file1 file2 | awk '{print $1 " " $2 " " $3 " " $5 }'

- GP
# 3  
Old 08-24-2011
MySQL

Thanks mate....

Actually I came upto Join... but that Awk was not striked for me using Pipe.


Many Thanks!!!
# 4  
Old 08-24-2011
AWK in this case can be avoided by using -o option of join:
Code:
join -j1 -o 1.1,1.2,1.3,2.3 file1 file2

This User Gave Thanks to bartus11 For This Post:
# 5  
Old 08-25-2011
Quote:
Originally Posted by g.pi
The easiest way to do this is by using, join. Join joins the 2 files based on a specified field, in your case field 1. This is the first time that I am using join myself and this may be one way to do this:
Code:
join -j 1 file1 file2 | awk '{print $1 " " $2 " " $3 " " $5 }'

- GP

This was not worked for the serial no 10-14 ; which are two digits.
But if i take 1-9 in one file and 10-14 in secon file, then it works.

But if 1-14 are in one single file, then when I use the above syntax it was showing only 1-9 records.

---------- Post updated at 09:12 PM ---------- Previous update was at 09:11 PM ----------

Quote:
Originally Posted by bartus11
AWK in this case can be avoided by using -o option of join:
Code:
join -j1 -o 1.1,1.2,1.3,2.3 file1 file2


Throwing error:

Usage: join [-a Filenum] [-v Filenum] [-[j] Filenum Fieldnum]
[-t Character] [-o list [-e String] ] File1 File2
# 6  
Old 08-25-2011
Try:
Code:
join -j 1 -o 1.1,1.2,1.3,2.3 file1 file2

# 7  
Old 08-25-2011
Quote:
Originally Posted by bartus11
Try:
Code:
join -j 1 -o 1.1,1.2,1.3,2.3 file1 file2

Thanks for correcting me in syntax!

Even now also it was showing only 1 to 9 records, what about 10 to 14 records?
How can I get those?

(Which I was discussed above).

Thanks!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Combine and complete multiple CSV files based on 1 parameter

I have to create a new CSV file based on the value listed on the 3rd column from different CSV files. This is what I need: 1. I should substitute the first column from each file, excluding the headers, with the file name InputXX. 2. Then, I need to look for rows with 0 on the third column in... (7 Replies)
Discussion started by: Xterra
7 Replies

2. Shell Programming and Scripting

Awk: Combine multiple lines based on number of fields

If a file has following kind of data, comma delimited 1,2,3,4 1 1 1,2,3,4 1,2 2 2,3,4 My required output must have only 4 columns with comma delimited 1,2,3,4 111,2,3,4 1,222,3,4 I have tried many awk command using ORS="" but couldnt progress (10 Replies)
Discussion started by: mdkm
10 Replies

3. Shell Programming and Scripting

Combine 3 files based on a pattern

HI, I have 3 files that contain the following information (sql output from Oracle database stored in a txt file): File1.txt : alter table "SYS"."INT_COST_PRICE" enable row movement; alter table "SYS"."INT_SOH" enable row movement; alter table "SYSMAN"."XX_ACI_SKURTP" enable row movement;... (6 Replies)
Discussion started by: rparavastu
6 Replies

4. Shell Programming and Scripting

Compare three files based on two fields

Guys, I tried searching on the internet and I couldn't get the answer for this problem. I have 3 files. First 2 fields of all of them are of same type, say they come from various databases but first two fields in the 3 files means the same. I need to verify the entries that are not present... (4 Replies)
Discussion started by: PikK45
4 Replies

5. Shell Programming and Scripting

combine lines from two files based on an if statement

I'm rather new to programming, and am attempting to combine lines from 2 files in a way that is way beyond my expertise - any help would be appreciated! I need to take a file (file1) and add columns to it from another file (file2). However, a line from file2 should only be added to a given line... (3 Replies)
Discussion started by: Cheri
3 Replies

6. Shell Programming and Scripting

Compare two files based on values of fields.

Hi All, I have two files and data looks like this: File1 Contents #Field1,Field2 Dist_Center_file1.txt;21 Dist_Center_file3.txt;20 Dist_Center_file2.txt;20 File2 Contents (*** No Header ***) Dist_Center_file1.txt;23 Dist_Center_file2.txt;20 Dist_Center_file3.txt;20 I have... (4 Replies)
Discussion started by: Hangman2
4 Replies

7. Shell Programming and Scripting

how to combine fields from different files

linus>cat data1 8119463 | 15000 8136789 | 50000 8152238 | 15000 8167890 | 15000 7155789 | 15000 8123334 | 30000 7166330 | 15000 8146683 | 15000 8152238 | 15000 8167877 | 15000 7145389 | 15000 8178434 | 15000 7166222 | 50000 8167566 | 15000 8115410 | 45000... (2 Replies)
Discussion started by: littleb
2 Replies

8. Shell Programming and Scripting

how to combine 2 lines in same files based on any text

hi, I want to combine two lines in same file. If the line ends with '&' it should belongs to previous line only Here i am writing example. Ex1: line 1 : return abcdefgh& line 2 : ijklmnopqr& line 3 : stuvw& line 4 : xyz output should be line 1: return abcdefghijklmnopqrstuvwxyz ... (11 Replies)
Discussion started by: spc432
11 Replies

9. Shell Programming and Scripting

sorting(both Ascending & Descending) files based on multiple fields

Hi All, I am encountered with a problem while sorting a file based on multiple columns . I need to sort like: (field2,ascending) , (field3,ascending) ,(field8,descending) , (field7,ascending),(field13,ascending). So far i was sorting only in ascending order but here i need to use one... (1 Reply)
Discussion started by: apjneeraj
1 Replies

10. Shell Programming and Scripting

Joining two files based on columns/fields

I've got two files, File1 and File2 File 1 has got combination of col1, col2 and col3 which comes on file2 as well, file2 does not get col4. Now based on col1, col2 and col3, I would like to get col4 from file1 and all the columns from file2 in a new file Any ideas? File1 ------ Col1 col2... (11 Replies)
Discussion started by: rudoraj
11 Replies
Login or Register to Ask a Question