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?

  1. Relevant commands, code, scripts, algorithms:

None

  1. The attempts at a solution (include all code and scripts):

None

  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

Vilnius University, Vilnius, Lithuania, A. Mita�inas, 2nd course

It's not a serious task, it is worth just 0,5 of 10. Of course, if I get the code, I would analyse it all and learn every single symbol meaning. But I don't even know how to start writing the code. There was only one practise lecture these year and the lecturer didn't really tought us anything. And there he gave us the task.
Please, help :slight_smile:

You can center the text by using the man printf (POSX) utility with the %s format specification. How far you have to pad it would depend on the length of the string, which you can get either through man wc (POSIX) or, if you're using bash, by the parameter expansion ${#variable}.

The screen size can be read from the man stty (POSIX) utility, by running it as stty size .

The formula for the padding itself is just (screen width - length of text) / 2.

I think that should be (screen_width - length_of_text)/2

Righty-o, typo on my side. Thanks

Thank you, I'll try this :slight_smile:

It's part of my program. Still missing centering by the screen. Is it ok so far?

#!/bin/sh

echo "What is the file name:"

read FILE

echo "Set X"

read X

echo "Set Y"

read Y

paste $FILE| sed -e :a -e 's/^.\{'$X,$Y'\}$/ & /;ta'

How can I center by the screen?

Hi, you cannot do this with a sed statement like that. You first have to calculate how many lines to skip and how many spaces to pad. Then skip the number of lines and then pad every line at the front before writing it.

Ok, I get numbers with wc and stty. How do I use it as variables for formula?