0

I have something like this;

I like reading **books** and taking **notes** 

I was trying to get something like this;

I like reading <b>books</b> and taking <b>notes</b> 

Tried doing;

sed 's/\*\*\(.*\)\*\*/<b>\1<\/b>/g' a.txt 

This returns;

I like reading <b>books** and taking **notes</b> 

It wraps around the whole line. Any way to get around it?

note: I'm using a GNU version of sed

2
  • There are many existing tools to convert markdown to html, including markdown and mmark and markdown_py. and, of course, pandoc. They each have their own quirks and pros and cons, but using any of them is better than wasting time re-inventing the wheel. Commented Apr 17, 2021 at 3:01
  • @cas appreciate the suggestion. I'm pretty new to regex and thought markdown to html conversion using regex would be a good way to get started. I'm having fun. Commented Apr 18, 2021 at 14:20

1 Answer 1

2

You can do the following with GNU sed:

$ sed 's:\*\*\([^*]*\)\*\*:<b>\1</b>:g' a.txt I like reading <b>books</b> and taking <b>notes</b> 

The \*\*\([^*]*\)\*\* pattern captures the string that is between **...** that is not a wildcard *. Finally you replace the captured group for itself between the tags.

1
  • Brilliant!!!!!! Commented Apr 16, 2021 at 16:53

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.