drewk has asked for the wisdom of the Perl Monks concerning the following question:
I am using Perl to find all the PowerPC apps on my newly upgraded Snow Leopard laptop. (Love Perl 5.10 on Snow Leopard btw...)
system_profiler SPApplicationsDataType produces a list of all the applications on OS X. There is an XML output option, but I am working with the text version. system_profiler produces output in text like this:
My perl script to parse this is:SystemUIServer: Version: 1.5.5 Last Modified: 8/23/09 8:22 PM Kind: Universal Get Info String: SystemUIServer version 1.5.5, Copyright 2000-20 +09 Apple Computer, Inc. Location: /System/Library/CoreServices/SystemUIServer.app UserNotificationCenter: Version: 3.0.0 Last Modified: 1/29/07 11:03 PM Kind: Universal Location: /System/Library/CoreServices/UserNotificationCenter.ap +p
#!/usr/bin/perl $apps_rep = `system_profiler SPApplicationsDataType 2> /dev/null`; @apps_lines = split(/\n/,$apps_rep) ; @apps=(); $count = @apps_lines ; $i=$j=$k=$p=0; while ($j<$count) { $apps[$i] .= $apps_lines[$j] ; $apps[$i] .= "\n" ; $i++ if ($apps_lines[$j]) =~ /^\s\s\s\s\S.*:$/; $j++; } print "$i apps\n" ; while($k<$i) { $_ = $apps[$k++] ; if (/Kind: PowerPC/s) {print; $p++} ; } print "$i applications, $p PowerPC applications\n\n";
Each application record is delimited by four spaces, the app name, and a colon at the end of the line. The regex /^\s\s\s\s\S.*:$/ captures this delimiter, but I cannot use the regex in split(/^\s\s\s\s\S.*:$/,$apps_rep);. Instead I have to read the output by lines, reassemble the lines into an array, and match /Kind: PowerPC/s on the resulting record.
Any wisdom on why I cannot use the split(/^\s\s\s\s\S.*:$/,$apps_rep);. call? The regex works, but not in split? Any better way to do this?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Is this the best regex?
by almut (Canon) on Sep 08, 2009 at 22:18 UTC | |
by ikegami (Patriarch) on Sep 09, 2009 at 00:32 UTC | |
by Anonymous Monk on Sep 09, 2009 at 04:54 UTC | |
by Anonymous Monk on Sep 09, 2009 at 04:59 UTC | |
by Anonymous Monk on Sep 09, 2009 at 05:03 UTC | |
|
Re: Is this the best regex?
by jwkrahn (Abbot) on Sep 09, 2009 at 01:54 UTC | |
by Anonymous Monk on Sep 09, 2009 at 04:51 UTC | |
|
Re: Is this the best regex?
by halfcountplus (Hermit) on Sep 08, 2009 at 22:27 UTC | |
by Anonymous Monk on Sep 09, 2009 at 04:57 UTC | |
|
Re: Is this the best regex?
by toolic (Bishop) on Sep 08, 2009 at 23:24 UTC |