home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / perl / 5874 < prev    next >
Encoding:
Internet Message Format  |  1992-09-11  |  5.3 KB

  1. Path: sparky!uunet!elroy.jpl.nasa.gov!swrinde!gatech!news.byu.edu!eff!world!ora.com!minya!jc
  2. From: jc@minya.UUCP (John Chambers)
  3. Newsgroups: comp.lang.perl
  4. Subject: Re: Columnization
  5. Message-ID: <1360@minya.UUCP>
  6. Date: 11 Sep 92 18:27:19 GMT
  7. References: <1992Aug5.182712.14285@athena.mit.edu> <BtM68r.J0A@NCoast.ORG>
  8. Lines: 103
  9.  
  10. > | Consider the example from the manual page:
  11. > |    ls | paste - - - -
  12. > | This is supposed to produce four-column output.  It does put the names
  13. > | four  to a line, but in most directories, they will not be in columns.
  14. > | There seems to be no options that will produce columnar output.
  15. > pr -m -t -w# file1 file2 ...
  16. > where # is the width (default 72).  May be System V only.
  17.  
  18. Several people have suggested something like this, but I don't see any
  19. way  that  you can use pr to columnize the output of ls like the above
  20. "ls | paste" command does (but correctly ;-). To do so, you would need
  21. a  filter  that reads ls's output and writes it into N files, and then
  22. feed these files to pr, and then remember to delete  the  intermediate
  23. files.   If you're going to write this filter, well, you might just as
  24. well have it write a single file (stdout) instead and align the fields
  25. itself.  So pr doesn't help you at all. If you have to write a program
  26. to chop up the data into multiple files, it's just as  easy  to  write
  27. your own program in C or perl to do the entire job.
  28.  
  29. Actually, I did just get a properly aligned 4-column listing  on  this
  30. Sys/V machine, using the command:
  31.    ls | paste - - - - | align l l l
  32. The  "align"  command  is  a  little C program that I wrote years ago,
  33. which aligns its input in columns.  It can do left, center,  or  right
  34. alignment,  you  can  specify  a field separator ("-s:" would work for
  35. /etc/passwd, for example), and there are a couple  of  other  goodies.
  36. The program is about 200 lines of C, so it's not all that difficult to
  37. do.  I use it all the time from within vi to columnize  tables  within
  38. documents.
  39.  
  40. For example, contrast the output of:
  41.  
  42. : ls -ltr |tail
  43.  
  44. -rw-r--r-- 1 jc       other       5227 Sep 10 11:41 People
  45. drwxr-xr-x 2 jc       mail        1024 Sep 10 12:09 uucp
  46. -rw-rw-r-- 1 jc       mail        1791 Nov  3 1991  test14
  47. drwxrwxr-x 7 jc       mail        4608 Sep 10 22:46 misc.920703
  48. -rw-rw-r-- 1 jc       mail       24110 Sep 10 22:46 NNTP.sh05
  49. drwxrwxr-x 2 jc       mail        2560 Sep 10 22:47 tuucp
  50. drwxr-xr-x 12 jc       other       5120 Sep 10 22:47 Mail
  51. -rw-rw-r-- 1 jc       mail        7550 Sep 11 10:58 Phones
  52. drwxr-xr-x 4 jc       other       2560 Sep 11 11:10 esp
  53. drwxrwxr-x 3 jc       mail        1024 Sep 11 12:13 pl
  54.  
  55. and:
  56.  
  57. : ls -ltr |tail |align l r l5 l r l r r
  58. -rw-r--r-- 1 jc    other  5227 Sep 10 11:41 People
  59. drwxr-xr-x 2 jc    mail   1024 Sep 10 12:09 uucp
  60. -rw-rw-r-- 1 jc    mail   1791 Nov  3  1991 test14
  61. drwxrwxr-x 7 jc    mail   4608 Sep 10 22:46 misc.920703
  62. -rw-rw-r-- 1 jc    mail  24110 Sep 10 22:46 NNTP.sh05
  63. drwxrwxr-x 2 jc    mail   2560 Sep 10 22:47 tuucp
  64. drwxr-xr-x 4 jc    other  5120 Sep 10 22:47 Mail
  65. -rw-rw-r-- 1 jc    mail   7550 Sep 11 10:58 Phones
  66. drwxr-xr-x 4 jc    other  2560 Sep 11 11:10 esp
  67. drwxrwxr-x 3 jc    mail   1024 Sep 11 12:13 pl
  68.  
  69. The "l5" illustrates a minimum field width.  I'm still thinking  about
  70. adding an option that would figure out how to compress things like the
  71. group and size fields, which could be one column  closer  together  in
  72. this example.  But that gets tricky.
  73.  
  74. An even worse problem turns up with things like the "ps" command.   On
  75. many  Unices  (mostly  bsd), this command occasionally produces fields
  76. that are run together, without any separator character at all.  It  is
  77. quite  difficult  to recognize such things, except on an ad-hoc basis.
  78. On the other hand, the Sys/V ps command generates an STIME field  that
  79. sometimes looks like "12:19:20" and sometimes looks like "Sep 08", and
  80. this also leads to yet more ad-hoc code. The folks who brought us Unix
  81. software  have put some interesting and amusing barriers in the way of
  82. getting things done in  a  simple,  straightforward  fashion.   It  is
  83. interesting  to  contemplate  how  one  might  go about writing a perl
  84. pattern that correctly chops apart  the  fields  of  things  like  the
  85. following excerpt:
  86.  
  87.  0 F   S    PID    C     ADDR   SZ WCHAN   STIME     TTY     TIME COMD
  88. 39 S   root 0      0  c0feb000   0 d0183c94 Aug 18   ?       2:10 sched
  89. 10 R N jc   252   65  c0feb288 397          Sep 08   console146:59 X :0
  90. 10 S   jc   11788 39  c0febb88  91 e0000000 14:04:40 pts/8   0:00 /bin/csh -c 
  91. ps -elf
  92.  
  93. Some special goodies to contemplate:  The "R N" that is one field,  as
  94. well  as  the  "Aug  18"  and  "Sep  08"  that  should  align with the
  95. "14:04:40" field below them (so you can't count  strings  of  spaces);
  96. the  missing  WCHAN  field  in  one line (so you can't count non-blank
  97. fields); the "console146:59" that is  two  fields  run  together;  the
  98. misaligned  COMD  field(s) caused by an overly-long TIME field (so you
  99. can't count columns); the dangling "ps -elf" as a separate line.  This
  100. is  a  somewhat  artificial  example, made by combining the results on
  101. several machines, but it gives you an idea of what you have to contend
  102. with to write a portable program that handles all such data.
  103.  
  104. Is there a way to handle such abortions in perl?
  105.  
  106. -- 
  107. All opinions Copyright (c) 1992 by John Chambers. Inquire for licensing at:
  108. 1-617-647-1813 ...!{bu.edu,harvard.edu,eddie.mit.edu,ruby.ora.com}!minya!jc 
  109. --
  110. Pensu tutmonde; agu loke.
  111.