我在日志文件中有一行看起来像
Oct 07, 2014 7:39:10 AM x.y.z
SEVERE: adding the post (STORY) abcd = 495274900579805_10204277254604731 : a = 0 b = 0 c = 0
我想从第一行得到日期和时间,从第二行得到a = 0 b = 0 c = 0,我怎么能用grep和awk实现这个目的。请帮助
答案 0 :(得分:0)
你可以尝试下面的grep命令,
$ grep -oP '.*?(?=\s[^.\s]+\.[^.\s]+\.\S+)|:\s+\K[^:]*$' file
Oct 07, 2014 7:39:10 AM
a = 0 b = 0 c = 0
<强>更新强>
$ grep -oP '.*?(?=\s[^.\s]+\.[^.\s]+\.\S+)|\) *\K.*' file
Oct 07, 2014 7:39:10 AM
abcd = 495274900579805_10204277254604731 : a = 0 b = 0 c = 0
答案 1 :(得分:0)
这是awk
版本(您要求使用awk)
awk '/AM|PM/ && NF--; /a =/ {print "a = "$(NF-6),"b = "$(NF-3),"c = "$NF}' file
Oct 07, 2014 7:39:10 AM
a = 0 b = 0 c = 0
另一个版本:
awk '/AM|PM/ && NF--; {n=split($0,a,"abcd");if (n==2) print "abcd"a[2]}' file
Oct 07, 2014 7:39:10 AM
abcd = 495274900579805_10204277254604731 : a = 0 b = 0 c = 0