Settings

The page will reload to apply your changes.
Theme

ASCII Art Font

ASCII Art Menu

Original Usenet message from alt.ascii-art, 7 Mar 1995.
Answer: Centering Stuff

Answer: Centering Stuff

Brad <syb...@aber.ac.uk> writes:

>	Does anybody have a script that centers text or pictures or whatever?
>I know this is only a really simple tr command, but I can't figure it out
>right now. :) Thanks... 

tr(1) is used to _translate_ characters... I don't think you can
use it to do text centering.

Ok, I have *two* solutions...

This awk script will center text according to the
length of *each* input line:

========8<----cut-all-above-this-line-and-keep-all-below-------
#!/usr/bin/awk -f
#
# center
#
# Centers all input text on an 80-column line according to the
# length of each input line.
#
# Tony Nugent ** T.N...@sct.gu.edu.au ** tnu...@gucis.cit.gu.edu.au
#
{
  pad=""
  if ( length < 78 ) {
    le = ( 80-length ) / 2
    for ( i=0 ; i<le ; i++ ) {
      pad=pad" "
    };
  } print pad $0
}
========8<----cut-all-below-this-line-and-keep-all-above-------

This could easily be modified so that you can give it a parameter
to set the right hand margin to something other than 80 columns.

However, Brad and I have had some email after he also posted this
question into the ascii art mailing list.  He was looking for
something that would evenly center lines in a file that had
uneven length.  Specifically, he was wanting to center ascii art,
and if you use the above script on files with lines of uneven
length then the result would be mashed.

Ok, so I've come up with a solution that should solve this
problem.  The following script will center lines in a file so
that the centering offset depend on the length of the longest
line in the file:

========8<----cut-all-above-this-line-and-keep-all-below-------
#!/usr/bin/awk -f
#
# centermax
#
# Centres all input text on an 80-column line using the length of
# the longest input line as the centering offset.
#
# Tony Nugent ** T.N...@sct.gu.edu.au ** tnu...@gucis.cit.gu.edu.au
#
{
  if ( length > le ) {
    le = length
  }
  line[numlines++] = $0
}
END {
  pad=""
  if ( le < 78 ) {
    le = ( 80-le ) / 2
    for ( i=0 ; i<=le ; i++ ) {
      pad=pad" "
    };
  }
  for ( i=0; i<numlines; i++ ) {
    print pad line[i]
  }
}
========8<----cut-all-below-this-line-and-keep-all-above-------

This one does seem to work the way he was wanting it to.

I hope others find it useful.

BTW, I do have another much longer two-part script that does
left/center/right justification, and you can also specify the
right-hand margin.  If anyone wants a copy of this then email
me... but warning - I haven't used it in a while and I don't know
how reliable it is :)

Share and Enjoy!  :-)

(Cc. to Brad... just to let you know mate that I've posted it in
r.a.a,etc,etc with some minor changes including the deletion of a
debugging line and fixing of the i count :)

Cheers
Tony
        MMM             \|/              www              __^__
       (o o)            @ @             (o o)            /(o o)\
  -.ooO-(_)-Ooo.-+-.oOO-(_)-OOo.-+-.oOO--(_)--OOo.-+-.oOO==(_)==OOo.-----+
  |    Tony Nugent  |-| Griffith University  T.N...@sct.gu.edu.au      |--.
  |     __'!`__     | | Brisbane, Queensland tnu...@gucis.cit.gu.edu.au |  |
  |      (o o)      | | Australia                                        |  |
  `-ooO---(=)---Ooo-' `--------------------------------------------------'  |
    `------------------' `--------------------------------------------------'

Original message headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f996b,c4df7801762e854d
X-Google-Attributes: gidf996b,public
X-Google-Thread: fbb9d,c4df7801762e854d
X-Google-Attributes: gidfbb9d,public
X-Google-Thread: 110f55,c4df7801762e854d
X-Google-Attributes: gid110f55,public
X-Google-ArrivalTime: 1995-03-08 05:19:26 PST
Path: bga.com!news.sprintlink.net!news.wwa.com!not-for-mail
From: Tony Nugent <T.N...@sct.gu.edu.au>
Newsgroups: alt.ascii-art,alt.binaries.pictures.ascii,rec.arts.ascii
Subject: Answer: Centering Stuff
Date: 7 Mar 1995 02:21:17 -0600
Organization: Griffith University Brisbane Queensland Australia
Lines: 108
Sender: bob...@gagme.wwa.com
Approved: bob...@wwa.com
Message-ID: <3jh51t$1e...@gagme.wwa.com>
References: <3jdmou$jr...@gagme.wwa.com>
Reply-To: T.N...@sct.gu.edu.au
NNTP-Posting-Host: gagme.wwa.com
Xref: bga.com alt.ascii-art:19544 alt.binaries.pictures.ascii:2170 rec.arts.ascii:4245
About this message. This message was posted publicly to the newsgroup alt.ascii-art in 1995 and is mirrored here unchanged as part of the Historic Archives – only email addresses are masked. The artwork and text belong to their original authors: if you copy a piece, keep the artist's initials or signature intact and credit them where you can. Are you the author? Contact us to get your posts attributed, connected to your artist profile, or removed.

Report this message

Help us keep the archive clean and accurate. Reports are reviewed by a person – nothing is changed automatically.

Help classify this post