sed例子
以care.log这个log文件为例,
care.log:
05:44:31,816 DEBUG RawAggregationWorker:70 - LTS is working on Raw Data Aggregation execute Start, Now:1535003071816, currentTimeMillis:1535003071816
05:44:31,816 DEBUG RawAggregationWorker:57 - LTS is working on Raw Data Aggregation to dataPoints:22, careConfig.getLtsRawaggregationDataPoints():10
05:46:53,264 DEBUG RawAggregationWorker:100 - LTS is working on Raw Data Aggregation execute End, Now:1535003071816, currentTimeMillis:1535003213264, now Done Took: 141448ms, currentTime Done Took: 141448ms
05:46:53,265 DEBUG MinAggregationWorker:59 - LTS is working on Min Data Aggregation execute Start, Now:1535002591816, currentTimeMillis:1535003213265
05:55:04,112 DEBUG MinAggregationWorker:108 - LTS is working on Min Data Aggregation execute End, Now:1535002591816, currentTimeMillis:1535003704112, now Done Took: 1112seconds, currentTime Done Took: 490seconds
05:55:04,112 DEBUG HourAgingWorker:53 - LTS is working on Hourly Aging, now: 1535003011816, currentTimeMillis:1535003704112
05:58:29,697 DEBUG HourAgingWorker:69 - LTS is working on Hourly Aging, now Done Took: 897881ms, currentTime Done Took: 205585ms
希望结果为:
Raw Data Aggregation Start-Now:1535003071816;currentTimeMillis:1535003071816;Raw Data Aggregation-dataPoints:10
Raw Data Aggregation End-Now:1535003071816;currentTimeMillis:1535003213264
Min Data Aggregation Start-Now:1535002591816;currentTimeMillis:1535003213265
Min Data Aggregation End-Now:1535002591816;currentTimeMillis:1535003704112
Hourly Aging-now: 1535003011816;currentTimeMillis:1535003704112
Hourly Aging-now Done Took: 897881ms;currentTime Done Took: 205585ms
- 匹配从行首到关键字并替换
sed 's#^.* on ##g'
- 匹配关键字并保留匹配到关键字其中一部分(用括号指代要保留的部分在#后用\1\2.....表示)
sed 's#\(^.*\) execute \(.*\),.*\(Now:[0-9]\{13\}\), \(currentTimeMillis:[0-9]\{13\}\).*$#\1 \2-\3;\4#g'
- 合并匹配到的行与上一行
sed '{N;s/\(.*\)\n\(^.*dataPoints.*$\)/\1;\2/;t merge;P;D};:merge;p;d'
- 删除空行
sed '/^$/d'
或
awk NF
- 最后,合并在一起:
cat care.log | sed 's#^.* on ##g' | sed 's#\(^.*\) execute \(.*\),.*\(Now:[0-9]\{13\}\), \(currentTimeMillis:[0-9]\{13\}\).*$#\1 \2-\3;\4#g' | sed 's#^\(.*\) to \(dataPoints\).*DataPoints():\([0-10]\{1,\}\)$#\1-\2:\3#g' | sed 's#\(^.*\), \(now.*\), \(current.*:.*\)$#\1-\2;\3#g' | sed '{N;s/\(.*\)\n\(^.*dataPoints.*$\)/\1;\2/;t merge;P;D};:merge;p;d' |awk NF