Basic Regular Expressions: Working with Hardware Data

Hands-On Lab

 

Photo of Elle Krout

Elle Krout

Content Team Lead in Content

Length

00:30:00

Difficulty

Beginner

Regular expressions allow us to take the most complicated pieces of text and match the patterns within them for a variety of results, but to learn regular expressions we have to start somewhere. Using a combination of literals, digits, and word characters, alongside some basic text processing commands (such as grep), we can match product IDs and architectures and parse down a large product list into something more manageable.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Intel CPUs

  1. We first want to break down the expressions we need to match. This one is fairly easy, with much of it being a literal match. In this instance, each token is encased in square brackets:

    [E][3][-][1][2][digit][digit][V][digit]
  2. We can now translate this into regex:

    E3-12ddVd
  3. Let's now test it to make sure it works. Before we save anything to a file, we can simply grep this expression and see what comes out:

    grep -P 'E3-12ddVd' intel-cpus.csv
  4. Now that we know everything looks right, let's send it to a file:

    grep -P 'E3-12ddVd' intel-cpus.csv > intel-options.csv

GPUs

  1. As with the CPUs, we want to look at our example matches and break them down:

    [G][word][digit][digit][digit][-][digit][digit][digit]-[word][word or digit]
  2. We can now translate this to regular expressrions. Note that since w also matches for digits, we do not have to do anything special for the last token in the expression:

    Gwddd-ddd-ww
  3. Let's test with grep:

    grep -P 'Gwddd-ddd-ww' gpus.csv

    Note that this list is particularly large.

  4. Now send the output to a file:

    grep -P 'Gwddd-ddd-ww' gpus.csv > gpu-options.csv

Do note that these are only two options for this learning activity; you may come up with something different that works just as well!