鍍金池/ 問答/Linux/ shell分析日志

shell分析日志

有一個(gè)日志文件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的值?

回答
編輯回答
情已空
grep "2018-04-26" logfile.log | cut -d "|" -f2 | cut -d "=" -f2
2017年7月8日 11:32
編輯回答
我不懂
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 就不能用了,所以要不自己寫循環(huán)來代替 g flag 的功能,要不自己 substring 來代替 capturing group 的功能。

2017年11月16日 11:54