免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1265 | 回复: 0
打印 上一主题 下一主题

awk内置变量、操作符、函数 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-01-29 16:49 |只看该作者 |倒序浏览

a:awk内置变量
变量名 含义
ARGC 命令行参数个数
ARGV 命令行参数数组
FILENAME string=当前输入的文件名
FNR 在当前文件中当前记录数(对输入文件起始为1)
FS 输入字段分隔符
NF 当前记录的字段数
NR 当前记录数(为全部输入文件)
OFMT 数值的输出格式(默认为%.6g)
OFS 输出字段的分隔符(默认为空格)
ORS 输出记录分隔符(默认为换行符)
RS 输入记录分隔符(默认为换行符)
awk允许的测试:
b:操作符 含义
x==y x等于y?
x!=y x不等于y?
x>y x大于y?
x>=y x大于或等于y?
x >=

d:awk内置字符串函数
gsub(r,s)       在整个$0中用s替代r
gsub(r,s,t)     在整个t中用s替代r
index(s,t)      返回s中字符串t的第一位置
length(s)       返回s长度
match(s,r)      测试s是否包含匹配r的字符串
split(s,a,fs)   在fs上将s分成序列a
sprint(fmt,exp) 返回经fmt格式化后的exp
sub(r,s)        用$0中最左边最长的子串代替s
substr(s,p)     返回字符串s中从p开始的后缀部分
substr(s,p,n)   返回字符串s中从p开始长度为n的后缀部分
依次举例:
[root@codfei5 Bash]# cat test.txt
M.Tansley       05/99   48311   Green   8       40      44
J.Lulu          06/99   48317   green   9       24      26
P.Bunny         02/99   48      Yellow  12      35      28
J.Troll         07/99   4842    Brown-3 12      26      26
L.Tansley       05/99   4712    Brown-2 12      30      28
[root@codfei5 Bash]# awk 'gsub(4842,4899){print $0}' test.txt
J.Troll         07/99   4899    Brown-3 12      26      26
[root@codfei5 Bash]# awk 'BEGIN{print index("Bunny","ny")}'
4
[root@codfei5 Bash]# awk '$1=="J.Troll"{print length($1)}' test.txt
7
[root@codfei5 Bash]# awk 'BEGIN {print length("codfei is handsome")}'
18
[root@codfei5 Bash]# awk 'BEGIN {print match("ANCD",/C/)}'
3
[root@codfei5 Bash]# awk 'BEGIN {print split("123-456-789--",array,"-")}'
5
[root@codfei5 Bash]# cat test.txt|awk 'sub(26,29)'
J.Lulu          06/99   48317   green   9       24      29
J.Troll         07/99   4842    Brown-3 12      29      26
#匹配每行第一个26
[root@codfei5 Bash]# awk '$1=="L.Tansley" {print substr($1,1,5)}' test.txt
L.Tan
#上面例子中,指定在域1的第一个字符开始,返回其前面5个字符。
[root@codfei5 Bash]# awk '$1=="L.Tansley" {print substr($1,3,99)}' test.txt
Tansley
#如果给定长度值远大于字符串长度,awk将从起始位置返回所有字符,要抽取 L.Tansley
#的姓,只需从第3个字符开始返回长度为7。可以输入长度99,awk返回结果相同。
[root@codfei5 Bash]# awk '{print substr($1,3)}' test.txt
Tansley
Lulu
Bunny
Troll
Tansley
[root@codfei5 Bash]# str="say hello to you "
[root@codfei5 Bash]# echo $str| awk '{print substr($str,1,3)}'
say
[root@codfei5 Bash]# echo $str| awk '{print substr($str,5,5)}'
hello
[root@codfei5 Bash]# echo $str| awk '{print substr($str,11,2)}'
to
[root@codfei5 Bash]# echo $str| awk '{print substr($str,14,3)}'
you
[root@codfei5 Bash]# echo $str| awk '{print substr($str,14)}'
you
[root@codfei5 Bash]# echo $str| awk '{print substr($str,11)}'
to you
[root@codfei5 Bash]# echo $str| awk '{print substr($str,5)}'
hello to you

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/103643/showart_2164284.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP