asuerhao's Blog

如果有什么做的不到的地方请尽管留言, 我会改进的 : )

出租车乘客上下车的数据文件处理

    原始文件内容为:

name,time,jd,wd,status,v,angle,
粤B13K97,2011/04/18 00:00:26,114.044151,22.531418,1,75,5,
粤B13K97,2011/04/18 00:00:56,114.038452,22.530817,0,78,5,
粤B13K97,2011/04/18 00:01:56,114.026199,22.531134,0,84,6,
粤B13K97,2011/04/18 00:02:26,114.020035,22.532049,0,80,5,
粤B13K97,2011/04/18 00:02:56,114.013885,22.530767,1,82,5,
粤B13K97,2011/04/18 00:03:04,114.012283,22.530399,1,80,5,
粤B13K97,2011/04/18 00:03:26,114.007767,22.529682,1,79,5,
粤B13K97,2011/04/18 00:03:56,114.001984,22.530184,1,75,6,
粤B13K97,2011/04/18 00:04:26,113.996498,22.528917,0,75,5,
粤B13K97,2011/04/18 00:04:56,113.991653,22.526068,0,73,5,
粤B13K97,2011/04/18 00:05:26,113.986450,22.523933,0,74,5,
粤B13K97,2011/04/18 00:06:26,113.975067,22.522949,1,78,5,
粤B13K97,2011/04/18 00:06:34,113.973465,22.522949,1,80,5,
粤B13K97,2011/04/18 00:06:56,113.968849,22.522932,1,83,5,
粤B13K97,2011/04/18 00:07:56,113.956467,22.523268,1,69,6,
粤B13K97,2011/04/18 00:08:26,113.951736,22.523832,1,54,6,
粤B13K97,2011/04/18 00:08:56,113.949387,22.525534,1,49,0,
粤B13K97,2011/04/18 00:09:26,113.949799,22.529217,0,47,0,

......

其中第五个字段表示出租车的载客信息, 1表示有乘客 , 0表示空载. 需要将所有上下车的乘车信息挑出来, 即由1变为0或由0变为1:

直接上Shell脚本:

sed -e 1d FileName |
                      awk -F, '{ now_f5=int($5)
                                 if((now_f5 != pro_f5) && (pro != ""))
                                     print pro "\n" $0
                                 pro_f5=int($5); pro=$0}' |
                                                           uniq

处理后的数据为:

粤B13K97,2011/04/18 00:00:26,114.044151,22.531418,1,75,5,
粤B13K97,2011/04/18 00:00:56,114.038452,22.530817,0,78,5,
粤B13K97,2011/04/18 00:02:26,114.020035,22.532049,0,80,5,
粤B13K97,2011/04/18 00:02:56,114.013885,22.530767,1,82,5,
粤B13K97,2011/04/18 00:03:56,114.001984,22.530184,1,75,6,
粤B13K97,2011/04/18 00:04:26,113.996498,22.528917,0,75,5,
粤B13K97,2011/04/18 00:05:26,113.986450,22.523933,0,74,5,
粤B13K97,2011/04/18 00:06:26,113.975067,22.522949,1,78,5,
粤B13K97,2011/04/18 00:08:56,113.949387,22.525534,1,49,0,
粤B13K97,2011/04/18 00:09:26,113.949799,22.529217,0,47,0,

......