当前位置: 技术问答>linux和unix
FreeBSD 下Bshell命令问题
来源: 互联网 发布时间:2017-04-09
本文导语: 我的日志格式如下: {"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":"u4e2du56fdu6c5fu82cfu7701u5357u4eacu5e0...
我的日志格式如下:
{"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":"u4e2du56fdu6c5fu82cfu7701u5357u4eacu5e02u73afu6e56u8def","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...),该怎么做啊?
{"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":"u4e2du56fdu6c5fu82cfu7701u5357u4eacu5e02u73afu6e56u8def","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 -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
|
格式上显然是逗号分隔,然后用冒号分隔域名和数值。