使用awk在列的开头和结尾添加花括号

时间:2018-04-14 20:26:16

标签: awk

我想在日志文件的整个第五列中添加一组花括号,以准备存储在数据库中。

日志文件:

45324342342 192.168.0.10 10.0.2.15 www.example.com {8.8.8.8,8.8.4.4} 95

到目前为止,我只能将其添加到列的末尾:

awk '$5=$5"}"' file

我不确定如何引用列的开头。

1 个答案:

答案 0 :(得分:1)

$ # OP's attempt
$ echo '1 2 3 4 5 6 7' | awk '$5=$5"}"'
1 2 3 4 5} 6 7

$ # answer mentioned in comments
$ # 5th field is changed to concatenation of {, $5 and }
$ # $0 gets printed as resulting non empty string serves as true condition
$ echo '1 2 3 4 5 6 7' | awk '$5="{"$5"}"'
1 2 3 4 {5} 6 7