Text centering

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Text centering
# 1  
Old 10-24-2010
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, scripts, algorithms:

None

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

None

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

Vilnius University, Vilnius, Lithuania, A. Mitašiūnas, 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 Smilie
# 2  
Old 10-24-2010
You can center the text by using the printf 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 wc or, if you're using bash, by the parameter expansion ${#variable}.

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

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

Last edited by pludi; 10-26-2010 at 06:37 PM..
# 3  
Old 10-26-2010
Quote:
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
# 4  
Old 10-26-2010
Righty-o, typo on my side. Thanks
# 5  
Old 10-28-2010
Thank you, I'll try this Smilie
# 6  
Old 10-31-2010
It's part of my program. Still missing centering by the screen. Is it ok so far?


Code:
#!/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?

Last edited by pludi; 10-31-2010 at 10:17 AM.. Reason: code tags please
# 7  
Old 10-31-2010
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies

2. Shell Programming and Scripting

awk to skip lines find text and add text based on number

I am trying to use awk skip each line with a ## or # and check each line after for STB= and if that value in greater than or = to 0.8, then at the end of line the text "STRAND BIAS" is written in else "GOOD". So in the file of 4 entries attached. awk tried: awk NR > "##"' "#" -F"STB="... (6 Replies)
Discussion started by: cmccabe
6 Replies

3. UNIX for Dummies Questions & Answers

Search String, Out matched text and input text for no match.

I need to search a string for some specific text which is no big deal using grep. My problem is when the search fails to find the text. I need to add text like "na" when my search does not match. I have tried this command but it does not work when I put the command in a loop in a bash script: ... (12 Replies)
Discussion started by: jojojmac5
12 Replies

4. Shell Programming and Scripting

Read text between regexps and write into files based on a field in the text

Hi, I have a huge file that has data something like shown below: huge_file.txt start regexp Name=Name1 Title=Analyst Address=Address1 Department=Finance end regexp some text some text start regexp Name=Name2 Title=Controller Address=Address2 Department=Finance end regexp (7 Replies)
Discussion started by: r3d3
7 Replies

5. Shell Programming and Scripting

Grep text matching problem with script which checks if web page contains text.

I wrote a Bash script which checks to see if a text string exists on a web page and then sends me an email if it does (or does not e.g. "Out of stock"). I run it from my crontab, it's quite handy from time to time and I've been using it for a few years now. The script uses wget to download an... (6 Replies)
Discussion started by: gencon
6 Replies

6. UNIX for Dummies Questions & Answers

Centering a line

So I am trying to use the .ce 3 command to center 3 lines in my text, but I am getting an execute permission denied. I made rwx capabilites for all users too. Any idea why I am getting this error? (15 Replies)
Discussion started by: itech4814
15 Replies

7. 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

8. Shell Programming and Scripting

[bash help]Adding multiple lines of text into a specific spot into a text file

I am attempting to insert multiple lines of text into a specific place in a text file based on the lines above or below it. For example, Here is a portion of a zone file. IN NS ns1.domain.tld. IN NS ns2.domain.tld. IN ... (2 Replies)
Discussion started by: cdn_humbucker
2 Replies
Login or Register to Ask a Question