Bash 从plist数组获取/Grep属性

Bash 从plist数组获取/Grep属性,bash,macos,shell,Bash,Macos,Shell,试图从“SnapshotDates”数组中获取最后一个值,该数组本身位于.plist数组中。这是我到目前为止得到的,但它无法提供比第一行更多的输出 defaults read /Library/Preferences/com.apple.TimeMachine Destinations | egrep '.*SnapshotDates(.*\n)*' 要打印“SnapshotDates”数组,请使用此Perl one liner,它将打印所有日期: defaults read /Library

试图从“SnapshotDates”数组中获取最后一个值,该数组本身位于.plist数组中。这是我到目前为止得到的,但它无法提供比第一行更多的输出

defaults read /Library/Preferences/com.apple.TimeMachine Destinations | egrep '.*SnapshotDates(.*\n)*'
要打印“SnapshotDates”数组,请使用此Perl one liner,它将打印所有日期:

defaults read /Library/Preferences/com.apple.TimeMachine Destinations | \
  perl -0077 -ne 'print "$_\n" for m{SnapshotDates \s+ = \s+ [(] .*? \n ( [^)]* ) [)] }xms'
输出:

            "2018-06-07 19:52:01 +0000",
            "2018-06-07 20:30:46 +0000",
            "2018-06-07 21:31:40 +0000",
            "2018-06-07 22:48:26 +0000"

2018-06-07 22:48:26 +0000
要仅打印最后一个快照日期,请执行以下操作:

defaults read /Library/Preferences/com.apple.TimeMachine Destinations | \
  perl -0077 -ne 'print "$_\n" for m{SnapshotDates \s+ = \s+ [(] [^)]* \n .*? "([^\n"]+)" \n \N* [)] }xms'  
输出:

            "2018-06-07 19:52:01 +0000",
            "2018-06-07 20:30:46 +0000",
            "2018-06-07 21:31:40 +0000",
            "2018-06-07 22:48:26 +0000"

2018-06-07 22:48:26 +0000
Perl one liner使用以下命令行标志:
-e
:告诉Perl在线查找代码,而不是在文件中。
-n
:一次循环输入一行,默认情况下将其分配给
$。
-00777
:整个文件都是Slurp文件

正则表达式使用以下修饰符:
/x
:为了便于阅读,请忽略空格和注释。
/m
:允许多行匹配。
/s
:允许
匹配换行符(为了便于维护,此处并不需要)

另请参见:




您是否仅限于使用Shell,还是可以使用“Swift脚本”(更易于读取plist文件)?它将用于applescript,因此可以选择applescript或js。但是如果你有一个快速的片段准备好了,请随意展示,可能会有所帮助。谢谢帖木儿非常详细和格式良好的回答!非常感谢:)第一个命令工作正常,但由于某些原因,仅打印最后一个快照的命令对我不起作用。