0

How to use commands, cal, sed and tr and the pipe to display the numerical days (e.g., 1, 2, 3, etc.) of a month, with each day on a separate line

I've tried:

cal 12 2015| sed '3,7p' | tr -cs "[:alpha:]" "[\n*]" 

and go this output:

December
S
M
Tu
W
Th
F
S
  • With homework questions, you need to show us what you have tried... – jasonwryan Nov 26 '15 at 19:18
4

You came close.

  1. You need to suppress sed's output before selectively printing lines:

    $ cal 12 2015 | sed -n '3,7p'                             
           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  
    27 28 29 30 31       
    

    Your sed might not have -n. In which case, use d to delete all lines except the ones you want.

  2. Your tr command replaces everything except alpahabets - and that includes digits. Just replace spaces:

    $ cal 12 2015 | sed -n '3,7p' | tr -s ' ' '\n'                
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    
2

The range of lines in the sed command by @muru caught my eye. You can do this relying less upon the format of cal:

cal 12 2015 | sed '/[[:alpha:]]/d' | tr ' ' '\n' |sed -e '/^$/d'

POSIX does not specify the format for cal, so the length of the header could vary by implementation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.