If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Shell script, data search, extract, and convert.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-14-04, 17:07
msb2112 msb2112 is offline
Registered User
 
Join Date: Sep 2003
Posts: 9
Shell script, data search, extract, and convert.

Hi,

I have two input files, "filea", "fileb".

filea = a "key" file containing an eight character customer number string in the first column, plus the "^" indicating it's located at the beginning of "fileb" and the target customer number in the second column which the first customer number must be converted to for the records in "fileb".

Org Cust# NewCust#
Examples: ^10532211 ^66040001
^10685660 ^66055522
^10644559 ^66032110
^11199000 ^66078890

fileb = a large data file containing addresses, where many addresses exist for single customers from "filea". The first eight characters within this file contain the data strings from "filea".

So, in summary I need to extract data from "fileb" using the key from the first column in filea, and replace the customer number for the extracted records with the data from the second column in "filea".

I suppose I need to use sed or awk or something but I don't know how to do this. I hope I explained this well enough.

Thanks for the help, Mike.
Reply With Quote
  #2 (permalink)  
Old 01-16-04, 06:19
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
The following example should work provided your fileb does not happen to contain the customer code string elsewhere in the record.

awk '
BEGIN {
while (getline < "filea" > 0){newcust[substr($1,2)] = substr($2,2)}
close ("filea")
}
{
for (oldcode in newcust)
{
if (match($0,oldcode == 1))
gsub(oldcode,newcust[oldcode],$0)
}
print $0
}' fileb

If fileb separates the customer code and the address by whitespace, replace...

if (match($0,oldcode == 1))
gsub(oldcode,newcust[oldcode],$0)

with...

if ($1==oldcode)
$1=newcust[oldcode]

HTH
Reply With Quote
  #3 (permalink)  
Old 01-16-04, 08:57
msb2112 msb2112 is offline
Registered User
 
Join Date: Sep 2003
Posts: 9
Thanks

Hi Damian, thanks for the reply. I'll certainly try this script. I'll let you know the results.

Mike.
Reply With Quote
  #4 (permalink)  
Old 01-16-04, 09:00
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
Re: Thanks

Quote:
Originally posted by msb2112
Hi Damian, thanks for the reply. I'll certainly try this script. I'll let you know the results.

Mike.
Just noticed a mistake...

if (match($0,oldcode == 1))

should be...

if (match($0,oldcode) == 1)

Oops!
Reply With Quote
  #5 (permalink)  
Old 01-16-04, 10:03
msb2112 msb2112 is offline
Registered User
 
Join Date: Sep 2003
Posts: 9
Got it...thanks, that would have frustrated me...
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On