Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!usc!henry.jpl.nasa.gov!elroy.jpl.nasa.gov!jpl-devvax!lwall From: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Newsgroups: comp.unix.questions Subject: Re: Datafile conversion with AWK ! Keywords: datafile conversion awk sed Message-ID: <6599@jpl-devvax.JPL.NASA.GOV> Date: 14 Dec 89 22:56:35 GMT References: <539@csoftec.csf.com> Reply-To: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Organization: Jet Propulsion Laboratory, Pasadena, CA Lines: 49 In article <539@csoftec.csf.com> root@csoftec.csf.com (Cliff Manis (cmanis@csoftec)) writes: : I am needing help with data conversion, and would appreciate help : in AWK or SED and/or awk & sed. Or whatever.... Here is, of course, the answer in "whatever": :-) #!/usr/bin/perl @key = ('','',''); while (<>) { ($key,$addr,$rest,$sub) = /^([^|]*\|[^|]*\|[^|]*)\|([^|]*)\|(.*(\d))/; &printone if $key ne $lastkey; $addr[$sub-1] = $addr; } &printone; sub printone { print join('|',$lastkey,@addr,$lastrest),"\n" if $lastkey ne ''; $lastkey = $key; $lastrest = $rest; @addr = ('','','',''); } I think you should find this fairly readable. If you're uncomfortable with the hairy regular expression, here's one that uses split instead: #!/usr/bin/perl @key = ('','',''); while (<>) { (@key[0..2],$addr,@remainder) = split(/[|]/,$_,14); $sub = pop(@remainder); $key = join('|',@key); &printone if $key ne $lastkey; $addr[$sub-1] = $addr; } &printone; sub printone { print join('|',$lastkey,@addr,@lastremainder,"1\n") if $lastkey ne ''; $lastkey = $key; @lastremainder = @remainder; @addr = ('','','',''); } Larry Wall lwall@jpl-devvax.jpl.nasa.gov