rimmern has asked for the wisdom of the Perl Monks concerning the following question:

I need to split the out from lspci, which looks like this:

00:1f.3 SMBus: Intel Corporation 82801BA/BAM SMBus (rev 12)
00:1f.4 USB Controller: Intel Corporation 82801BA/BAM USB (Hub #2) (rev 12)
00:1f.5 Multimedia audio controller: Intel Corporation 82801BA/BAM AC'97 Audio (rev 12)

I figured I can split on the first " " and then split again on the ":".

But I cant figure out how to just split the string on the first " ".

Do I need a regex ?

Replies are listed 'Best First'.
Re: splitting lspci output
by Paladin (Vicar) on May 17, 2006 at 22:58 UTC
    From perldoc -f split:

    If LIMIT is specified and positive, it represents the maximum number of fields the EXPR will be split into

    So if you want to split it into just 2 fields, you give that as the limit

Re: splitting lspci output
by GrandFather (Saint) on May 17, 2006 at 23:07 UTC

    What do you actually want to achieve? Show us an example of the result you require because our mind reading units tend to be a bit flakey and don't always let us know what it is that you want.

    It may be that you want this:

    use strict; use warnings; while (<DATA>) { chomp; next if ! length; my ($section, $device, $culprit, $rev) = m/(\S+)\s+ ([^:]+):\s+ ((?:(?!\s*\(rev).)+)\s* (.*)/x; next if ! defined $rev; print "$section, $device, $culprit, $rev\n"; } __DATA__ 00:1f.3 SMBus: Intel Corporation 82801BA/BAM SMBus (rev 12) 00:1f.4 USB Controller: Intel Corporation 82801BA/BAM USB (Hub #2) (re +v 12) 00:1f.5 Multimedia audio controller: Intel Corporation 82801BA/BAM AC' +97 Audio (rev 12)

    Prints:

    00:1f.3, SMBus, Intel Corporation 82801BA/BAM SMBus, (rev 12) 00:1f.4, USB Controller, Intel Corporation 82801BA/BAM USB (Hub #2), ( +rev 12) 00:1f.5, Multimedia audio controller, Intel Corporation 82801BA/BAM AC +'97 Audio, (rev 12)

    DWIM is Perl's answer to Gödel
      hi

      m/(\S+)\s+ ([^:]+):\s+ ((?:(?!\s*\(rev).)+))\s* (.*)/x;
      what does the above pattern match do? what is that 'x' stands for ? could you please explain ?

        The x option indicates that most white space should be ignored. That allows me to use spaces to break up the regex a little to make it easier to see where each field is.

        The first field is captured by (\S+)\s+ which grabs the first sequence of non-space characters from the start of the line.

        The second field is captured by ([^:]+):\s+ which matches and captures a sequence of characters excluding ':', then matches the ':' and any following white space.

        The third field is a little more interesting. ((?:(?!\s*\(rev).)+))\s* crawls along the remainder of the line matching and capturing one character at a time until it comes to '(rev' preceeded by any amount of white space. The (?:...) bit groups some stuff without capturing. The (?!...) bit looks ahead to make sure the following stuff doesn't match whatever '...' is. The '...' bit inside of (?:...) matches a single character so long as it's not the start of something that would match '\s*\(rev'.

        The fourth field just picks up everything from '(Rev' to the end of the line.


        DWIM is Perl's answer to Gödel