#!/bin/bash
awk 'FNR > 1 && NR==FNR {
f_name = tolower($1) # first name
l_name = tolower($2) # last name
full_name = f_name":"l_name # full name used as array index
address_city_county_state_zip = $4","$5","$6","$7","$8
address[full_name] = address_city_county_state_zip
next
}
{
first_n = tolower($1) # first name
last_n = tolower($2) # last name
full_n = first_n":"last_n # full name used as array index
printf ("%-10s %-10s %-55s %06.2f %-10s\n", $1, $2, address[full_n], $4, FILENAME)
}' $1 $2
./ch10_challenge.awk addresses.txt nameemailavg.tab
Input file nameemailavg.tab
:
printf 'Art\tVenere\tart@venere.org\t256.62394383\nLenna\tPaprocki\tlpaprocki@hotmail.com\t259.688783099\nDonette\tFoller\tdonette.foller@cox.net\t282.32979844\n' > nameemailavg.tab
Input file addresses.txt
:
printf 'first_name\tlast_name\tcompany_name\taddress\tcity\tcounty\tstate\tzip\tphone1\tphone2\temail\tweb\nAbel\tMaclead\tRangoni Of Florence\t37275 St Rt 17m M\tMiddle Island\tSuffolk\tNY\t11953\t631-335-3414\t631-677-3675\tamaclead@gmail.com\thttp://www.rangoniofflorence.com\nArt\tVenere\tChemel, James L Cpa\t8 W Cerritos Ave #54\tBridgeport\tGloucester\tNJ\t08014\t856-636-8749\t856-264-4130\tart@venere.org\thttp://www.chemeljameslcpa.com\nDonette\tFoller\tPrinting Dimensions\t34 Center St\tHamilton\tButler\tOH\t45011\t513-570-1893\t513-549-4561\tdonette.foller@cox.net\thttp://www.printingdimensions.com\nSimona\tMorasca\tChapman, Ross E Esq\t3 Mcauley Dr\tAshland\tAshland\tOH\t44805\t419-503-2484\t419-800-6759\tsimona@morasca.com\thttp://www.chapmanrosseesq.com\nKiley\tCaldarera\tFeiner Bros\t25 E 75th St #69\tLos Angeles\tLos Angeles\tCA\t90034\t310-498-5651\t310-254-3084\tkiley.caldarera@aol.com\thttp://www.feinerbros.com\n' > addresses.txt
$ cat nameemailavg.tab
Art Venere art@venere.org 256.62394383
Lenna Paprocki lpaprocki@hotmail.com 259.688783099
Donette Foller donette.foller@cox.net 282.32979844
$ cat addresses.txt
first_name last_name company_name address city county state zip phone1 phone2 email web
Abel Maclead Rangoni Of Florence 37275 St Rt 17m M Middle Island Suffolk NY 11953 631-335-3414 631-677-3675 amaclead@gmail.com http://www.rangoniofflorence.com
Art Venere Chemel, James L Cpa 8 W Cerritos Ave #54 Bridgeport Gloucester NJ 08014 856-636-8749 856-264-4130 art@venere.org http://www.chemeljameslcpa.com
Donette Foller Printing Dimensions 34 Center St Hamilton Butler OH 45011 513-570-1893 513-549-4561 donette.foller@cox.net http://www.printingdimensions.com
Simona Morasca Chapman, Ross E Esq 3 Mcauley Dr Ashland Ashland OH 44805 419-503-2484 419-800-6759 simona@morasca.com http://www.chapmanrosseesq.com
Kiley Caldarera Feiner Bros 25 E 75th St #69 Los Angeles Los Angeles CA 90034 310-498-5651 310-254-3084 kiley.caldarera@aol.com http://www.feinerbros.com
Expected output:
| Art | Venere | James,L,Cpa,8,W | 256.62 | nameemailavg.tab |
| ---- | ---- | ---- | --- | --- |
| Lenna | Paprocki | 259.69 | nameemailavg.tab |
| Donette | Foller | Dimensions,34,Center,St,Hamilton | 282.33 | nameemailavg.tab |
The undesirable output is the first row with first_name, last_name, 000.00, last_name:
| first_name | last_name | 000.00 | addresses.txt |
| ---- | ---- | ---- | ---- | ---- |
| Art | Venere | James,L,Cpa,8,W | 256.62 | nameemailavg.tab |
| Lenna | Paprocki | 259.69 | nameemailavg.tab |
| Donette | Foller | Dimensions,34,Center,St,Hamilton | 282.33 | nameemailavg.tab |
Questions:
- Why does my code print the first line from the file "addresses.txt"?
- an
awk
program consists of apattern { action }
. Since lines 11-14 do not have a pattern and only an action, they are not executed because the previous block hasnext
? - I updated the code block (10-15) to include the pattern
FS="\t"
so I can get topattern { action }
format. Unfortunately that had no impact. - When I reversed the call to
awk
like so,./ch10_challenge.awk nameemailavg.tab addresses.txt
, the output was messed up (expected), but the first line was from the file "nameemailavg.tab". In other words, whatever files is the first file, the output has that file as a 'header'.
Here is the revised code from the second block (line 10ff):
FS = "\t" {
first_n = tolower($1) # first name
last_n = tolower($2) # last name
full_n = first_n":"last_n # full name used as array index
printf ("%-10s %-10s %-55s %06.2f %-10s\n", $1, $2, address[full_n], $4, FILENAME)
}' $1 $2