正在将应用程序日志文本文件解析为数组



好的,这就是问题所在。我正在尝试获取一系列已安装的应用程序和版本。

到目前为止,我发现我可以运行以下命令:

system_profiler SPApplicationsDataType > log.txt

这将给我一个如下格式的txt文件:

Applications:
MacTubes:
  Version: 3.1.5
  Obtained from: Unknown
  Last Modified: 9/15/12, 2:41 AM
  Kind: Universal
  64-Bit (Intel): No
  Location: /Volumes/Storage/MacTubes.app
Boom:
  Version: 1.6
  Obtained from: Identified Developer
  Last Modified: 10/12/12, 3:24 AM
  Kind: Intel
  64-Bit (Intel): No
  Signed by: Developer ID Application: Global Delight Technologies Pvt. Ltd, Developer ID Certification Authority, Apple Root CA
  Location: /Volumes/Storage/Boom.app
  Get Info String: 1.6  Copyright © 2008 - 2012, Global Delight Technologies Pvt. Ltd.
World of Goo:
  Version: 1.30
  Obtained from: Unknown
  Last Modified: 2/8/10, 8:43 AM
  Kind: Universal
  64-Bit (Intel): No
  Location: /Volumes/Storage/Misc/World of Goo.app
  Get Info String: 1.30, Copyright © 2008 2D Boy LLC
VZAccess Manager:
  Version: 4.3.0
  Obtained from: Unknown
  Last Modified: 12/17/09, 8:48 PM
  Kind: Universal
  64-Bit (Intel): No
  Location: /Volumes/Storage/Misc/VZAccess Manager.app
  Get Info String: VZAccess Manager 4.3.0 Copyright © 2000-2008 Smith Micro Software, Inc.
ColorSync:
  Obtained from: Unknown
  Last Modified: 10/9/00, 12:00 PM
  Kind: Classic
  Location: /Volumes/Storage/Misc/System Folder/Control Panels/ColorSync
AppleTalk:
  Obtained from: Unknown
  Last Modified: 10/1/96, 12:00 PM
  Kind: Classic
  Location: /Volumes/Storage/Misc/System Folder/Control Panels/AppleTalk

我希望能够获得应用程序名称和版本号,并以某种方式将它们存储在数组中。我不知道从哪里开始,因为所有的条目在我看来都一样?我也需要忽略没有版本号的条目。

这里有一个Awk脚本,它将为您提供您提到的输出格式:

/^ {4}.+:$/ {
    sub(/^ {4}/, "", $0)
    last_application = $0
}
/^ +Version:/ {
    sub(/^ +Version: /, "", $0)
    print last_application " " $0
}

如果您将其保存为script.awk,则可以运行

awk -f script.awk log.txt

以获得类似的输出

MacTubes: 3.1.5
Boom: 1.6
World of Goo: 1.30
VZAccess Manager: 4.3.0

这已经用OSX10.09.0的CCD_ 2进行了测试。

相关内容

  • 没有找到相关文章

最新更新