Centering a line


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Centering a line
# 8  
Old 05-07-2012
:ce works in vim. Is your editor vim?
# 9  
Old 05-07-2012
If you are trying to center output, take a look at this function. I don't know where I got it as it's been in my bag of tricks for ages. Works in ksh93 on Solaris.

Code:
$ cat print_centered.ksh
#!/usr/dt/bin/dtksh

print_centered(){
  integer screen_width=$(stty|grep columns|cut -f2 -d';'|cut -f2 -d'=')
  integer text_length=${#1}
  integer pad=$((($screen_width - $text_length)/2 ))
  format="%${pad}s%s"
  printf "$format\n" " " "$1"
}

print_centered "center this text please"

exit 0

$ print_centered.ksh
                            center this text please
$

# 10  
Old 05-07-2012
I just have vi editor running....

---------- Post updated at 05:58 PM ---------- Previous update was at 12:09 PM ----------

gary...

when I run your script, it says `(' unexpected

Am I missing something?
# 11  
Old 05-07-2012
What shell are you using?
# 12  
Old 05-08-2012
bash shell
# 13  
Old 05-08-2012
At the bash prompt...
Code:
$ cat file1
Phone Book
Company
Phone Number

$ COLUMNS=179

$ while read TEXT; do printf '%*s\n'  $(((($COLUMNS-${#TEXT})/2)+${#TEXT})) "$TEXT"; done < file1
                                                                                    Phone Book
                                                                                      Company
                                                                                   Phone Number

$

# 14  
Old 05-08-2012
This works in bash on my system. All I did was remove the "integer" before the variable definitions.

Note too that getting the width from the stty command allows the function to work dynamically. I can change the width of my putty window and the text will still be centered in window when run.

Code:
#!/bin/bash

print_centered(){
  screen_width=$(stty|grep columns|cut -f2 -d';'|cut -f2 -d'=')
  text_length=${#1}
  pad=$((($screen_width - $text_length)/2 ))
  format="%${pad}s%s"
  printf "$format\n" " " "$1"
}

print_centered "center this text please"

exit 0

 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

2. Shell Programming and Scripting

How to read file line by line and compare subset of 1st line with 2nd?

Hi all, I have a log file say Test.log that gets updated continuously and it has data in pipe separated format. A sample log file would look like: <date1>|<data1>|<url1>|<result1> <date2>|<data2>|<url2>|<result2> <date3>|<data3>|<url3>|<result3> <date4>|<data4>|<url4>|<result4> What I... (3 Replies)
Discussion started by: pat_pramod
3 Replies

3. Shell Programming and Scripting

Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line...

Hello, I need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... An example of entries in the file would be: SRVXPAPI001 ERRO JUN24 07:28:34 1775 REASON= 0000, PROCID= #E506 #1065: TPCIPPR, INDEX= 003F ... (8 Replies)
Discussion started by: Ferocci
8 Replies

4. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

5. Shell Programming and Scripting

how to read the contents of two files line by line and compare the line by line?

Hi All, I'm trying to figure out which are the trusted-ips and which are not using a script file.. I have a file named 'ip-list.txt' which contains some ip addresses and another file named 'trusted-ip-list.txt' which also contains some ip addresses. I want to read a line from... (4 Replies)
Discussion started by: mjavalkar
4 Replies

6. Solaris

Line too long error Replace string with new line line character

I get a file which has all its content in a single row. The file contains xml data containing 3000 records, but all in a single row, making it difficult for Unix to Process the file. I decided to insert a new line character at all occurrences of a particular string in this file (say replacing... (4 Replies)
Discussion started by: ducati
4 Replies

7. Homework & Coursework Questions

Text centering

Hello, people, 1. The problem statement, all variables and given/known data: I have to make a tool that is centering the given text by the screen width or given parameter. I have no idea how to write this script. Can anyone make it for me ir at least help? 2. Relevant commands, code,... (7 Replies)
Discussion started by: LimitedWings
7 Replies

8. Shell Programming and Scripting

Text centering

Hello, people, I have to make a tool that is centering the given text by the screen width or given parameter. I have no idea how to write this script. Can anyone make it for me ir at least help? (4 Replies)
Discussion started by: LimitedWings
4 Replies

9. Shell Programming and Scripting

[Solved] Problem in reading a file line by line till it reaches a white line

So, I want to read line-by-line a text file with unknown number of files.... So: a=1 b=1 while ; do b=`sed -n '$ap' test` a=`expr $a + 1` $here do something with b etc done the problem is that sed does not seem to recognise the $a, even when trying sed -n ' $a p' So, I cannot read... (3 Replies)
Discussion started by: hakermania
3 Replies
Login or Register to Ask a Question