Parsing an application log text file into an array.

349 views Asked by At

Ok so here is the problem. I am trying to get an array of installed applications and versions.

So far what I have discovered is that I can run this command:

system_profiler SPApplicationsDataType > log.txt

Which will give me a txt file with the following format:

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

I want to be able to take the Application name and the Version number and store them somehow preferably in an array. I'm not sure how to where to start since all entries look the same to me? I need to ignore entries without versions numbers as well.

1

There are 1 answers

6
bdesham On BEST ANSWER

Here’s an Awk script that will give you the output format you mentioned:

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

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

If you save it as script.awk you can run

awk -f script.awk log.txt

to get output like

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

This has been tested with the /usr/bin/awk of OS X 10.9.0.