[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Merging two files using shell script

Posted on 2004-11-30
9
Medium Priority
?
6,006 Views
Last Modified: 2008-01-09
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
------------------------------------------------


0
Comment
Question by:gopal_kgk
  • 5
  • 3
9 Comments
 
LVL 38

Accepted Solution

by:
yuzh earned 1500 total points
ID: 12713151
You need to use the "paste" command to do the job, eg

paste file1 file2 > newfile

man paste
to learn more details
0
 
LVL 38

Expert Comment

by:yuzh
ID: 12713168
PS, you might want to "cut" the "NB" colume of the 2nd file, before meeging (paste)
them.
0
 

Author Comment

by:gopal_kgk
ID: 12713201
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:gopal_kgk
ID: 12713332
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
 
LVL 38

Expert Comment

by:yuzh
ID: 12713424
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
 

Author Comment

by:gopal_kgk
ID: 12713521
Hi yuzh,
waiting for ur script..
meanwhile i'll try with ur concept..
0
 
LVL 85

Expert Comment

by:ozo
ID: 12714213
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
 

Author Comment

by:gopal_kgk
ID: 12722409
hi ozo ,
i need it unix only not in perl...
0
 

Author Comment

by:gopal_kgk
ID: 12722484
Ok thx for everybody i got my solution...
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Suggested Courses
Course of the Month19 days, 16 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question