shell script to...

Jonathan I. Kamens jik at athena.mit.edu
Wed Apr 10 07:05:54 AEST 1991


In article <neil.671228747 at s.ms.uky.edu>, neil at ms.uky.edu (Neil Greene) writes:
|> A need a shell script that will read from another (ascii) data file, find an
|> occurance of a DRUG_NAME, write the line to another (ascii) file and append
|> the appropriate DRUG_TYPE to the new line.
|> 
|> # line with drug name in it
|> xxxx 01/02/90 xxxxxx xxxxx xxx x xxxxxxx Dipyrone .... xxxx xxxxx 
|> 
|> # rewrite new line to new ascci file
|> xxxx 01/02/90 xxxxxx xxxxx xxx x xxxxxxx Dipyrone .... xxxx xxxxx Analgesic

  You could do this in awk by reading in the first data file and putting its
contents into an associative array -- for each line in the drug type data
file, use $1 as the index in the array, and $2 as the value to associate with
that index.  Then read in the second data file and look up the drugs and
append the type to the end of the line.  Something like this:

(cat drug-list; echo "END_OF_DRUG_LIST"; cat other-data-file) | awk '
BEGIN {drug_list = 1}
/END_OF_DRUG_LIST/ {drug_list = 0; next}
drug_list != 0 {drugtypes[$1] = $2; next}
{print $0 " " drugtypes[$8]}'

  Personally, I would do this in perl, and write one function to read in the
data file and build the array, and another function to read in the other file
and do the output once the array has been built.

-- 
Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik at Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8085			      Home: 617-782-0710



More information about the Comp.unix.questions mailing list