0

I have output from a specific command that can provide the number of times an application has been marked as 'used' on an OS X system. The command is mdls.

The output looks similar to the following (truncated)

...
kMDItemLastUsedDate            = 2016-03-15 18:45:00 +0000
kMDItemLogicalSize             = 47225668
kMDItemPhysicalSize            = 57065472
kMDItemUseCount                = 90
kMDItemUsedDates               = (
    "2016-02-02 06:00:00 +0000",
    "2016-02-03 06:00:00 +0000",
    "2016-02-04 06:00:00 +0000",
    "2016-02-05 06:00:00 +0000",
    "2016-02-09 06:00:00 +0000",
    "2016-02-10 06:00:00 +0000",
    "2016-02-11 06:00:00 +0000",
    "2016-02-12 06:00:00 +0000",
    "2016-02-13 06:00:00 +0000",
    "2016-02-14 06:00:00 +0000",
    "2016-02-15 06:00:00 +0000",
    "2016-02-16 06:00:00 +0000",
    "2016-02-17 06:00:00 +0000",
    "2016-02-18 06:00:00 +0000",
    "2016-02-19 06:00:00 +0000",
    "2016-02-20 06:00:00 +0000",
    "2016-02-21 06:00:00 +0000",
    "2016-02-22 06:00:00 +0000",
    "2016-02-23 06:00:00 +0000",
    "2016-02-24 06:00:00 +0000",
    "2016-02-25 06:00:00 +0000",
    "2016-02-26 06:00:00 +0000",
    "2016-02-27 06:00:00 +0000",
    "2016-03-02 06:00:00 +0000",
    "2016-03-05 06:00:00 +0000",
    "2016-03-07 06:00:00 +0000",
    "2016-03-09 06:00:00 +0000",
    "2016-03-10 06:00:00 +0000",
    "2016-03-11 06:00:00 +0000",
    "2016-03-12 06:00:00 +0000",
    "2016-03-13 06:00:00 +0000",
    "2016-03-15 05:00:00 +0000"
)
kMDItemVersion                 = "9.0.3"
...

Of this list of stuff, I really only want to view the specific entries related to kMDItemUsedDates. I don't know how many lines this entry may have on any given system, it could be 1 or it could be 32.

How would I process this output to be able to retrieve ONLY a count of the number of date-stamp entries pertaining to the kMDItemUsedDates array? It's important to note that date-stamps in and of themselves are not unique in this command output.

2
perl -nE '$c++ if m/^kMDItemUsedDates\s+=\s+\(/..m/^\)/; END { say $c - 2 }'

Match and increment a counter between the start and end lines then print that count, minus the included count for the first and last lines. Feed it a file, files, or standard input.

  • Thanks! I was assuming that something like perl would have to have been used, but I'm not that experienced with it or regex to have thought this through. But this works great! – Andrew Mar 17 '16 at 11:42
1

The follow can count choiced lines if output will piped through:

sed -n '/kMDItemUsedDates\s*= (/{:1;n;/)/!{p;b1;};}' | wc -l

variant 2:

sed '/kMDItemUsedDates\s*= (/,/)/!d;/[()]/d' | wc -l
  • I ran both of these and neither of them worked as expected :( – Andrew Mar 17 '16 at 11:41
  • @Andrew what do it produced without | wc -l ? – Costas Mar 17 '16 at 15:27
  • sed: 1: "/kMDItemUsedDates\s*= ( ...": unexpected EOF (pending }'s) – Andrew Mar 21 '16 at 19:09
  • @Andrew try to put ( and ) inside [] : [(] – Costas Mar 22 '16 at 18:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.