簡體   English   中英

使用cut / awk或其他方法提取字符串的一部分

[英]Extract Part of String using cut/awk or anything

[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}' cat out_log.txt | awk -F '[:]' '{print $11}'給出的輸出是getRetrieveLocalSubscriber [通話結束於

嘗試這樣的事情:

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

子。

您可以嘗試這樣的事情;

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

要么

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

例如;

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]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM