home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!haven.umd.edu!darwin.sura.net!gatech!emory!ogicse!reed!romulus!merlyn
- From: merlyn@ora.com (Randal L. Schwartz)
- Newsgroups: comp.unix.questions
- Subject: Re: sed how to
- Message-ID: <MERLYN.93Jan8084849@romulus.reed.edu>
- Date: 8 Jan 93 16:48:53 GMT
- Article-I.D.: romulus.MERLYN.93Jan8084849
- References: <1993Jan7.234638.6106@mtek.com>
- Sender: news@reed.edu (USENET News System)
- Distribution: usa
- Organization: Stonehenge Consulting Services; Portland, Oregon, USA
- Lines: 47
- In-Reply-To: bud@mtek.com's message of 7 Jan 93 23:46:38 GMT
-
- >>>>> In article <1993Jan7.234638.6106@mtek.com>, bud@mtek.com (Bud Hovell) writes:
- Bud> Given rows of pipe-delimited ascii database records of this form:
-
- Bud> 0113|Hog Farms|0213|Hogs|
- Bud> 0114|Poultry & Egg Farms|0251|Broiler, Fryer, and Roaster Chickens|
- Bud> ||0252|Chicken Eggs|
- Bud> ||0253|Turkeys and Turkey Eggs|
- Bud> ||0259|Poultry and Eggs NEC|
- Bud> 0119|Livestock Comb'n Farms|0219|General Livestock NEC|
- Bud> ||027|Animal Specialties|
- Bud> 0121|Honey & Other Apiary Prod. Farms|0279|Animal Specialties NEC|
- Bud> 0122|Horse & Other Equine Farms|0272|Horses and Other Equines|
-
- Bud> ....what sed construct will give me output in this form:
-
- Bud> 0113|Hog Farms|0213|Hogs|
- Bud> 0114|Poultry & Egg Farms|0251|Broiler, Fryer, and Roaster Chickens|
- Bud> 0114|Poultry & Egg Farms|0252|Chicken Eggs|
- Bud> 0114|Poultry & Egg Farms|0253|Turkeys and Turkey Eggs|
- Bud> 0114|Poultry & Egg Farms|0259|Poultry and Eggs NEC|
- Bud> 0119|Livestock Comb'n Farms|0219|General Livestock NEC|
- Bud> 0119|Livestock Comb'n Farms|027|Animal Specialties|
- Bud> 0121|Honey & Other Apiary Prod. Farms|0279|Animal Specialties NEC|
- Bud> 0122|Horse & Other Equine Farms|0272|Horses and Other Equines|
-
- Sed is not powerful enough to do this (easily). Awk would do it, but
- I'll write it as a Perl solution, and you can back-translate if you're
- on a backwater system that doesn't yet have Perl. :-)
-
- #!/usr/bin/perl
- while (<>) {
- @x = split(/\|/);
- if ($x[0]) {
- @save = @x;
- } else {
- $x[0] = $save[0];
- $x[1] = $save[1];
- $_ = join("|",@x);
- }
- print;
- }
-
- Just another Perl hacker,
- --
- Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
- merlyn@ora.com (semi-permanent) merlyn@reed.edu (for newsreading only)
- factoid: "Portland, Oregon, home of the California Raisins and Lone-Star Beer!"
-