2

How do I use sed to substitute date by a word. I tried doing this:

tmp=`date +%d`
echo $tmp
if [ $tmp -lt 10 ]
then
        pat="d"
        echo $pat
else
        pat="dd"
        echo $pat
fi
cal | sed "s/$tmp/$pat/"

And the output is like so:

robin@robin-VirtualBox:~/lx$ bash p13.sh
27
dd
     April 2014       
Su Mo Tu We Th Fr Sa  
       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           

Edit:

I want the date 27 to change into dd, like in here:

     April 2014       
Su Mo Tu We Th Fr Sa  
       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  
dd 28 29 30           
4
  • Sorry, but I don't finish to understand what are you trying to achieve. How should the output be like? – Lucio Apr 27 '14 at 3:47
  • @Lucio Hello, I have edited my question. Please have a look at it. Thank you. – Robin Apr 27 '14 at 3:51
  • 1
    You're right, the script works for anything but 27 specifically, weird. I'm assuming there is some issue with the current day. – Lucio Apr 27 '14 at 4:02
  • Yes... Its working with other dates! LOL! – Robin Apr 27 '14 at 4:06
3

TL;DR: Use -h when using cal

Why?

The cal command prints the current day with special characters ("highlighting"?). I found out this saving it to a file and using nano (not cat) to read it:

$ cal >> out
$ nano out

Now you will see the following:

     Abril 2014
do lu ma mi ju vi 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
_^H2_^H7 28 29 30

Beside Spanish (sorry) 27 is not with simple characters.

Now if you use -h this is solved:

$ cal -h >> out2
$ nano out2

Your output should be like this so you can parse it without problems:

     Abril 2014
do lu ma mi ju vi 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
27 28 29 30

Solution

In your script replace cal | sed "s/$tmp/$pat/" with cal -h | sed "s/$tmp/$pat/"


Source: http://netbsd.gw.com/cgi-bin/man-cgi?cal+1+NetBSD-current

4

Apparently you cannot parse the output of BSD cal for the current day. This is because it's highlighted. If you turn off highlighting with the -h switch, it will work:

➜  ~  cal -h | sed "s/$tmp/$pat/"
     Abril 2014       
do lu ma mi ju vi 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  
dd 28 29 30           

To identify the output of cal, check the strace:

strace cal
[...] # tons of lines here
write(1, "       1  2  3  4  5  \n", 23       1  2  3  4  5  
) = 23
write(1, " 6  7  8  9 10 11 12  \n", 23 6  7  8  9 10 11 12  
) = 23
write(1, "13 14 15 16 17 18 19  \n", 2313 14 15 16 17 18 19  
) = 23
write(1, "20 21 22 23 24 25 26  \n", 2320 21 22 23 24 25 26  
) = 23
write(1, "\33[7m27\33[27m 28 29 30           \n", 3227 28 29 30           
) = 32
write(1, "                      \n", 23                      
) = 23
exit_group(0)                           = ?

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.