求完善,将"user_id":"2" 里面的2取出来。 LINUX里。。。

不会武功的猪 发布于 2015/05/12 14:53
阅读 135
收藏 0

[ 22:38:41.814 221.223.91.84 866333026904415 run User login ] {"code":"0","result":{"user_id":"2","user_avatar":"http:\/\/img.haha.com\/avatar\/2014-12-12\/w_120x120_548af266b0283.jpg","user_nick":"\u5218","user_sex":"1","user_token":"7b0687f5b50a32b9b1e02c24d48b7050","user_disturb_time":"23:00-06:00","user_hidden_mobile":"186****2431","driver":null,"is_set_default_route":"0"},"msg":"success"} [48661]

grep 'User login' /home/backup/20150512.log | grep 'user_hidden_mobile' |awk ???????

求完善,将"user_id":"2" 里面的2取出来。



加载中
0
JasonWcx
JasonWcx
正则命令是这样的 grep '(?<="user_id":")\w+(?=")'   /home/backup/20140512.log
0
不会武功的猪
不会武功的猪

 [ 22:38:41.814 221.223.91.84 866333026904415 run User login ] {"code":"0","result":{"user_id":"2","user_avatar":"http:\/\/img.51yche.com\/avatar\/2014-12-12\/w_120x120_548af266b0283.jpg","user_nick":"\u5218","user_sex":"1","user_token":"7b0687f5b50a32b9b1e02c24d48b7050","user_disturb_time":"23:00-06:00","user_hidden_mobile":"186****2431","driver":null,"is_set_default_route":"0"},"msg":"success"} [48661]


awk 或是正则 取出 22:38:41  user_id:2 

0
eechen
eechen
用PHP参与到命令行开发,用于分析日志也是可以的,PHP有一个file()函数,可以打开一个文件,返回一个数组,每一行是数组的一个元素。这里用正则得到中间的JSON串,然后解码为数组,输出user_id:
<?php
$log = <<<EOF
[ 22:38:41.814 221.223.91.84 866333026904415 run User login ] {"code":"0","result":{"user_id":"2","user_avatar":"http:\/\/img.haha.com\/avatar\/2014-12-12\/w_120x120_548af266b0283.jpg","user_nick":"\u5218","user_sex":"1","user_token":"7b0687f5b50a32b9b1e02c24d48b7050","user_disturb_time":"23:00-06:00","user_hidden_mobile":"186****2431","driver":null,"is_set_default_route":"0"},"msg":"success"} [48661]
EOF;
preg_match('@^\[(.*)\] (.*) \[(.*)\]$@', $log, $matches);
$arr = json_decode($matches[2], true);
print_r($arr);
echo $arr['result']['user_id']."\n";

不会武功的猪
不会武功的猪
谢谢~~
返回顶部
顶部