how to edit following line in sed, awk or anything

2019-09-21 18:25发布

问题:

how to edit following line in sed, awk or anything else:

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 

to make it look like following:

2016-03-01 06:08:26     "msid":"199033"  batch is running 

or

2016-03-01 06:08:26     msid is 199033  batch is running 

or

2016-03-01 06:08:26     msid=199033  batch is running 

回答1:

$ awk -F'[{,}]' '{print $1, $4, $NF}' file
2016-03-01 06:08:26      "msid":"199033"   batch is running 


回答2:

The safest way would be to parse the json string with a json parser, like 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;

Output:

2016-03-01 06:08:26 msid=199033 batch is running