Merging two files using shell script

hi,
i need to solve this using scripts and alsoi newbie to unix shell scripting can any one help it .its very urgent.
i tried using join command in unix..nut i am not sure..


file A.txt

contains

NB  | Data  
------------
1   | xyz  
2   | aaa
3   | yyy
4   | xxx



file B.txt contains

NB  |Curr  | Amount
--------------------
1   |AUD   | 243.5
1   |CAD   | 765
1   |USD   | 2999
2   |AUD   | 654
4   |SGD   | 8796

The result must be stored in a different file

NB  |Data  |
------------------------------------------------
1   |xyz   | AUD  243.5  CAD  765  USD  2999  
------------------------------------------------
2   |aaa   | AUD  654                                          |
------------------------------------------------
3   |yyy   |
------------------------------------------------
4   |xxx   | SGD  8796
------------------------------------------------


gopal_kgkAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

yuzhCommented:
You need to use the "paste" command to do the job, eg

paste file1 file2 > newfile

man paste
to learn more details
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
yuzhCommented:
PS, you might want to "cut" the "NB" colume of the 2nd file, before meeging (paste)
them.
0
gopal_kgkAuthor Commented:
Hi
First i want to compare both the files for NB
for NB = 1(file A.txt)  i need to compare all the NB = 1 (file B.txt)  (1 to many)

so in the example i shown above
i pick the value NB = 1 in file  A.txt using this value like (joints in table)
NB =1 in file B.txt ( i got 3 data of value NB = 1 )then merging them
like
------------------------------------------------
1   |xyz   | AUD  243.5  CAD  765  USD  2999  
------------------------------------------------


0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

gopal_kgkAuthor Commented:
hi yuzh,
i tried the paste

but it is doing like  eg
A.txt contains
1 ddd
2 fff
3 ggg

B.txt contains
1 usd 1555
1 sgd 566
1 aud 8876
3 sgd 88

result
=====
C.txt contains
1 ddd 1 usd 1555
2 fff   1 sgd 566
3 ggg 1 aud 8876
         3 sgd 88


But i want the result as
C.txt
============

1 ddd usd 1555 sgd 566 aud 8876
2 fff
3 ggg sgd 88





 

 
0
yuzhCommented:
I think it is eaier to read the records for the 2nd file into arrays, base on the "NB" value,
then read the 1st first, base on the "NB" print the record and the associate array vaules
to produce the result you wanted.

Here's my example script to read file and save it to arrays:
http:Q_21224826.html#12712005

also
http:Q_20894122.html

I'm busy today, can't spend time to write a script for you. hopefully other experts can
give you a hand, otherwise, I'll get back to you.

0
gopal_kgkAuthor Commented:
Hi yuzh,
waiting for ur script..
meanwhile i'll try with ur concept..
0
ozoCommented:
perl -ane '$_=shift @F;$a[$_][0]=$_;chomp($a[$_][@F].=" @F");END{print "@$_\n" for grep$_,@a}' [AB].txt > C.txt

head [ABC].txt
==> A.txt <==
1 ddd
2 fff
3 ggg

==> B.txt <==
1 usd 1555
1 sgd 566
1 aud 8876
3 sgd 88

==> C.txt <==
1  ddd  usd 1555 sgd 566 aud 8876
2  fff
3  ggg  sgd 88
0
gopal_kgkAuthor Commented:
hi ozo ,
i need it unix only not in perl...
0
gopal_kgkAuthor Commented:
Ok thx for everybody i got my solution...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Unix OS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.