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?
# 8  
Old 08-25-2011
Can you post sample data that is giving incorrect output as well as the desired output?
# 9  
Old 08-25-2011
Question

Quote:
Originally Posted by bartus11
Can you post sample data that is giving incorrect output as well as the desired output?
Incorrect Output:

1 STATUS 60462578 60933968
2 CMPGNPT 75389063 75389063
3 LEADHIST 1716608 1716608
4 INBNDCN 13039079 13039079
5 MOD 21915188 22071450
6 DLGSCRS 5615481 5615481
7 PROS 41398096 41398096
8 CMPHST 44773566 47099303
9 PRSFINC 6737299 6737299

Desired Output:

1 STATUS 60462578 60933968
2 CMPGNPT 75389063 75389063
3 LEADHIST 1716608 1716608
4 INBNDCN 13039079 13039079
5 MOD 21915188 22071450
6 DLGSCRS 5615481 5615481
7 PROS 41398096 41398096
8 CMPHST 44773566 47099303
9 PRSFINC 6737299 6737299
10 PGNSCOR 773422 773422
11 respshist 22844444 22846172
12 commhist 48543695 49093924
13 commsta 55229204 55229204
14 INTRTHIST 9723948 9885086


Just these 10-14 records are not coming.......
# 10  
Old 08-25-2011
You didn't post your input data...
# 11  
Old 08-25-2011
Quote:
Originally Posted by bartus11
You didn't post your input data...
Input 1:

1 STATUS 60462578
2 CMPGNPT 75389063
3 LEADHIST 1716608
4 INBNDCN 13039079
5 MOD 21915188
6 DLGSCRS 5615481
7 PROS 41398096
8 CMPHST 44773566
9 PRSFINC 6737299
10 PGNSCOR 773422
11 respshist 22844444
12 commhist 48543695
13 commsta 55229204
14 INTRTHIST 9723948



Input 2:

1 imstatus.load 60933968
2 immtnhist.load 75389063
3 imadhistory.load 1716608
4 imcntcthist.load 13039079
5 imccist.load 22071450
6 imccscrs.load 5615481
7 imcospect.load 41398096
8 imccmpdlrhst.load 47099303
9 imctcmpgnfinc.load 6737299
10 imcctcmpgnscor.load 773422
11 imcrespshist.load 22846172
12 imclecommhist.load 49093924
13 imcagevci.load 55229204
14 imcclofintrthist.load 9885086

All are separated using Tab.

Desired out put is:

Column 1 is serial numbers (col 1 from Input 1 or 2), Column 2 is from input_1 Col 2, Column 3 is input_1 col 3, Column 4 is input_2 col 3.

For that I wrote the syntax as mentioned before, but 10-14 records are missing in the output.
# 12  
Old 08-25-2011
This is weird, cause I get correct output for this data:
Code:
1 STATUS 60462578 60933968
2 CMPGNPT 75389063 75389063
3 LEADHIST 1716608 1716608
4 INBNDCN 13039079 13039079
5 MOD 21915188 22071450
6 DLGSCRS 5615481 5615481
7 PROS 41398096 41398096
8 CMPHST 44773566 47099303
9 PRSFINC 6737299 6737299
10 PGNSCOR 773422 773422
11 respshist 22844444 22846172
12 commhist 48543695 49093924
13 commsta 55229204 55229204
14 INTRTHIST 9723948 9885086

Can you post output of:
Code:
cat -Te file1
cat -Te file2

# 13  
Old 08-25-2011
Can you post output of:
Code:
cat -Te file1
cat -Te file2

[/QUOTE]

cat: Not a recognized flag: T
Usage: cat [-qrSsu] [-n[b]] [-v[et]] [-|File ...]
# 14  
Old 08-25-2011
join is not appropriate for this problem, at least not without some further processing. The key you are trying to join on is not properly sorted. It is sorted numerically but the standard says requires that "The files ... shall be ordered in the collating sequence of sort -b on the fields on which they shall be joined".

To make this work, you'd have to pre-sort each of the files, on the first field, so that the order is similar to 1, 10, 2, 20 ... instead of 1, 2, 10, 20 ... run the join, and finally numerically sort the result on the first column.

Regards,
Alister
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