Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do i get specific info's from 2 files?

Status
Not open for further replies.

rhxk

Programmer
Oct 18, 2003
3
US
Hi All,

I have 2 data files:
#1) Customer.txt which has the following:
ARRON, Germany, Sales, Jack
PHILLIP, Sweden, Programmer, Anna
JERRY, Mexico, Administrator, John
....and so on....

#2) Order.txt which has the following
203, PHILLIP, 9-Jul-88, 13-Sep-03
1429, ARRON, 12-Sep-86, 8-Feb-87
894, JERRY, 3-Sep-86, 8-Mar-99
....and so on....


I want to write an awk script that would output this:
ARRON, Germany, Sales, Jack, 1429, 09/12/1986, 02/08/1987
JERRY, Mexico, Administrator, John, 894, 09/03/1986, 03/08/1999

But I want the records to only Sep of 86 and to output in that format. I wrote an awk script that it parses out the first file, but don't know how to include the second file to the same awk script.
Here's my script.

#!/usr/bin/awk -f

BEGIN{
FS=","
OFS="\t"
}
{
custid = $1
if (custid == "AARON" || "JERRY" || custid == "PHILLIP") {
country = $9
if ((country ~ /[A-Z][a-z]/) || (country ~ /[a-z][A-Z]/)) {
print $1,$2,$4,$9
} else {
print $1,$2,$4,$10
}
} else {
# print $1
}
}


Any help would be greatly appreciated. Thanx!


As Always,
...Robert
 
Try this
Code:
#!/bin/awk -f

BEGIN {
  split("Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec",month_names,",")
  for ( i = 1 ; i <= 12 ; i++ ) {
    # so we can do month_name_to_num[&quot;Sep&quot;] and get the answer 9
    month_name_to_num[month_names[i]] = i
  }
}

# save all the lines from file1, using the first field as an index
FILENAME == ARGV[1] {
  if ( !($1 in lines) ) {
    lines[$1] = $0
  } else {
    print &quot;Duplicate record&quot;
  }
}

# use the 2nd field as an index to access all the records from file1
FILENAME == ARGV[2] {
  if ( !($2 in lines) ) {
    print &quot;Missing record&quot;
  } else {
    if ( sort_date($3) <= &quot;19860930&quot; ) {
      # print only records prior to this date
      print lines[$2], $1, print_date($3), print_date($4)
    }
  }
}

# turn d-mmm-yy into yyyymmdd
# to assist with date comparisons
function sort_date ( d,
    arr ) {
  split_date( d, arr )
  return sprintf( &quot;%04d%02d%02d&quot;, arr[3], arr[2], arr[1] )
}

# turn d-mmm-yy into mm/dd/yyyy
function print_date ( d,
    arr ) {
  split_date( d, arr )
  return sprintf( &quot;%02d/%02d/%04d&quot;, arr[2], arr[1], arr[3] )
}

# split d-mmm-yy into its components
# turn text months into numeric months
# roll yy years into the correct century (Y2K rides again!)
function split_date ( d, arr ) {
  split(d,arr,&quot;-&quot;)
  arr[2] = month_name_to_num[arr[2]]    # turn &quot;Sep&quot; into 9
  if ( arr[3] >= 70 ) {
    arr[3] += 1900
  } else {
    arr[3] += 2000
  }
}

You run this like so (for example)
Code:
./prog.awk customer.txt order.txt

--
 
Thanks for the reply...

However, I get a syntax error:

./ccc.awk Customers.txt Orders.txt
awk: syntax error near line 13
awk: illegal statement near line 13
awk: syntax error near line 15
awk: bailing out near line 15

which are:
FILENAME == ARGV[1] {
if ( !($1 in lines) ) { #line 13
lines[$1] = $0
} else { #line 15
print &quot;Duplicate record&quot;
}
}


odd...because I look at the book & this syntax is correct.
...any suggestions?...


...Robert
 
Thanks!

I changed to nawk (with a little mods to apply to my
txt files) & it worked!

Thanks a bunch!


As Always,
...Robert
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top