0

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

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

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

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

4

1 回答 1

1

这是一个 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

这已经通过/usr/bin/awkOS X 10.9.0 的测试。

于 2013-12-11T16:53:52.233 回答