Solved

merging two files using awk

Posted on 2000-02-25
6
438 Views
Last Modified: 2013-12-26
Here's the problem.I have 2 files say file1 and file2.The format of the files are 10 segments(represent 1 customer information) repeating inself like

dus0101 customer number
dus0102 customer related information
dus0103 customer related information
...     ...
...     ...
dus0110 ...
dus0101 customer number
...
The customers in this 2 files are in asending order of customer account number.
Now i need to mearge these 2 files and create another file(say file3) depending on the customer number and the mearged file shud be in asending order of customer number.There might be same customer number in both files.If so skip that from file1 and write from file2 that customer to file3.The logic i can implement it as a c code but the requirement is to use shell script to arrive at this requirement.I can use awk script but i douth if nested awk can be used.Please let me know if you need anything else on this.Is there any other logic i can use

Thanks.
0
Comment
Question by:dbehera
6 Comments
 

Expert Comment

by:Gyles
ID: 2558529

You could do it with a zsh script (maybe other shells too?)

create script (see below) then call it with 2 file inputs.

script.zsh  3<file1 4<file2 >outputfile

In the script use your logic from above.

Use
read -u 3 file1line
read -u 4 file2line
to get lines from the 2 files. Then use
cut to get the customer numbers. Use if to compare, then do 9 more reads and prints on whichever file has the lower number.
0
 

Expert Comment

by:mdchachi
ID: 2560251
How about using perl?  It's ideal for these kinds of tasks.
0
 
LVL 84

Expert Comment

by:ozo
ID: 2560546
it sounds like it mught just be a sort|comm
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:dbehera
ID: 2560634
>>zsh script  -- Gyles

Can you be more descriptive.I am not familiar with zsh.And i did not understand the logic u explained.

>>about using perl

Yea i can use perl and do it but i need to do it in shell programming.

 

0
 

Accepted Solution

by:
Gyles earned 100 total points
ID: 2564119
zsh is really just like bash or sh. The reason I mention it is I know you can read from 2 files.

The following should give you the gist:

#!/bin/zsh

IFS=!      # some unused character

read -u 3 file1line
read -u 4 file2line
file1custno = `echo $file1line |cut -d\  -f2-`
file2custno = `echo $file2line |cut -d\  -f2-`

if (file1custno -lt file2custno) then
      echo $file1line
      
      #copy next 2 lines 9 times:
      read -u 3 file1line
      echo $file1line
else
      # do file2 instead
fi

All you then need to do is wrap this in a loop to go to the end of each file. You test for EOF by return code for read being non-zero. Once one file is at EOF just read from the other.
0
 

Author Comment

by:dbehera
ID: 2567881
I have changed it to Perl and really Perl is so flexible in the things i want to do.It's quite easy.

Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Expand data scrubbing tool 13 32
Importing Special Characters in Dynamics GP Through Integration Manager 3 69
WinWaitActive parameters 12 31
modThree challenge 4 79
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

910 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now