I can get the data using sudo lspci -vvv but then I have to parse it, and it gets a bit tricky...

I can grep the relevant PCIe name I need, and then use -A to take N lines after the name, but it isn't robust. I can also split the outcome by \n\n and then process it, grepping the relevant pcie name, but again, feels not good.

cut or tr doesn't work with strings AFAIK, so I can't process using delimiter \n\n and so I feel a bit stuck

1 Answer 1


The easiest way is with a regular expression that can match multiple lines of a string that consists of the whole output of the lspci command. There may be a command to do this, but I don't know of it, so typically you have to do this from a language such as perl or Python. For brevity, here is an example perl command to extract just the flag NonFatalErr for the "Root Complex" capability of my audio chipset.

#!/usr/bin/env perl
use strict;
my $data = join('',<>);
if ( $data =~ m{
 Audio\ device:
 .*?Capabilities:[^\n]*Root\ Complex
 }sx ){
        print "found DevSta: $1 !\n";

Put this in a file myprog, do chmod +x myprog, and pipe the lspci output into it.

Regular expressions are a big subject which cannot be explained here, but are worth learning about as they are very powerful.

The above perl reads all the input into a single string $data. Then a match m{regex}flags is done on it with perl flag s to make "." match newlines, and flag x to ignore whitespace in the regex.

"Audio\ Device" matches "Audio Device" in the data, and .*? skips over the minimal number of characters ("non-greedy") until the rest of the regex matches. One of several "Capabilities:" is found, but we continue until we find the one with "Root Complex" with no intervening newlines ([^\n]*). There are several "NonFatalErr" strings, but we want the one for DevSta. We capture the string (using ()) plus one following character (NonFatalErr.) which is the flag's value (+ or -). We print the capture which perl saved in variable $1.

This is a proof of concept, not a tutorial, so please use it as motivation for your own solution.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .