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

限于喜欢 提交于 2019-12-13 09:03:27

问题


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


来源:https://stackoverflow.com/questions/35728121/how-to-edit-following-line-in-sed-awk-or-anything-else

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!