1

我正在尝试从如下日志文件中获取所有“CP”值:

2013-06-27 17:00:00,017 INFO - [AlertSchedulerThread18] [2013-06-27 16:59:59, 813] -- SN: 989333333333 ||DN: 989333333333 ||CategoryId: 4687 ||CGID: null||Processing started ||Billing started||Billing Process: 97 msec ||Response code: 2001 ||Package id: 4387 ||TransactionId: 66651372336199820989389553437483742||CDR:26 msec||CDR insertion: 135 msec||Successfully inserted in CDR Table||CP:53 msec||PROC - 9 msec||Successfully executed procedure call.||Billing Ended||197 msec ||Processing ended
2013-06-27 17:00:00,018 INFO - [AlertSchedulerThread62] [2013-06-27 16:59:59, 824] -- SN: 989333333333 ||DN: 989333333333 ||CategoryId: 3241 ||CGID: null||Processing started ||Billing started||Billing Process: 61 msec ||Response code: 2001 ||Package id: 2861 ||TransactionId: 666513723361998319893580191324005184||CDR:25 msec||CDR insertion: 103 msec||Successfully inserted in CDR Table||CP:59 msec||PROC - 24 msec||Successfully executed procedure call.||Billing Ended||187 msec ||Processing ended
2013-06-27 17:00:00,028 INFO - [AlertSchedulerThread29] [2013-06-27 16:59:59, 903] -- SN: 989333333333 ||DN: 989333333333 ||CategoryId: 4527 ||CGID: null||Processing started ||Billing started||Billing Process: 47 msec ||Response code: 2001 ||Package id: 4227 ||TransactionId: 666513723361999169893616006323701572||CDR:22 msec||CDR insertion: 83 msec||Successfully inserted in CDR Table||CP:21 msec||PROC - 7 msec||Successfully executed procedure call.||Billing Ended||112 msec ||Processing ended

...得到这样的输出:

CP:53 msec
CP:59 msec
CP:21 msec

如何使用 awk 做到这一点?

4

6 回答 6

3

cut对于这些事情总是好的和快速的:

$ cut -d"*" -f3 file
CP:53 msec
CP:59 msec
CP:21 msec

无论如何,这些awk方法可以做到:

$ awk -F"|" '{print $27}' file  | sed 's/*//g'
CP:53 msec
CP:59 msec
CP:21 msec

或者

$ awk -F"\|\|" '{print $14}' file | sed 's/*//g'
CP:53 msec
CP:59 msec
CP:21 msec

或者也

$ awk -F"*" '{print $3}' file
CP:53 msec
CP:59 msec
CP:21 msec

在两者中,我们设置字段分隔符将字符串拆分为某个特定字符|*. 然后我们打印分割文本的某个块。

于 2013-06-27T13:27:47.660 回答
2

GNU

$sed -r 's/.*(CP:[0-9]+\smsec).*/\1/' 文件
CP:53 毫秒
CP:59 毫秒
CP:21 毫秒
于 2013-06-27T15:04:43.203 回答
2

总有grep

grep -o 'CP:[[:digit:]]* msec' log.txt

如果不一定每次都是毫秒,你可以把所有东西都带到管道上:

grep -o 'CP:[^|]*' log.txt
于 2013-06-27T14:49:00.983 回答
2
$ awk -F'[|][|]' '{print $14}' file
**CP:53 msec**

**CP:59 msec**

**CP:21 msec**

如果你真的在输入中有'*',只需调整以删除它们:

$ awk -F'[|][|]' '{gsub(/\*/,""); print $14}' file
CP:53 msec

CP:59 msec

CP:21 msec
于 2013-06-27T14:34:23.220 回答
2

一个有趣的 sed 命令怎么样?

sed -n 's/.*\*\*\(.*\)\*\*.*/\1/p'
于 2013-06-27T13:28:03.437 回答
2

awk

awk -F"[|*]+" '{ print $14 }' file
于 2013-06-27T13:52:11.507 回答