使用cut / awk或任何东西提取String的一部分

时间:2016-10-25 11:07:01

标签: linux shell

[DOSLB] : [dmob7h-002.on.bell.ca] : [61421820100992016102414274381420414330] : [[ACTIVE] ExecuteThread: '15' for queue: 'weblogic.kernel.Default (self-tuning)'] : [ca.bell.tv.doslb.infrastructure.logging.LoggingAspect] : [Ending execution of the class: ca.bell.tv.doslb.application.webservice.impl.RetrieveLocalSubscriberDelegateImpl] : [Method: getRetrieveLocalSubscriber[Call ended at: 2016-10-24 14:27:44.150] : [lasted 0 sec, 305 ms] : [WITHINLIMITS]]

我想从上面的字符串中提取 getRetrieveLocalSubscriber 。但我不能具体说明它的位置和字符串,因为它是一个服务名称,因此它会在日志中随时间变化,位置可能会改变,但它的格式相同, [方法:getRetrieveLocalSubscriber [电话会议结束于:2016-10-24 14:27:44.150] 此部分将始终相同。

我还想提取持续0秒部分,但问题是秒将始终改变。

我想在一个变量中输出 getRetrieveLocalSubscriber 之类的输出 在另一个变量

中持续0秒

我尝试过awk命令     cat out_log.txt | awk -F '[:]' '{print $11}' 提供输出的是 getRetrieveLocalSubscriber [呼叫结束于

2 个答案:

答案 0 :(得分:2)

尝试这样的事情:

echo "[DOSLB] : [dmob7h-002.on.bell.ca] : [61421820100992016102414274381420414330] : [[ACTIVE] ExecuteThread: '15' for queue: 'weblogic.kernel.Default (self-tuning)'] : [ca.bell.tv.doslb.infrastructure.logging.LoggingAspect] : [Ending execution of the class: ca.bell.tv.doslb.application.webservice.impl.RetrieveLocalSubscriberDelegateImpl] : [Method: getRetrieveLocalSubscriber[Call ended at: 2016-10-24 14:27:44.150] : [lasted 0 sec, 305 ms] : [WITHINLIMITS]]" | perl -ne '/Method: getRetrieveLocalSubscriber\[Call ended at: (\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})\.(\d+?)\] : \[lasted (\d+?) sec, (\d+?) ms\]/ && print "Call end: $1-$2-$3 $4:$5:$6.$7, lasted for $8s $9ms";'
Call end: 2016-10-24 14:27:44.150, lasted for 0s 305ms

或者,如果这样的字符串在文件中:

cat test.log | perl -ne '/Method: getRetrieveLocalSubscriber\[Call ended at: (\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})\.(\d+?)\] : \[lasted (\d+?) sec, (\d+?) ms\]/ && print "Call end: $1-$2-$3 $4:$5:$6.$7, lasted for $8s $9ms";'

这个正则表达式也接受不同的“呼叫结束”时间。你可以替换

(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})\.(\d+?)

2016-10-24 14:27:44.150 $ 8 以及 $ 9 $ 1 $ 2 分别仅匹配

字符串
Call ended at: 2016-10-24 14:27:44.150

子串。

答案 1 :(得分:2)

你可以尝试这样的事情;

grep -o -P '(?=\[Method: getRetrieveLocalSubscriber).*(?<=ms])' yourFile

 grep -o -P '(?=\[Call).*(?<=ms])' yourFile

EG;

user@host$ grep -o -P '(?=\[Method: getRetrieveLocalSubscriber).*(?<=ms])' test
[Method: getRetrieveLocalSubscriber[Call ended at: 2016-10-24 14:27:44.150] : [lasted 0 sec, 305 ms]

user@host$ grep -o -P '(?=\[Call).*(?<=ms])' test
[Call ended at: 2016-10-24 14:27:44.150] : [lasted 0 sec, 305 ms]