Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!bu.edu!snorkelwacker.mit.edu!bloom-picayune.mit.edu!athena.mit.edu!jik From: jik@athena.mit.edu (Jonathan I. Kamens) Newsgroups: comp.unix.questions Subject: Re: shell script to... Keywords: sed, awk, script Message-ID: <1991Apr9.210554.16312@athena.mit.edu> Date: 9 Apr 91 21:05:54 GMT References: Sender: news@athena.mit.edu (News system) Organization: Massachusetts Institute of Technology Lines: 32 In article , neil@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@Athena.MIT.EDU Allston, MA 02134 Office: 617-253-8085 Home: 617-782-0710