home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.unix.shell:3162 comp.sources.wanted:3770 alt.sources.wanted:1290 alt.usage.english:5572
- Path: sparky!uunet!ogicse!uwm.edu!linac!convex!convex!tchrist
- From: tchrist@convex.COM (Tom Christiansen)
- Newsgroups: comp.unix.shell,comp.sources.wanted,alt.sources.wanted,alt.usage.english
- Subject: Re: script to center text
- Message-ID: <1992Jul28.151623.12798@news.eng.convex.com>
- Date: 28 Jul 92 15:16:23 GMT
- Article-I.D.: news.1992Jul28.151623.12798
- References: <AeRB8p600WB60JlPZc@andrew.cmu.edu> <josef.712322528@uranium>
- Sender: usenet@news.eng.convex.com (news access account)
- Reply-To: tchrist@convex.COM (Tom Christiansen)
- Followup-To: comp.unix.shell
- Organization: CONVEX Realtime Development, Colorado Springs, CO
- Lines: 129
- Originator: tchrist@pixel.convex.com
- Nntp-Posting-Host: pixel.convex.com
- X-Disclaimer: This message was written by a user at CONVEX Computer
- Corp. The opinions expressed are those of the user and
- not necessarily those of CONVEX.
-
- From the keyboard of Josef Moellers <mollers.pad@sni.de>:
- :In <AeRB8p600WB60JlPZc@andrew.cmu.edu> es2j+@andrew.cmu.edu (Edward John Sabol) writes:
- :>Could someone post a awk, perl, or sed script which can center text. I'd
- :>like to be able to pipe a file or region into it and get out the same
- :>text only centered in 80 columns.
- :
- :>It shouldn't be hard to do, but I don't know awk or perl very well. I
- :>can do it in C, of course, but I want a script for ease of portability
- :>and the smaller size. Speed needn't be an issue.
- :
- :The following is a bourne shell script that does it, followed by a Korn
- :shell script (caveat: both can't handle backslashes!):
- ^^^^^^^^^^
-
- In my techojargon brand of English, "both can't" is not the same
- as "neither can". This is "both can't":
-
- if (!a || !b)
-
- Meaning it's true if EITHER A OR B OR BOTH CANNOT.
-
- whereas this is "neither can":
-
- if (!(a && b))
-
- Meaning is true only if neither A nor B can do something.
-
- I realize that the poster is not a native speaker of English.
- Perhaps these are equivalent in German. Perhaps I'm no longer
- a native speaker of English. :-)
-
- :-sh-sh-sh-sh-sh-sh-sh-sh-sh-sh-sh-sh-sh-sh-sh-sh-sh-sh-sh-sh-sh-sh-sh-sh-
- :#!/bin/sh
- :while read line
- :do len=`expr "$line" : '.*'`
- : prefix=`expr \( 80 - $len \) / 2`
- : while [ $prefix -ge 8 ]
- : do echo '\t\c'
- : prefix=`expr $prefix - 8`
- : done
- : while [ $prefix -ge 0 ]
- : do echo ' \c'
- : prefix=`expr $prefix - 1`
- : done
- : echo $line
- :done
-
- [ksh solution omitted]
-
- I think we have another candidate for the annual Dead Parrot Society award;
- you know, those lethargic programs that remind you of the Monty Python skit.
- Yes, folks, the Efficiency Cops are back!
-
- Consider, if you would, the following file:
-
- $ wc /etc/motd
- 74 404 4119 /etc/motd
-
- First off, here are some timings:
-
- A modestly sized file, I might add.
-
- $ time sh ctr.sh </etc/motd > /dev/null
- non-numeric argument
- test: argument expected
- test: argument expected
- 16.377u 76.475s 0:55.71 50.0% 0+0k 0+19io 12275pf+0w
-
- Well, maybe we're from the Correctness Cops, too. Let's look at
- the first line of /etc/motd:
-
- $ sed 1q /etc/motd
- * Fri Aug 16 19:13:53 CDT 1991:
-
- Let's just run the program on that line:
-
- $ sed 1q /etc/motd | sh ctr.sh
- \t\c
- \t\c
- \t\c
- \c
- 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:
-
- Oh my. That's wrong twice. First of all, you can't rely on echo
- doing backslash evalutation. (From my perpective, the SysV crew
- screwed up for trying to make it such. They should have invented
- another command, like print, to do that.) Second of all, some place
- in there you're the shell take a swipe and globbing your raw data.
-
- While both these problems can be solved by enough quoting and mucking with
- the echo args enough, you still have the problem of it running much as a
- dead parrot flies. Actually, I took a shot at fixing it, and kept
- getting whinage like this,
-
- non-numeric argument
- test: argument expected
-
- which just goes to show you that it's easier to port a shell
- than a shell script. :-(
-
- On the other hand, this solution
-
- perl -pe 'print " " x int((80 - length())/2)'
-
- not rely on echo behavior, isn't a process pig, and doesn't
- run afoul of my /bin/test command. It also runs a tad faster:
-
- $ time perl -pe 'print " " x int((80 - length())/2)' </etc/motd >/dev/null
- 0.030u 0.052s 0:00.05 50.0% 0+0k 0+5io 48pf+0w
-
- where we'll define a tad as some 1,132 times faster on this dataset.
- It also inflicts some 256 fewer page faults on the flogged system.
-
- 'Nuff said.
-
- Please direct followups appropriately:
-
- TOPIC NEWSGROUP
-
- shell programming comp.unix.shell
- (and maybe alt.sources.d)
- "both can't" alt.usage.english
- (and maybe alt.folklore.computers)
- the evils of perl alt.religion.computers
- (and maybe alt.flame)
-
-
- --tom
- --
-