shell分析日志

有一个日志文件logfile.log如下格式:

2018-04-26 15:30:02|uri=/user/info/index.php|method=GET
2018-04-26 15:31:01|uri=/agent/info/index.php|method=GET
2018-04-26 15:31:32|uri=/auth/login|method=POST
2018-04-26 15:32:33|uri=/user/edit|method=POST
2018-04-26 15:33:00|uri=/auth/logout|method=POST
2018-04-26 15:33:11|uri=/group/index.php|method=GET
...
2018-04-27 13:12:23|uri=/auth/login|method=POST
...

1,如何打印出所有uri值,即:

/user/info/index.php
/agent/info/index.php
/auth/login
/user/edit
/auth/logout
/group/index.php

2,如何打印出2018-04-26这一天的所有uri的值?

阅读 1.8k
2 个回答
grep "2018-04-26" logfile.log | cut -d "|" -f2 | cut -d "=" -f2
const log = `
2018-04-26 15:30:02|uri=/user/info/index.php|method=GET
2018-04-26 15:31:01|uri=/agent/info/index.php|method=GET
2018-04-26 15:31:32|uri=/auth/login|method=POST
2018-04-26 15:32:33|uri=/user/edit|method=POST
2018-04-26 15:33:00|uri=/auth/logout|method=POST
2018-04-26 15:33:11|uri=/group/index.php|method=GET
...
2018-04-27 13:12:23|uri=/auth/login|method=POST
`

const rtn = log.match(/(2018-04-26 \S{13}).*?(?=\|)/g)
console.log(rtn.map(i => i.substring(24)))

如果用 g flag 匹配所有值的话,capturing group 就不能用了,所以要不自己写循环来代替 g flag 的功能,要不自己 substring 来代替 capturing group 的功能。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进