我正在尝试解析一个日志文件,该文件将包含如下行:
aaa bbb ccc: [DDD] efg oi
aaa bbb ccc: lll [DDD] efg oo
aaa bbb ccc: [DDD]
其中[DDD]
可以位于队列中的任何位置。
任何行中的 [
和 ]
之间只有一件事
使用awk和空格作为分隔符,如何打印[
和]
之间的第一个、第三个和所有数据(整个字符串)?
预期输出:aaa ccc: DDD
最佳答案
gawk(GNU awk)方法:
假设我们有一个包含以下行的文件:
aaa bbb ccc: ddd [fff] ggg hhh
命令:
awk '{match($0,/\[([^]]+)\]/, a); print $1,$3,a[1]}' file
输出:
aaa ccc: fff
match(string, regexp [, array])
Search string for the longest, leftmost substring matched by the regular expression regexp and return the character position (index) at which that substring begins (one, if it starts at the beginning of string). If no match is found, return zero..
关于Bash 在 awk 打印中包含字符之间的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43611941/