0

I'm trying to replace _ _1 or possibly _3_1 with ★, anywhere in the text. So it can be a whitespace or a digit between to underscores and in the end it's always a digit.

So far I only can replace a number and everytime I try to add more it stops working.

This is the line that's not working:

sed -e 's/ [0-9] /★/g' |

The updated whole code:

echo
echo `cal` | 

sed "s/$(date +%e) / $(date +%e | sed 's/.*/★/g') /" | 

sed s'/Su Mo Tu We Th Fr Sa//g' | 

sed s'/  */  \|  /g' | 

sed 's/_[ 0-9]_[0-9]/★/g'|


sed s'/^  \|//' | sed s'/  \|//' | sed s'/\|/:  /' | 

sed s'/\|//g' | sed s"/$/      /"

Here is the original output: Original output

Here is the output after my code and the underscores are still there: enter image description here

Original string is coming from 'cal', for some reason is puts a _ _ on the current day. If it's 2.september is says _ _2 and if it's the 30th is says _3_0:

"September 2018 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"

My desired output A star that replaces the current day.

"September 2018: ★ 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"
3
  • Please add sample input and your desired output for that sample input to your question. Commented Sep 1, 2018 at 13:53
  • @Cyrus I updated the post now, hope it's more descriptive! Commented Sep 1, 2018 at 14:24
  • At least on OS X, echo $(cal) | od -a shows that today's date has a backspaces on the LH side it: _\b_\b1 Commented Sep 1, 2018 at 15:09

2 Answers 2

2

If you examine the output of cal with od you can see there are backspaces that you should remove with the _ in front of today's date:

$ echo $(cal) | od -a
0000000    S   e   p   t   e   m   b   e   r  sp   2   0   1   8  sp   S
0000020    u  sp   M   o  sp   T   u  sp   W   e  sp   T   h  sp   F   r
0000040   sp   S   a  sp   _  bs  sp   _  bs   1  sp   2  sp   3  sp   4
0000060   sp   5  sp   6  sp   7  sp   8  sp   9  sp   1   0  sp   1   1
0000100   sp   1   2  sp   1   3  sp   1   4  sp   1   5  sp   1   6  sp
0000120    1   7  sp   1   8  sp   1   9  sp   2   0  sp   2   1  sp   2
0000140    2  sp   2   3  sp   2   4  sp   2   5  sp   2   6  sp   2   7
0000160   sp   2   8  sp   2   9  sp   3   0  nl  

Today is the 1 and you can see the sequence _ bs sp _ bs 1 in the output above.

To replace today's date with a it is easily done with awk by replacing those 5 characters and the date number:

$ echo $(cal) | awk -v t=$(echo $(date +%e)) 'sub("_[\b] _[\b]"t,"★")' 
September 2018 Su Mo Tu We Th Fr Sa ★ 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

Easier, you can use cal -h which turns off the highlight of today's date and then just do:

$ echo $(cal -h) | awk -v t=$(echo $(date +%e)) 'sub(" " t " "," ★ ")'
September 2018 Su Mo Tu We Th Fr Sa ★ 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

Or with sed also in this case:

$ echo $(cal -h) | sed -E "s/ $(echo $(date +%e)) / ★  /" 
September 2018 Su Mo Tu We Th Fr Sa ★  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

sed is not as easily used as awk in the first case because it lacks consistent support for escaped characters such as \b (other than GNU sed).

Sign up to request clarification or add additional context in comments.

Comments

2

You can use:

_[ 0-9]_[0-9]

Regex demo here.

$ echo "_ _1 _3_1" | sed 's/_[ 0-9]_[0-9]/★/g'
★ ★

As per the update:

$ echo "September 2018 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" | sed -r 's/ ([A-Za-z]{2} ){7}(_[ 0-9]_[0-9])/: ★/g'
September 2018: ★ 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

Regex demo here.

2 Comments

Thank you! but this doesn't seem to work in my code, maybe I'm mis-interpreting the whitespace. You can see my updated code with screenshots now!
You have added several more lines of code. It would be helpful if you provided your original string and desired output. Also, it's easier to work with text rather than images.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.