考虑以下日志:
tm=2017-08-29 11:25:33.579`user_id=1`device_id=1
tm=2017-08-29 11:25:35.961`user_id=1`device_id=1
tm=2017-08-29 11:25:38.961`user_id=2`device_id=2
tm=2017-08-29 11:25:40.993`user_id=2`device_id=2
tm=2017-08-29 11:25:41.729`user_id=3`device_id=3
tm=2017-08-29 11:25:46.075`user_id=3`device_id=4
.....(more logs)
我怎样才能找出device_id正在改变的用户?
我期望的输出是:
tm=2017-08-29 11:25:41.729`user_id=3`device_id=3
tm=2017-08-29 11:25:46.075`user_id=3`device_id=4
答案 0 :(得分:0)
awk 解决方案。我理解这个问题的方法是:显示每个用户的设备的每个变化。我稍微更改了输入文件:
$ cat ch.txt
tm=2017-08-29 11:25:33.579`user_id=1`device_id=1
tm=2017-08-29 11:25:35.961`user_id=1`device_id=1
tm=2017-08-29 11:25:38.961`user_id=2`device_id=2
tm=2017-08-29 11:25:38.961`user_id=2`device_id=1
tm=2017-08-29 11:25:40.993`user_id=2`device_id=2
tm=2017-08-29 11:25:40.993`user_id=2`device_id=1
tm=2017-08-29 11:25:41.729`user_id=3`device_id=3
tm=2017-08-29 11:25:46.075`user_id=3`device_id=4
$ cat tst.awk
BEGIN { FS="[[:space:]]+|`" }
!($3 in usr) { usr[$3] = $0; dev[$3] = $4 }
{
if ( $4 != dev[$3] ){ print usr[$3]; print $0; usr[$3] = $0; dev[$3] = $4 }
}
说明:
然后:
$ awk -f tst.awk ch.txt
tm=2017-08-29 11:25:38.961`user_id=2`device_id=2
tm=2017-08-29 11:25:38.961`user_id=2`device_id=1
tm=2017-08-29 11:25:38.961`user_id=2`device_id=1
tm=2017-08-29 11:25:40.993`user_id=2`device_id=2
tm=2017-08-29 11:25:40.993`user_id=2`device_id=2
tm=2017-08-29 11:25:40.993`user_id=2`device_id=1
tm=2017-08-29 11:25:41.729`user_id=3`device_id=3
tm=2017-08-29 11:25:46.075`user_id=3`device_id=4
编辑:我讨厌这个输出。如果我们将其更改为:,该怎么办?
cat tst2.awk
BEGIN { FS="[[:space:]]+|`|=" }
!($5 in dev) { dev[$5] = $7 }
{
if ( $7 != dev[$5] ){
print $2 OFS $3 ": user " $5 " changed device from " dev[$5] " to " $7;
dev[$5] = $7
}
}
生成:
$ awk -f tst2.awk ch.txt
2017-08-29 11:25:38.961: user 2 changed device from 2 to 1
2017-08-29 11:25:40.993: user 2 changed device from 1 to 2
2017-08-29 11:25:40.993: user 2 changed device from 2 to 1
2017-08-29 11:25:46.075: user 3 changed device from 3 to 4
2017-08-29 11:25:33.579: user 1 changed device from 1 to 2
答案 1 :(得分:0)
awk 解决方案:
awk -F'[[:space:]]+|`|=' 'uid && $5==uid && $7!=did{ print r ORS $0 }
{ uid=$5; did=$7; r=$0 }' file
-F'[[:space:]]+|``|='
- 复合字段分隔符
uid=$5; did=$7; r=$0
- 捕获 user_id uid
, device_id did
和当前记录r
输出:
tm=2017-08-29 11:25:41.729`user_id=3`device_id=3
tm=2017-08-29 11:25:46.075`user_id=3`device_id=4