Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2003
    Posts
    9

    Unanswered: 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.

  2. #2
    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

  3. #3
    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.

  4. #4
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525

    Re: Thanks

    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!

  5. #5
    Join Date
    Sep 2003
    Posts
    9
    Got it...thanks, that would have frustrated me...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •