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
阅读(794) | 评论(0) | 转发(0) |