免费注册 查看新帖 |

Chinaunix

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

awk 学习笔记 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-01-16 17:43 |只看该作者 |倒序浏览
awk:
tfengjun@shaseng:~# more f1.txt
J.aa 02/99 44 Blue 23 33 33
P.tfj 03/12 88 Golden 24 34 56
tfengjun@shaseng:~# awk '{print $0}' f1.txt   % $0代表所有域
J.aa 02/99 44 Blue 23 33 33
P.tfj 03/12 88 Golden 24 34 56
tfengjun@shaseng:~# awk '{print $1}' f1.txt  
J.aa
P.tfj
tfengjun@shaseng:~# awk '{print $2}' f1.txt  
02/99
03/12
tfengjun@shaseng:~# awk '{print $3}' f1.txt  
44
88
tfengjun@shaseng:~# awk '{print $4}' f1.txt  
Blue
Golden
tfengjun@shaseng:~# awk '{print $9}' f1.txt  
tfengjun@shaseng:~# awk '{print $1,$4,$3}' f1.txt  % $n 之间的逗号也可换成"\t"
J.aa Blue 44
P.tfj Golden 88
tfengjun@shaseng:~# awk 'BEGIN {print "Name  belt\n-----------"} {print $1,$4} END {print "-----------\nend of report"}' f1.txt        
Name  belt
-----------
J.aa Blue
P.tfj Golden
-----------
end of report
tfengjun@shaseng:~# awk '{if($1~/tfj/) print $0}' f1.txt    % ~匹配, !~不匹配
P.tfj 03/12 88 Golden 24 34 56
tfengjun@shaseng:~# awk '{if($1 !~ /tfj/) print $0}' f1.txt
J.aa 02/99 44 Blue 23 33 33
tfengjun@shaseng:~# awk '{if($0~/tfj/) print $0}' f1.txt   
P.tfj 03/12 88 Golden 24 34 56
tfengjun@shaseng:~# awk '$5=="23" {print $0}' f1.txt   % 精确匹配
J.aa 02/99 44 Blue 23 33 33
tfengjun@shaseng:~# awk '$5==23 {print $0}' f1.txt   
J.aa 02/99 44 Blue 23 33 33
tfengjun@shaseng:~# awk '$5 ~/(23|24)/' f1.txt  % regxp语法
J.aa 02/99 44 Blue 23 33 33
P.tfj 03/12 88 Golden 24 34 56
tfengjun@shaseng:~# awk '{if ($1=="P.tfj" && $5>23) print $0}' f1.txt    % && || !
P.tfj 03/12 88 Golden 24 34 56
表9-3 awk内置变量
ARGC 命令行参数个数
ARGV 命令行参数排列
ENVIRON 支持队列中系统环境变量的使用
FILENAME awk浏览的文件名
FNR 浏览文件的记录数
FS 设置输入域分隔符,等价于命令行- F选项
NF 浏览记录的域个数
NR 已读的记录数
OFS 输出域分隔符
ORS 输出记录分隔符
RS 控制记录分隔符
tfengjun@shaseng:~# awk '{print $0} END {print FILENAME}' f1.txt   
J.aa 02/99 44 Blue 23 33 33
P.tfj 03/12 88 Golden 24 34 56
f1.txt
重命名域(方便记忆与理解): [分号;分开awk命令]
tfengjun@shaseng:~# awk '{name=$1;grade=$5; if(grade>10) print name" is in grade "grade}' f1.txt
J.aa is in grade 23
P.tfj is in grade 24
修改域(副本):
tfengjun@shaseng:~# awk '{if($1=="P.tfj") ($1="Eric.tian"); print $1}' f1.txt  
J.aa
Eric.tian
tfengjun@shaseng:~# awk '{if($1=="P.tfj") {$1="Eric.tian";print $1}}' f1.txt    % 注意大括号的位置
Eric.tian
增加新的域:
tfengjun@shaseng:~# awk 'BEGIN {print "Name\tScore"} {if($6<$7) {diff=$6-$7; print $1"\t"diff}}' f1.txt   % 也可使用$8=$6-$7
Name    Score
P.tfj   -22
统计:
tfengjun@shaseng:~# awk '{(total+=$6)}; END {print "total score: " total}' f1.txt
total score: 67
tfengjun@shaseng:~# ls -l | awk ' /^[^d]/ {tot+=$5} END {print "total KB:" tot}'   %统计文件夹大小(不包括子文件夹)
total KB:4343
表9-4 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) 返回经f m t格式化后的e x p
sub(r,s) 用$ 0中最左边最长的子串代替s
substr(s,p) 返回字符串s中从p开始的后缀部分
substr(s,p,n) 返回字符串s中从p开始长度为n的后缀部分
tfengjun@shaseng:~# gawk '{gsub(/tfj/,"aa"); {print $0}}' f1.txt   %注意是gawk! osol自带的awk不支持!
J.aa 02/99 44 Blue 23 33 33
P.aa 03/12 88 Golden 24 34 56
               
               
               

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

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP