home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / unix / question / 15323 < prev    next >
Encoding:
Internet Message Format  |  1993-01-08  |  2.3 KB

  1. Path: sparky!uunet!haven.umd.edu!darwin.sura.net!gatech!emory!ogicse!reed!romulus!merlyn
  2. From: merlyn@ora.com (Randal L. Schwartz)
  3. Newsgroups: comp.unix.questions
  4. Subject: Re: sed how to
  5. Message-ID: <MERLYN.93Jan8084849@romulus.reed.edu>
  6. Date: 8 Jan 93 16:48:53 GMT
  7. Article-I.D.: romulus.MERLYN.93Jan8084849
  8. References: <1993Jan7.234638.6106@mtek.com>
  9. Sender: news@reed.edu (USENET News System)
  10. Distribution: usa
  11. Organization: Stonehenge Consulting Services; Portland, Oregon, USA
  12. Lines: 47
  13. In-Reply-To: bud@mtek.com's message of 7 Jan 93 23:46:38 GMT
  14.  
  15. >>>>> In article <1993Jan7.234638.6106@mtek.com>, bud@mtek.com (Bud Hovell) writes:
  16. Bud> Given rows of pipe-delimited ascii database records of this form:
  17.  
  18. Bud>   0113|Hog Farms|0213|Hogs|
  19. Bud>   0114|Poultry & Egg Farms|0251|Broiler, Fryer, and Roaster Chickens|
  20. Bud>   ||0252|Chicken Eggs|
  21. Bud>   ||0253|Turkeys and Turkey Eggs|
  22. Bud>   ||0259|Poultry and Eggs NEC|
  23. Bud>   0119|Livestock Comb'n Farms|0219|General Livestock NEC|
  24. Bud>   ||027|Animal Specialties|
  25. Bud>   0121|Honey & Other Apiary Prod. Farms|0279|Animal Specialties NEC|
  26. Bud>   0122|Horse & Other Equine Farms|0272|Horses and Other Equines|
  27.  
  28. Bud> ....what sed construct will give me output in this form:
  29.  
  30. Bud>   0113|Hog Farms|0213|Hogs|
  31. Bud>   0114|Poultry & Egg Farms|0251|Broiler, Fryer, and Roaster Chickens|
  32. Bud>   0114|Poultry & Egg Farms|0252|Chicken Eggs|
  33. Bud>   0114|Poultry & Egg Farms|0253|Turkeys and Turkey Eggs|
  34. Bud>   0114|Poultry & Egg Farms|0259|Poultry and Eggs NEC|
  35. Bud>   0119|Livestock Comb'n Farms|0219|General Livestock NEC|
  36. Bud>   0119|Livestock Comb'n Farms|027|Animal Specialties|
  37. Bud>   0121|Honey & Other Apiary Prod. Farms|0279|Animal Specialties NEC|
  38. Bud>   0122|Horse & Other Equine Farms|0272|Horses and Other Equines|
  39.  
  40. Sed is not powerful enough to do this (easily).  Awk would do it, but
  41. I'll write it as a Perl solution, and you can back-translate if you're
  42. on a backwater system that doesn't yet have Perl. :-)
  43.  
  44. #!/usr/bin/perl
  45. while (<>) {
  46.     @x = split(/\|/);
  47.     if ($x[0]) {
  48.         @save = @x;
  49.     } else {
  50.         $x[0] = $save[0];
  51.         $x[1] = $save[1];
  52.         $_ = join("|",@x);
  53.     }
  54.     print;
  55. }
  56.  
  57. Just another Perl hacker,
  58. --
  59. Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
  60. merlyn@ora.com (semi-permanent) merlyn@reed.edu (for newsreading only)
  61. factoid: "Portland, Oregon, home of the California Raisins and Lone-Star Beer!"
  62.