如何在sed,awk或其他任何内容中编辑以下行

时间:2016-03-01 16:24:57

标签: regex perl awk sed gawk

如何在sed,awk或其他任何内容中修改以下行:

Array subArr(int pos, int siz)
int * subArr(int pos, int siz)

使其如下所示:

subArr(int pos, int siz)
subArr(int pos, int siz)

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 

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:2)

最安全的方法是使用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