寻找一个有效的正则表达式来捕获以下时间戳后面的字符串:
<38>Oct 10 14:32:29 UAT01
<86>Oct 10 14:32:29 Test04
<13>Oct 10 14:35:09 Dev02
<13>Oct 10 14:35:10 Test03
答案1
鉴于问题是专门要求正则表达式:
grep -Eo '\s(\w+).$' file
UAT01
Test04
Dev02
Test0
解释:
`\s` matches any whitespace character.
`(\w+)` is the first Capturing Group
`\w+` matches any word character and it is equal to [a-zA-Z0-9_]
`+ ` Quantifier — Matches between one and unlimited times, as many times as possible.
`.` matches any character (except for line terminators)
`$` asserts position at the end of the string, or before the line terminator right at the end of the string.
cut
使用或可以更轻松地提取最后一个字符串awk
cut -d' ' -f 7 file
awk '{print $7}' file