LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-28-2004, 08:25 PM   #1
Soulful93
Member
 
Registered: Dec 2003
Location: Denver, CO
Distribution: SuSE 9.2 Slackware 10.1
Posts: 137

Rep: Reputation: 15
Center Output


I'm trying to center the output of

cd /etc
cat passwd

how would I do that?
 
Old 04-28-2004, 11:13 PM   #2
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
Not sure why, but...
Code:
#!/bin/sh

IFS='
'
WIDTH=`stty -a | grep columns | cut -d';' -f3 | awk ' { print $2 }'`

typeset -i LENGTH
for i in `cat /etc/passwd`; do
  LENGTH=`echo $i | wc -c`
  DIFF=`expr $WIDTH - $LENGTH`
  SPACE=`expr $DIFF / 2`
  while [ $SPACE -gt 0 ]; do
     echo  -n ' '
     SPACE=`expr $SPACE - 1`
  done
  echo $i
done
 
Old 04-28-2004, 11:19 PM   #3
rkef
Member
 
Registered: Mar 2004
Location: bursa
Posts: 110

Rep: Reputation: 15
There are many text formatting programs that would do this sort of thing. It's also fairly simple to write an ugly script (doesn't break on lines greater than the screen width):

Code:
#!/usr/bin/perl -w
use strict;
                                                                                                    
my $screen_width = 100;
                                                                                                    
for my $line (<>) {
        my $line_length = length $line;
        my $whitespace = $screen_width/2 - $line_length / 2;
        print " " while ($whitespace-- > 0);
        print $line;
}
Just pipe in whatever you like. I hardcoded the screen width because, for reasons unbeknownst to me, my script doesn't receive the $COLUMNS environment variable. Season to taste.

(As I was previewing this, I noticed someone else was also bored hahaha! edit: ooh stty ... I'm asleep at the wheel.)

Last edited by rkef; 04-28-2004 at 11:34 PM.
 
Old 04-29-2004, 02:11 AM   #4
Castelior
LQ Newbie
 
Registered: Apr 2004
Location: /France/Nantes
Posts: 1

Rep: Reputation: 0
Try this:

cat /etc/passwd|sed -e :a -e 's/^.\{1,77\}$/ & /;ta'

change 77 to modify line length
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Crystal cs46xx (Hercules GTXP), ALSA, no Subwoofer/Rear/Center output shock_ez Linux - Hardware 4 11-09-2005 11:21 AM
Media Center glensterjonez Linux - Newbie 2 10-01-2005 07:14 AM
Via AC'97 5.1 Optical Output or Audigy 4.1 Output Nza Fedora 3 06-01-2004 07:49 AM
the sound gives output when using mic but no output when run a music file medo Debian 0 04-19-2004 07:17 PM
Command Center fatcpu Mandriva 1 12-02-2003 04:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration