Regular Expressions: Marking Up a Document with sed

1 hour
  • 2 Learning Objectives

About this Hands-on Lab

Regular expressions can be especially powerful when paired with any kind of scripting or text processing language, such as `sed`. In this learning activity, we craft a script to add formatting to a plain-text file using various regular expressions concepts alongside `sed` to create a Latex-friendly document.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Create `sed` script.

Create a sed script that meets all the requirements listed in the instructions.

Convert the example file

Convert mail-forwarding.txt to mail-forwarding.latex with the sed script developed in the hands-on lab.

Additional Resources

You are an employee at EtherCloud, a web hosting company. There has been a push to migrate all internal documentation to Latex, and it has fallen to you to convert some files. A few files in particular are oddly formatted. Using a sed script and the provided example file, mail-forwarding.txt, create a script that completes the following tasks:

  • Add the following to the top of the document:

    documentclass{article}
    usepackage{hyperref}
    begin{document}
  • Encase the first line in a /title{NAME} tag

  • Convert all sections starting with Roman numerals to use section{NAME}; do not keep the Roman numerals (Roman numerals use the following letters: I V X L C D M

  • Encase all lines starting with [root@red-hat ~]# in code blocks (begin{verbatim} and end{verbatim}; note that the ~ can be alternate text

    • There are some instances where two lines start with [root@red-hat ~]# immediately after one another; use sed's N functionality to write this portion of the script
  • Any lines beginning with four spaces or a tab should be contained in code blocks; it is acceptable for each line to have its own code block tags since each line in a different setting

  • Format any file names ending with .cf to use /texttt{TEXT} tags

  • Change anything encased in backtics to use /texttt{TEXT} tags

  • At the end of the document add end{document}; this can be done by using $ a <APPENDED TEXT>

Save the script as latex.sed and the converted file as mail-forwarding.txt.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?