The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Joining 2 CSV files together chachabronson UNIX for Advanced & Expert Users 3 05-06-2008 03:42 AM
Problem joining 2 files rochitsharma UNIX for Advanced & Expert Users 4 04-03-2008 03:12 AM
Joining lines from two files - please help chandra004 Shell Programming and Scripting 25 07-26-2006 11:39 PM
joining files Manu UNIX for Dummies Questions & Answers 2 04-25-2005 09:24 AM
joining 2 files webtekie UNIX for Dummies Questions & Answers 1 10-21-2003 07:51 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 09-27-2006
Registered User
 

Join Date: Sep 2006
Posts: 3
Help with joining two files

Greetings, all. I've got a project that requires I join two data files together, then do some processing and output. Everything must be done in a shell script, using standard unix tools. The files look like the following:

File_1
Layout:
Acct#,Subacct#,Descrip
Sample:
0001,0001,Account1/Sub1
0001,0002,Account1/Sub2
0002,0001,Account2/Sub1
0002,0002,Account2/Sub2
0002,0003,Account2/Sub3
...

File_2
Layout:
TransID,Code,Acct#,SubAcct#,Date,To,For,Amount,Ref#
Sample:
1,D,0002,0001,2006-01-03,Joe,Services,35.00,1234
2,C,0002,0003,2006-01-05,Mary,PC Repair,50.00,
3,D,0001,0001,2006-01-05,Amazon.com,book,39.95,1235
...

In essence, I need to add the proper description from File_1 into each record of File_2. Once the join is complete, I plan on using awk to summarize the data and output, so order is not important (description can go in right after the acct# & subacct# columns in File_2, or it can be appended to the end of each line).

I've tried join already, but since join expects to match on a single field, and I'm trying to match on two fields, join hasn't been much help. I've also tried sed, but I can't seem to get the replacement syntax right.

Any help would be greatly appreciated.

Rich Lohman
Reply With Quote
Forum Sponsor
  #2  
Old 09-27-2006
aigles's Avatar
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,212
Try an adapt the following awk program :
Code:
BEGIN {
   FS = OFS = ",";
}
FNR == NR {
   Descr[$1,$2] = $3;
   next;
}
{
   if (($3,$4) in Descr) 
      print $0,Descr[$3,$4];
   else
      print $0,"Unknown Account";
}
Execute the program with the command :
Code:
awk -f program.awk File_1 File_2
The account description will be append to the end of each line of File_2.


Jean-Pierre.
Reply With Quote
  #3  
Old 09-27-2006
Registered User
 

Join Date: Sep 2006
Posts: 3
That did the trick! Thanks Jean-Pierre!
Reply With Quote
  #4  
Old 09-27-2006
Registered User
 

Join Date: Sep 2006
Posts: 3
Just a thought

Hi,

I'm failly new to UNIX scripting, but couldn't cat work?

I think cat file_1 file_2 > endfile will create a file that has file_1 and file_2 stuck together.

Or maybe grep the line you want, pipe it into another file and then use cat to join.

But then, I'm a newbie, so there's some glaring thing I might be missing.

Best wishes,
Laurel
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 06:45 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0