Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!caen!ox.com!math.fu-berlin.de!fub!einoed!uwbln!jde From: jde@uwbln.uucp (Jutta Degener) Newsgroups: comp.unix.questions Subject: Re: shell script to... Keywords: sed, awk, script Message-ID: <1991Apr14.082754.17956@uwbln.uucp> Date: 14 Apr 91 08:27:54 GMT References: <1991Apr11.171043.13657@convex.com> Organization: UniWare Computer GmbH, Berlin Lines: 49 Neil Greene writes: > I have a file that contains drug names and next to the drug name is the drug > group. > > > Dipyrone Analgesic > > [...] > > I 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. `write all occurances of into a file, followed by ' is something sed can do well. A statement like "/name/s/$/ /w file" or "/name/s/$/ /p" for stdout works fine. Your input file with the drug pairs is almost a `program' you could feed to sed. One would want to add a few slashes, but that, again, could be accomplished using sed, for example with s/\([-a-zA-Z0-9]*\)[ ]*\([-a-zA-Z0-9]*\)/-e \/\1\/s\/$\/\ \2\/p/p which turns foo bar into -e /foo/s/$/ bar/p if I'm not mistaken (quote until works). Now you have three possibilities left: (a) forget it, get pearl (b) figure out how to get those darn spaces across and end up using something like eval sed -n `sed -n -e "s/\([-a-zA-Z0-9]*\)[ ]*\([-a-zA-Z0-9]*\)/ -e \'\/\1\/s\/$\/\ \2\/p'/p" < $1` which actually seems to work (joined into one line) from sh, given the drug file as first argument (c) get bitten by the command line length limit, use a tempfile for the sed program or go back to step (a) Still waiting for the awk solution, Jutta -- #include Jutta Degener jutta@tub.cs.tu-berlin.de (owl:hugs.)