Center output of 'cal' command in terminal


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Center output of 'cal' command in terminal
# 1  
Old 02-02-2011
Center output of 'cal' command in terminal

I have a function to center output in a terminal. However It only works for variables. I would like to center the output of the command 'cal' or 'cal -3' in a terminal.

I have tried:
  • center `cal`
  • CAL=`cal`; center $CAL
  • cal > cal.txt; center `cat cal.txt`
  • center "`exec cal`"
To no avail, but the closest I've got is this:
Code:
cal | sed  -e :a -e 's/^.\{1,77\}$/ & /;ta'

which still messes up the last line | week, and only works for a preset terminal width ($COLUMNS=79)

Here is my function center ()
Code:
	function center ()

	{
     
		VALUE=${#1}

		if [[ $VALUE -lt $COLUMNS ]] ; then

			WIDTH=$(( (  $COLUMNS - ${#VALUE} ) ))
			printf "%*s\n" $(( ( (WIDTH + ${#1}) / 2 ) + 1 )) $"$1"

		else

			echo "$1"

		fi

	}

How can I make `cal` be centered in terminal regardless of xterm width?
# 2  
Old 02-02-2011
Have a look at man tput
# 3  
Old 02-02-2011
Hi AlphaLexman,

Your 'sed' line could be useful, using the environment variable $COLUMNS and single quotes:
Code:
cal | sed  -e :a -e "s/^.\{1,$COLUMNS\}$/ & /;ta"

Regards,
Birei
# 4  
Old 02-02-2011
@ Scutinizer: Hey thanks for the quick reply, I know that the variable $COLUMNS does not work in a script, it does work in a function. COLS=$(tput cols) will give the same value as $COLUMNS.

---------- Post updated at 03:55 PM ---------- Previous update was at 03:48 PM ----------

@ birei: Your code was very similar to my 'sed' command, except it seemed to be double spaced, and the last line | week was still centered and not under the day of week!

FYI: I am on Fedora 13 with bash 4.1.7
# 5  
Old 02-02-2011
Try:
Code:
center() {
  while IFS= read -r line
  do
    printf "%$(((COLUMNS+${#line})/2))s\n" "$line"
  done
}
cal|center

# 6  
Old 02-02-2011
Quote:
Originally Posted by Scrutinizer
Try:
Code:
center() {
  while IFS= read -r line
  do
    printf "%$(((COLUMNS+${#line})/2))s\n" "$line"
  done
}
cal|center

That was still on the left side of the terminal, Thanks, though!
# 7  
Old 02-03-2011
Do you mean a little bit to the left or all the way to the left?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print Terminal Output Exactly how it Appears in the Terminal to a New Text File

Hello All, I have a text file containing output from a command that contains lots of escape/control characters that when viewed using vi or view, looks like jibberish. But when viewed using the cat command the output is formatted properly. Is there any way to take the output from the cat... (7 Replies)
Discussion started by: mrm5102
7 Replies

2. UNIX for Dummies Questions & Answers

Cal command to display 5 months ?

Hi, I'm curious to know if we can display 5 months of a calendar using the cal command. I know we can three successive months (cal -3) but I wanted to know if we can do it with 5 months for example. (Give a specific month, and get as a result two previous months + the month in question + two... (12 Replies)
Discussion started by: Imane
12 Replies

3. UNIX for Dummies Questions & Answers

Cal command in UNIX

modify "cal " command to display calenders of the specified months. $ cal jan....aug (1 Reply)
Discussion started by: ssaini
1 Replies

4. Shell Programming and Scripting

how to Redirect the output of telnet command on a terminal to a file ?

(/home/user1)-> more script.sh #!/bin/ksh ( echo open devicename sleep 3; echo user; sleep 2; echo password; sleep 2; echo "/info/dump"; ---------> This needs to redirect to a file .Can be number of pages sleep 2; echo "exit" ) | telnet Please use code tags next time for... (2 Replies)
Discussion started by: necro98
2 Replies

5. Shell Programming and Scripting

another cal command question.

I got this from this board yesterday cal | xargs -n1 | tail -1 which displays the current months days.. for instance if you type this in a shell today you will get 31. I would like to also display the month and year.. something like March 2011 has 31 days. how would I do that? ... (3 Replies)
Discussion started by: rontopia
3 Replies

6. UNIX for Dummies Questions & Answers

cal command

Hello, I wanted to display calender for the previou, current and next month in a single command... I used the command cal -3 for this. But its throwing me a Bad Argument error. I am using HP UX to execute this command. Is this a syntax error, or let me know if there any other ways to... (6 Replies)
Discussion started by: atlantis
6 Replies

7. Shell Programming and Scripting

Getting a specific date from cal output with AWK

Hi guys! I'll make this short... Is there any good way to get the day number that first matches the Monday column from the cal command output with awk (or any other text manipulator commands) ? I'm sorry if my question wasn't clear at all. For example... One cal output would be $... (6 Replies)
Discussion started by: Casey
6 Replies

8. Shell Programming and Scripting

Formating cal output

Hi Gurus, In my Cal output i want to cut the date of 2nd saturday how tyo achive this. for eg in the below output i need that second saturday 13 to be cut. crypto $ cal January 2007 S M Tu W Th F S 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26... (2 Replies)
Discussion started by: Krrishv
2 Replies

9. AIX

doubt in cal command

I am new to unix... How to get all the saturdays of a specific year? for a specific month, i tried as below.. cal 02 2006 | awk '{print $7}' but it is not giving all saturdays.... can anyone help me with this? Thanks in advance, Sumi (9 Replies)
Discussion started by: sumi
9 Replies

10. UNIX for Dummies Questions & Answers

Cal command

I am trying to configure the cal command to recognize the month names. When you type: cal - you get the calander for the current month of the current year. Is there a way of making the system recognize March, and Mar. So I could type: cal March or cal mar and get the same response as cal.... (5 Replies)
Discussion started by: Astudent
5 Replies
Login or Register to Ask a Question