awk用法小结

$ awk '{guojun=$1;print length(guojun)" "guojun}' readtxt
8 sq_forum
4 bgin
5 nihao
4 hehe
4 enda
4 wuya
8 sq_forum
$ awk '{guojun=$1;print length($guojun)" "$guojun}' readtxt
awk: 0602-562 Field $(sq_forum) is not correct.
The input line number is 1. The file is readtxt.
The source line number is 1.

在AWK中变量只能用本身的,不能用$符号

$ awk 'BEGIN{guojun="nihao"}{print length(guojun)" "guojun}' readtxt
5 nihao
5 nihao
5 nihao
5 nihao
5 nihao
5 nihao
5 nihao

$ awk 'begin {guojun="nihao"}{print length(guojun)" "guojun}' readtxt
0
0
0
0
0
0
0

可见赋值处必须用大写的begin

----------------------------------------------------------------------

$ vi readtxt
"readtxt" 7 lines, 68 characters
sq_forum begin
bgin a
nihao b
hehe c
enda d
wuya e
sq_forum end
wang
jing
goj

$ awk '/sq_forum begin/,/sq_forum end/' readtxt
sq_forum begin
bgin a
nihao b
hehe c
enda d
wuya e
sq_forum end

可见 awk '/sq_forum begin/,/sq_forum end/' readtxt指显示readtxt文件中sq_forum begin和sq_forum end范围段内的内容

[@more@]
请使用浏览器的分享功能分享到微信等