[英]Print first word of first line and last word
I'm trying to parse a log file in SHELL and want to print first word of first line and last word of each line under it. 我正在尝试在SHELL中解析一个日志文件,并希望在其下打印第一行的第一个单词和每行的最后一个单词。
For instance: 例如:
$ grep -A3 "2015-01-22T07" Test.log | grep -A3 "Messages from Summary report is"
2015-01-22T07:36:30 | 9316 | 461 | 50 | Messages from Summary report is :[ Number of C is 1500
Total distance 10 km
Total number of A is 2
Number of B is 2
]
--
2015-01-22T07:37:30 | 9316 | 461 | 50 | Messages from Summary report is :[ Number of C is 1600
Total distance 11 km
Total number of A is 3
Number of B is 3
]
--
2015-01-22T07:38:30 | 9316 | 461 | 50 | Messages from Summary report is :[ Number of C is 1700
Total distance 12 km
Total number of A is 4
Number of B is 4
]
Expected output: 预期产量:
2015-01-22T07:36:30,1500,10 km,2,2
2015-01-22T07:37:30,1600,11 km,3,3
2015-01-22T07:38:30,1700,12 km,4,4
sorry, im new to this site. 抱歉,我是这个网站的新手。
cat test1.log
2015-01-22T07:36:30 | 9316 | 461 | 50 | Messages from Summary report is :[
Number of C is 1500
Total distance 10 km
Total number of A is 2
Number of B is 2
]
--
2015-01-22T07:37:30 | 9316 | 461 | 50 | Messages from Summary report is :[
Number of C is 1600
Total distance 11 km
Total number of A is 3
Number of B is 3
]
--
2015-01-22T07:38:30 | 9316 | 461 | 50 | Messages from Summary report is :[
Number of C is 1700
Total distance 12 km
Total number of A is 4
Number of B is 4
]
Re-attempt: 重新尝试:
# awk -v RS='\n' -v OFS=, '$1~/^[0-9]{4}-[0-9]{2}-[0-9]{2}T/ {if (s) print s; s=$1; next}
/Total distance/{s = s OFS $(NF-1) " " $NF;next}
NF>2{s = s OFS $NF}
END{print s
}' test1.log
Output 产量
,:[,1500,10 km,2,2,:[,1600,11 km,3,3,:[,1700,12 km,4,4 ,:[,1500,10 km,2,2,:[,1600,11 km,3,3,:[,1700,12 km,4,4
Check* 校验*
# head -1 test.log|cat -vte
2015-01-22T07:36:30 | 9316 | 461 | 50 | Messages from Summary report is :[ $
You can use this awk on your given outpur: 您可以在给定的outpur上使用该awk:
awk -v RS='\r' -v OFS=, '$1~/^[0-9]{4}-[0-9]{2}-[0-9]{2}T/ {if (s) print s; s=$1; next}
/Total distance/{s = s OFS $(NF-1) " " $NF;next}
NF>2{s = s OFS $NF}
END{print s
}' file
2015-01-22T07:36:30,1500,10 km,2,2
2015-01-22T07:37:30,1600,11 km,3,3
2015-01-22T07:38:30,1700,12 km,4,4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.