2

I have a file containing text

**This is bold text** 
*This is italicized text*

I wish to convert the above into

  \textbf{This is bold text}
  \emph{This is italicized text}

using stream editor(sed). Kindly guide me on this matter.

I have used this code

sed -i 's/\*\*\(.*\)\*\*/\\textbf{\1}/g' SampleCode 

and it works like a charm

Thanks from Google group

pasmon
  • 51
  • 4
  • 2
    Do the texts ever span more than a single line? – steeldriver Jan 23 '20 at 19:08
  • Let's break this down into smaller chunks. What text do you wish to find, and what do you want to convert it to? Looks like you wish to find the first instance of two asterisks and replace it with ' \textbf{' - find the second instance of two asterisks and replace it with '}' - find the first instance of one asterisk and replace it with '\emph{' - find the second instance of one asterisk and replace it with '}' . Is that correct? – K7AAY Jan 23 '20 at 19:35

2 Answers2

4

It looks like you are trying to invent new Pandoc.
Instead:

  1. Install Pandoc:

    sudo apt-get install pandoc
    
  2. Create input file:

    $ cat << EOF > file.md
    **This is bold text** 
    *This is italicized text*
    EOF
    
  3. Run conversion from Markdown to LaTeX:

    pandoc file.md -o file.tex
    
  4. Enjoy the result:

    $ cat file.tex 
    \textbf{This is bold text} \emph{This is italicized text}
    
dessert
  • 39,392
  • 12
  • 115
  • 163
N0rbert
  • 97,162
  • 34
  • 239
  • 423
0

This is the simple answer:

echo '**This is bold text**' | sed -r 's/\*\*(.+)\*\*/\\textbf{\1}/'
echo '*This is italicized text*' | sed -r 's/\*(.+)\*/\\emph{\1}/'

Most important thing is to understand regular expressions. There are several online regex testers where you can experiment with regex.

Here is a very good explanation of sed. https://www.grymoire.com/Unix/Sed.html

Thomas Aichinger
  • 2,746
  • 5
  • 23
  • 48
  • I wish to use these commands used in a script file(.sh) to work on a global scale in a document . How to convert the commands listed above for that purpose? – pasmon Jan 23 '20 at 20:32
  • 2
    fails for `**This** is bold **text**` – oh, and what about `**mixed *italicized* and bold**`, and multiline blocks? there are so many things to consider here, a tool like `pandoc` is the only sane solution IMO. – dessert Jan 23 '20 at 21:19