日期:2014-05-16  浏览次数:20642 次

FreeBSD 下Bshell命令问题
我的日志格式如下:
{"CP_LAT":0.0,"CP_PHONENUM":"","CP_RATIO":"480*800","actions":[{"action":"searchandrange","paras":{"ps":10,"city_id":"19010000","standard":"02","near_order_by":"","lat":"","distance":"","order_by":"popular","keyword":"\u4e2d\u56fd\u6c5f\u82cf\u7701\u5357\u4eac\u5e02\u73af\u6e56\u8def","type":"","lon":"","mark":"1","trade_name":"","pn":1},"requuid":"319bd356-4680-49e7-9d1c-1a91ab863411"}],"CP_TOUCH":"1","CP_PRT":"SHA001A02600","CP_PUBRESPATH":"file:///android_asset/more/public","CP_TPL":"android","CP_CITYID":"19010000","CP_UID":"","CP_LON":0.0,"CP_IMEI":"000000000000000","CP_RESVER":"1.0","CP_CH":"A001","CP_PLTFM":"ANDROID","CP_MODEL":"sdk-15-4.0.4","CP_VER":"2.6.0"}

用sed命令截取CP_TPL和CP_IMEI的值,如上条日志截取结果就是:android 000000000000000
上面例子为文件中的一条记录,观察所有记录 这两个字段的位置不确定(没办法用awk -F":"截取),字段值的长短不确定(例如CP_TPL的值可能是adroid ios java...),该怎么做啊?
 
sed? freebsd

------解决方案--------------------
引用:
引用:版主的方法不可取,人家说了,位置不定,所以有可能CP_IMEI会出现在CP_TPL前面的。这个问题我认为一步搞不定,最好先用,做FS,把每一项都提取出来,然后再过滤出……
我觉得楼主说的位置不定主要是指列数不定,看看楼主怎么回复吧。

不过仔细想想即使颠倒用你提供的这个方法也能实现,多加一句处理就行了,用一条命令能搞定当然是最好的了:
sed -e 's/.*CP_TPL...\([^"]\+\).*CP_IMEI...\([^"]\+\).*/\1 \2/' -e 's/.*CP_IMEI...\([^"]\+\).*CP_TPL...\([^"]\+\).*/\2 \1/' urfile

------解决方案--------------------
sed 's/.*CP_TPL...\([^"]\+\).*CP_IMEI...\([^"]\+\).*\
------解决方案--------------------
.*CP_IMEI...\([^"]\+\).*CP_TPL...\([^"]\+\).*/\1 \2/' urfile

------解决方案--------------------
格式上显然是逗号分隔,然后用冒号分隔域名和数值。