ITPub博客

首页 > 应用开发 > IT综合 > awk用法小结

awk用法小结

原创 IT综合 作者:flysky0814 时间:2007-11-14 11:55:19 0 删除 编辑

$ 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@]

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/11423276/viewspace-982375/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: eval使用详解(转)
请登录后发表评论 登录
全部评论

注册时间:2008-03-31

  • 博文量
    53
  • 访问量
    393120