Bash 如何解析给定的文件?

Bash 如何解析给定的文件?,bash,shell,Bash,Shell,我想要一个包含最新utc时间的块(无论是什么)。删除重复的块(非行),仅显示包含最新utc时间的块。我希望它是在shell脚本 { "cat": "error", "topic": "glp/0/17q2d9v/rq/dev/iox/dio/do", "message": "ERROR in iox_test handle", "utc": "2018-07-12 12:39:42.670 UTC" } { "cat": "erro

我想要一个包含最新utc时间的块(无论是什么)。删除重复的块(非行),仅显示包含最新
utc
时间的块。我希望它是在shell脚本

{
     "cat":  "error",
     "topic":    "glp/0/17q2d9v/rq/dev/iox/dio/do",
     "message":  "ERROR in iox_test handle",
     "utc":  "2018-07-12 12:39:42.670 UTC"
}
{
     "cat":  "error",
     "topic":    "glp/0/17q2d9v/rq/dev/iox/dio/do",
     "message":  "ERROR in iox_test handle",
     "utc":  "2018-07-12 12:39:42.840 UTC"
}
{
     "cat":  "error",
     "topic":    "glp/0/17q2d9v/rq/dev/iox/dio/do",
     "message":  "ERROR in iox_test handle",
     "utc":  "2018-07-12 12:39:42.840 UTC"
}
{
     "cat":  "error",
     "topic":    "glp/0/17q2d9v/rq/dev/iox/dio/do",
     "message":  "ERROR in iox_test handle",
     "utc":  "2018-07-12 12:39:42.840 UTC"
}
{
     "cat":  "error",
     "topic":    "glp/0/17q2d9v/rq/dev/iox/dio/do",
     "message":  "ERROR in iox_test handle",
     "utc":  "2018-07-12 12:39:43.20 UTC"
}
{
     "cat":  "error",
     "topic":    "glp/0/17q2d9v/rq/dev/iox/dio/do",
     "message":  "ERROR in iox_test handle",
     "utc":  "2018-07-12 12:39:43.20 UTC"
}
{
     "cat":  "error",
     "topic":    "glp/0/17q2d9v/rq/dev/iox/dio/do",
     "message":  "ERROR in iox_test handle",
     "utc":  "2018-07-12 12:39:43.20 UTC"
}
例如:我希望给定文件的输出为:

{
     "cat":  "error",
     "topic":    "glp/0/17q2d9v/rq/dev/iox/dio/do",
     "message":  "ERROR in iox_test handle",
     "utc":  "2018-07-12 12:39:43.20 UTC"
}

您可以测试以下脚本(假设:数据文件名为file.txt):


假设您的文件是
te1.txt

grep 'utc' te1.txt | grep -oP '[[:digit:]].*(?= [[:space:]]*UTC.*)' | sort -k1,2 -ur | head -n1 | xargs -Iregex grep -m1 -B4 -A1 "regex" te1.txt
示例

grep 'utc' te1.txt | grep -oP '[[:digit:]].*(?= [[:space:]]*UTC.*)' | sort -k1,2 -ur | head -n1 | xargs -Iregex grep -m1 -B4 -A1 "regex" te1.txt 
{
    "cat":  "error",
        "topic":    "glp/0/17q2d9v/rq/dev/iox/dio/do",
        "message":  "ERROR in iox_test handle",
        "utc":  "2018-07-12 12:39:43.20 UTC"
}

当块是连续的时,您需要最后一个块。
使用
GNU sed 4.2.2
可以使用

sed -z 's/.*}\n{/{/' inputfile

你试过什么了吗?shell脚本似乎不适合做这项工作。你甚至可以建议一个python解决方案。。!
sed -z 's/.*}\n{/{/' inputfile