如何在 sed、awk 或其他任何程序中编辑以下行
how to edit following line in sed, awk or anything else
如何在 sed、awk 或其他任何程序中编辑以下行:
root@laptop002:/tmp# cat /tmp/log
2016-03-01 06:08:26 {"id":"778640","cuid":"1","msid":"199033","lid":"582","Started":"1","qid":"9401"} batch is running
使其看起来像下面这样:
2016-03-01 06:08:26 "msid":"199033" batch is running
或
2016-03-01 06:08:26 msid is 199033 batch is running
或
2016-03-01 06:08:26 msid=199033 batch is running
$ awk -F'[{,}]' '{print , , $NF}' file
2016-03-01 06:08:26 "msid":"199033" batch is running
最安全的方法是使用 json 解析器解析 json 字符串,例如 JSON
:
use strict;
use warnings;
use JSON;
my $str = '2016-03-01 06:08:26 {"id":"778640","cuid":"1","msid":"199033","lid":"582","Started":"1","qid":"9401"} batch is running';
my ($date, $time, $json, $msg) = split ' ', $str, 4;
my $hash = decode_json($json);
print join " ", $date, $time, "msid=" . $hash->{msid}, $msg;
输出:
2016-03-01 06:08:26 msid=199033 batch is running
如何在 sed、awk 或其他任何程序中编辑以下行:
root@laptop002:/tmp# cat /tmp/log
2016-03-01 06:08:26 {"id":"778640","cuid":"1","msid":"199033","lid":"582","Started":"1","qid":"9401"} batch is running
使其看起来像下面这样:
2016-03-01 06:08:26 "msid":"199033" batch is running
或
2016-03-01 06:08:26 msid is 199033 batch is running
或
2016-03-01 06:08:26 msid=199033 batch is running
$ awk -F'[{,}]' '{print , , $NF}' file
2016-03-01 06:08:26 "msid":"199033" batch is running
最安全的方法是使用 json 解析器解析 json 字符串,例如 JSON
:
use strict;
use warnings;
use JSON;
my $str = '2016-03-01 06:08:26 {"id":"778640","cuid":"1","msid":"199033","lid":"582","Started":"1","qid":"9401"} batch is running';
my ($date, $time, $json, $msg) = split ' ', $str, 4;
my $hash = decode_json($json);
print join " ", $date, $time, "msid=" . $hash->{msid}, $msg;
输出:
2016-03-01 06:08:26 msid=199033 batch is running