Sunday, May 5, 2024
 Popular · Latest · Hot · Upcoming
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 1853  / 2 Years ago, thu, november 25, 2021, 9:35:21

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

More From » bash

 Answers
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


[#25632] Friday, November 26, 2021, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ainsbeave

Total Points: 280
Total Questions: 98
Total Answers: 105

Location: Faroe Islands
Member since Sat, Aug 20, 2022
2 Years ago
;