home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / unix / shell / 3162 < prev    next >
Encoding:
Internet Message Format  |  1992-07-28  |  4.9 KB

  1. Xref: sparky comp.unix.shell:3162 comp.sources.wanted:3770 alt.sources.wanted:1290 alt.usage.english:5572
  2. Path: sparky!uunet!ogicse!uwm.edu!linac!convex!convex!tchrist
  3. From: tchrist@convex.COM (Tom Christiansen)
  4. Newsgroups: comp.unix.shell,comp.sources.wanted,alt.sources.wanted,alt.usage.english
  5. Subject: Re: script to center text
  6. Message-ID: <1992Jul28.151623.12798@news.eng.convex.com>
  7. Date: 28 Jul 92 15:16:23 GMT
  8. Article-I.D.: news.1992Jul28.151623.12798
  9. References: <AeRB8p600WB60JlPZc@andrew.cmu.edu> <josef.712322528@uranium>
  10. Sender: usenet@news.eng.convex.com (news access account)
  11. Reply-To: tchrist@convex.COM (Tom Christiansen)
  12. Followup-To: comp.unix.shell
  13. Organization: CONVEX Realtime Development, Colorado Springs, CO
  14. Lines: 129
  15. Originator: tchrist@pixel.convex.com
  16. Nntp-Posting-Host: pixel.convex.com
  17. X-Disclaimer: This message was written by a user at CONVEX Computer
  18.               Corp. The opinions expressed are those of the user and
  19.               not necessarily those of CONVEX.
  20.  
  21. From the keyboard of Josef Moellers <mollers.pad@sni.de>:
  22. :In <AeRB8p600WB60JlPZc@andrew.cmu.edu> es2j+@andrew.cmu.edu (Edward John Sabol) writes:
  23. :>Could someone post a awk, perl, or sed script which can center text. I'd
  24. :>like to be able to pipe a file or region into it and get out the same
  25. :>text only centered in 80 columns.
  26. :
  27. :>It shouldn't be hard to do, but I don't know awk or perl very well. I
  28. :>can do it in C, of course, but I want a script for ease of portability
  29. :>and the smaller size. Speed needn't be an issue.
  30. :
  31. :The following is a bourne shell script that does it, followed by a Korn
  32. :shell script (caveat: both can't handle backslashes!):
  33.                ^^^^^^^^^^
  34.  
  35. In my techojargon brand of English, "both can't" is not the same
  36. as "neither can".  This is "both can't":
  37.  
  38.     if (!a || !b)
  39.  
  40. Meaning it's true if EITHER A OR B OR BOTH CANNOT.
  41.  
  42. whereas this is "neither can":
  43.  
  44.     if (!(a && b))
  45.  
  46. Meaning is true only if neither A nor B can do something.
  47.  
  48. I realize that the poster is not a native speaker of English.
  49. Perhaps these are equivalent in German.  Perhaps I'm no longer
  50. a native speaker of English. :-)
  51.  
  52. :-sh-sh-sh-sh-sh-sh-sh-sh-sh-sh-sh-sh-sh-sh-sh-sh-sh-sh-sh-sh-sh-sh-sh-sh-
  53. :#!/bin/sh
  54. :while read line
  55. :do len=`expr "$line" : '.*'`
  56. :   prefix=`expr \( 80 - $len \) / 2`
  57. :   while [ $prefix -ge 8 ]
  58. :   do echo '\t\c'
  59. :      prefix=`expr $prefix - 8`
  60. :   done
  61. :   while [ $prefix -ge 0 ]
  62. :   do echo ' \c'
  63. :      prefix=`expr $prefix - 1`
  64. :   done
  65. :   echo $line
  66. :done
  67.  
  68. [ksh solution omitted]
  69.  
  70. I think we have another candidate for the annual Dead Parrot Society award;
  71. you know, those lethargic programs that remind you of the Monty Python skit.
  72. Yes, folks, the Efficiency Cops are back!
  73.  
  74. Consider, if you would, the following file:
  75.  
  76.     $ wc /etc/motd
  77.       74     404    4119 /etc/motd
  78.  
  79. First off, here are some timings:
  80.  
  81. A modestly sized file, I might add.
  82.  
  83.     $ time sh ctr.sh </etc/motd > /dev/null
  84.     non-numeric argument
  85.     test: argument expected
  86.     test: argument expected
  87.     16.377u 76.475s 0:55.71 50.0% 0+0k 0+19io 12275pf+0w
  88.  
  89. Well, maybe we're from the Correctness Cops, too.    Let's look at
  90. the first line of /etc/motd:
  91.  
  92.     $ sed 1q /etc/motd
  93.     * Fri Aug 16 19:13:53 CDT 1991:
  94.  
  95. Let's just run the program on that line:
  96.  
  97.     $ sed 1q /etc/motd | sh ctr.sh
  98.     \t\c
  99.     \t\c
  100.     \t\c
  101.      \c
  102.     ftpget ftplib.pl ftpls getdate n0 n1 n2 n3 n4 n5 n6 n7 n8 n9 statmon tree Fri Aug 16 19:13:53 CDT 1991:
  103.  
  104. Oh my.  That's wrong twice.  First of all, you can't rely on echo 
  105. doing backslash evalutation.  (From my perpective, the SysV crew
  106. screwed up for trying to make it such.  They should have invented
  107. another command, like print, to do that.)  Second of all, some place
  108. in there you're the shell take a swipe and globbing your raw data.
  109.  
  110. While both these problems can be solved by enough quoting and mucking with
  111. the echo args enough, you still have the problem of it running much as a
  112. dead parrot flies.  Actually, I took a shot at fixing it, and kept
  113. getting whinage like this,
  114.  
  115.     non-numeric argument
  116.     test: argument expected
  117.  
  118. which just goes to show you that it's easier to port a shell 
  119. than a shell script. :-(
  120.  
  121. On the other hand, this solution
  122.  
  123.     perl -pe 'print " " x int((80 - length())/2)' 
  124.  
  125. not rely on echo behavior, isn't a process pig, and doesn't
  126. run afoul of my /bin/test command.  It also runs a tad faster:
  127.  
  128.     $ time perl -pe 'print " " x int((80 - length())/2)' </etc/motd >/dev/null
  129.     0.030u 0.052s 0:00.05 50.0% 0+0k 0+5io 48pf+0w
  130.  
  131. where we'll define a tad as some 1,132 times faster on this dataset.  
  132. It also inflicts some 256 fewer page faults on the flogged system.
  133.  
  134. 'Nuff said.
  135.  
  136. Please direct followups appropriately:
  137.  
  138.     TOPIC                       NEWSGROUP
  139.  
  140.     shell programming           comp.unix.shell
  141.                     (and maybe alt.sources.d)
  142.     "both can't"                alt.usage.english      
  143.                     (and maybe alt.folklore.computers)
  144.     the evils of perl           alt.religion.computers
  145.                     (and maybe alt.flame)
  146.     
  147.  
  148. --tom
  149. -- 
  150.