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
# 8  
Old 02-03-2011
Code:
w=$(( $COLUMNS / 2 - 20 ))
cal | while IFS= read -r line
do
  printf "%${w}s %s\n" '' "$line"
done

This User Gave Thanks to cfajohnson For This Post:
# 9  
Old 02-03-2011
Quote:
Originally Posted by Scrutinizer
Do you mean a little bit to the left or all the way to the left?
All the way to the left.
And so was the code by cfajohnson.

I really like the idea of setting the IFS to newline, but this is how I imagine the problem:

In my code let newline={/n}
Here is the last two lines of Feb 2011:
Code:
20 21 22 23 24 25 26{/n}
27 28{/n}

And I think it should be:
Code:
20 21 22 23 24 25 26{/n}
27 28               {/n}

# 10  
Old 02-03-2011
Hi, as you stated yourself in the first post this will not run in a script (COLUMNS will not be available there). You need to source it from a file:
Code:
. file

or run it directly in your interactive shell.
Another option would be to pass $COLUMNS as a parameter to the script...
This User Gave Thanks to Scrutinizer For This Post:
# 11  
Old 02-03-2011
duh, I knew that, that was last night and I was tired.
Now the code from cfajohnson works I just need to tweak the number 20.

Thanks to all that helped.

EDIT: This works!
Code:
w=$(( $COLUMNS / 2 - 10 ))
cal | while IFS= read -r line
do
  printf "%${w}s %s\n" '' "$line"
done


Last edited by AlphaLexman; 02-03-2011 at 09:29 AM..
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