Basic Regular Expressions: Working with Hardware Data
Content Team Lead in Content
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.
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:
We can now translate this into regex:
Let's now test it to make sure it works. Before we save anything to a file, we can simply
grepthis expression and see what comes out:
grep -P 'E3-12ddVd' intel-cpus.csv
Now that we know everything looks right, let's send it to a file:
grep -P 'E3-12ddVd' intel-cpus.csv > intel-options.csv
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]
We can now translate this to regular expressrions. Note that since
walso matches for digits, we do not have to do anything special for the last token in the expression:
Let's test with
grep -P 'Gwddd-ddd-ww' gpus.csv
Note that this list is particularly large.
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!