More Regular Expressions: Using Lookarounds to Parse Data

1 hour
  • 3 Learning Objectives

About this Hands-on Lab

While using regular expressions, lookarounds allow us to create zero-width boundaries where the boundaries are also regular expressions. This will enable us to ensure that our primary expression either begins or ends with the provided lookaround or does _not_ begin or end with the provided lookaround. The expression in the lookaround is matched, but not captured, so the resulting match is only the non-lookaround text. This allows us to fine-tune our expressions and ensure we’re only matching what we want.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Remove excess spacing from `highstate` file.

Remove the additional spacing before each line in the highstate file.

Using regular expressions to provide the desired data.

Craft a regular expression that will result in only the __id__, __run_num__, and __sls__ lines from the highstate file.

Create `sls-report.txt`.

Save the newly-generated list of information to sls-report.txt.

Additional Resources

You work with EtherCloud, a hosting provider. EtherCloud utilizes Salt to maintain their infrastructure, and your manager has requested to see information from the last Salt run, or "highstate". However, the raw log will provide too much information for what the manager is looking for. Instead, she requests that you parse down the log file to only contain information about the ID, run number, and state (or sls).

Using lookarounds, take the provided highstate file and match all data in between the separator (----------) but before the changes: line. It may help to remove excess spacing from the file first.

Additionally, since the regular expression needs to cross multiple lines, it is suggested to output the data with the grep flag -z to allow for multi-line matching.

Save the resulting output as sls-report.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?