merging files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting merging files
# 1  
Old 08-12-2008
merging files

Thanks in advance
I have 2 files having key field in each.I would like to join both on common key.I have used join but not sucessful.
The files are attached here .
what i Want in the output is on the key field SLS OFFR .
I have used join commd but not successful.
File one
=======
SNO NAME ID NO SLS OFFR
122 JACOB a1232 0002522
PUMBA a1535 0002522
STMBA c1456 0002522
111 SMITH b1233 0002513
124 JHON a1256 0002532
SWMNA a2145 0002532
327 GAOLP c3783 0002518
853 GLODY c2579 0002518

FILE TWO


NAME SLS OFFR
SUPHYA 0002533
SOTRKY 0002546
UBERTS 0002514
MANDYS 0002518
MAICHS 0002524

output required is:OUT PUT FILE
NAME SLS OFFR
SUPHYA 0002522 122 JACOB a1232
PUMBA a1535
STMBA c1456
SOTRKY 0002513 111 SMITH b1233
UBERTS 0002532 124 JHON a1256
SWMNA a2145
MANDYS 0002518 853 GLODY c2579
327 GAOLP c3783



join -1 4 -2 2 -t ' ' -o 1.1 1.2 1.3 1.4 file1 file2 > newfile

Can any one help please,Thanks in advance for your help.
# 2  
Old 08-12-2008
Hi,
You can use paste command
In this case you can also use join command as the fields are same

Regards,
Prakash
# 3  
Old 08-12-2008
Join will help if the files are sorted. Also you have in your file1 rows, that have not 4 fields/columns but only 3.

Also in your desired output your first line looks like:
Quote:
SUPHYA 0002522 122 JACOB a1232
but in your input file1, the only line that has "a1232" in it, has the key "0002522". The line of file2 that matches and contains "SUPHYA", has "0002533" as key... so how should that work. The desired output doesn't not work with the input files. I see no logic behind the output or maybe it is screwed up because it is not in [ code ] [ /code ] tags, idk.
If there is no match, you still want the line of file2 printed out, it seems, sometimes, not always.

Maybe you are better off using awk and to check if $NF of file1 matches with $2 or $NF of file2.

Something is screwed up in the desired output file, at least for me.

EDIT: Also in the attached file there is only the content of file2 as far as I can see.

@prakashreddy
He used join already and without sorting it won't work anyway. Also a simple paste will help nothing to get that desired output. Paste can't join on fields. Or maybe you can show how to do that.

Last edited by zaxxon; 08-12-2008 at 02:58 AM.. Reason: Adding a note for attachment
# 4  
Old 08-12-2008
merging files

Thanks Prakash amd Zaxxon for your reply ,
sorry for my typo for suphya 0002533 it is 0002522. sorry for that.

Secondly in the first file there are four fields but where the SLS OFFR
are the same the first field is not supplied in . That is where the
problems comes.There are NAME ID SLS OFFR But SNO is not
there and there I required the help is SLS OFFR IS SAME BUT THE
SNO is not given there the no. of fields are three and not four,otherwise
where the SNO is there the field count are four .I have given proper space
and aligned but in reality it is not showing the proper alignments.

Can we still join , logically NO , but through the shell script is only hope .

But thanks once again for your help.
# 5  
Old 08-12-2008
merging files

I will just add to simplyfy it more
file 11 abc jonny 1234
cde jigsw 1234
efg hary 1234
2 hjk nilof 2345
klm sumpr 2345

file 2john 1234
hanf 2345


output file john 1 abc jonny 1234
cde jigsw 1234
efg hary 1234
hanf 2 hjk nilof 2345
klm sumpr 2345

Here the file1 has 4th field and file 2 has 2nd field common.But the problem
is when the second line has only three field in file 1 , that is where the join
and paste command fails.

Is it possible?? Please guide ?? Thanks for your guidence.
# 6  
Old 08-12-2008
merging files

I will just add to simplyfy it more
Code:
file 1
1      abc   jonny      1234
       cde   jigsw      1234
       edg   hary       1234
2      hjk   nilof      2345
       klm   sumpr      2345


[/CODE]
Code:
file 2 john 1234 hanf 2345

Code:
output file john 1 abc jonny 1234 cde jigsw 1234 efg hary 1234 hanf 2 hjk nilof 2345 klm sumpr 2345

Here the file1 has 4th field and file 2 has 2nd field common.But the problem
is when the second line has only three field in file 1 , that is where the join
and paste command fails.

Is it possible?? Please guide ?? Thanks for your guidence.
# 7  
Old 08-12-2008
Different record types/layouts in one file always causes more problems than it's worth. I think that your easiest solution would be to (1) modify your file1 so that all line contain all 4 fields - you can do this either in the program which produces file1, or by using awk; (2) use join or paste to combine the 2 files.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Merging two files

Hi All , I have a scenario where we need to combine two files . Below are the sample files and expected output , File 1: 1|ab 1|ac 1|ae 2|ad 2|ac File 2: 1|xy 1|fc 2|gh 2|ku Output file : 1|ab|xy (3 Replies)
Discussion started by: saj
3 Replies

2. Shell Programming and Scripting

Merging two files

Guys, I am having little problem with getting a daily report! The daily process that I do is as follows 1. Unload Header for the report from the systables to one unl file, say Header.unl 2. Unload the data from the required table/tables to another unl file, say Data.unl 3. Send a... (2 Replies)
Discussion started by: PikK45
2 Replies

3. Shell Programming and Scripting

Help with merging 2 files into 1

::::::::: ::FileA:: ::::::::: A1-------A2--------A3---A4---A5-- ================================= AC5VXVLT-XX---------------------- B57E434--XXXX1-----MMMM-ZZZ--111- C325G20--XXXXX3----CCCC------3332 DC35S51--XXXXY1----DDDD------44X- DC35S52--XXXXY2----DDDD------44Y-... (5 Replies)
Discussion started by: lordsmiter
5 Replies

4. Shell Programming and Scripting

Merging two files with same name

Hello all, I have limited experience in shell scripting. Here goes my question: I have two directories that have same number of files with same file names i.e. consider 2 directories A and B. Both directories have files 1.txt, 2.txt...... I need to merge the file 1.txt of A with file 1.txt... (5 Replies)
Discussion started by: jaysean
5 Replies

5. Shell Programming and Scripting

Merging files

I have two files file 1 containing x rows and 1 column file 2 containing x rows and 1 column I want to merge both the files and add a comma between the two eg plz guide (1 Reply)
Discussion started by: test_user
1 Replies

6. UNIX for Dummies Questions & Answers

Merging two files

Hi, I have two files a.txt and b.txt. a.txt 1 2 3 4 b.txt a b c d e I want to generate a file c.txt by merging these two file and the resultant file would contain c.txt 1 (4 Replies)
Discussion started by: siba.s.nayak
4 Replies

7. Shell Programming and Scripting

merging of files.

Hi, I want to merge the two files on the basis of columns like... file 1 Data Key A 12 B 13 file2 Data Value A A1 A A2 B B1 B B2 (5 Replies)
Discussion started by: clx
5 Replies

8. Shell Programming and Scripting

merging two files

Hi everyone, I have two files which will be exactly same at first. After sometime there will be inserts in one file. My problem is how to reflect these changes in second file also. I found out that any compare and merge utility would do the job like, GNU " sdiff " command. But the... (14 Replies)
Discussion started by: rameshonline
14 Replies

9. Shell Programming and Scripting

Help with merging files

i would like to merge two files that have the same format but have different data. i would like to create one output file that contains information from both the original files.:rolleyes: (2 Replies)
Discussion started by: joe black
2 Replies

10. Shell Programming and Scripting

Merging 2 files

Hi, I have got two files 1.txt 1111|apple| 2222|orange| 2.txt 1111|1234|000000000004356| 1111|1234|000000001111| 1111|1234|002000011112| 2222|5678|000000002222| 2222|9102|000000002222| I need to merge these two so that my out put looks like below: Search code being used should be... (4 Replies)
Discussion started by: jisha
4 Replies
Login or Register to Ask a Question