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?
In reply to Is this the best regex? by drewk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |