ARGC |
命令行参数个数 |
NF |
浏览记录的域个数 |
AGRV |
命令行参数排列 |
NR |
已读的记录数 |
ENVIRON |
支持队列中系统环境变量的使用 |
OFS |
输出域分隔符 |
FILENAME |
awk 浏览的文件名 |
ORS |
输出记录分隔符 |
FNR |
浏览文件的记录数 |
RS |
控制记录分隔符 |
FS |
设置输入域分隔符,同- F 选项 |
NF |
浏览记录的域个数 |
@H_301_122@
例:
awk 'END {print NR}' temp 在最后打印已读记录条数
awk '{print NF,NR,$0} END {print FILENAME}' temp
awk '{if (NR>0 && $4~/Brown/) print $0}' temp 至少存在一条记录且包含Brown
NF的另一用法: echo $PWD | awk -F/ '{print $NF}' 显示当前目录名
2.5. awk 操作符:
在 awk 中使用操作符,基本表达式可以划分成数字型、字符串型、变量型、域及数组元素
设置输入域到变量名:
'{name=$1;six=$3; if (six=="man") print name " is " six}'
域值比较操作:
'BEGIN {BASE="27"} {if ($4<BASE) print $0}'
修改数值域取值:(原输入文件不会被改变)
'{if ($1=="asima") $6=$6-1;print $1,$6,$7}'
修改文本域:
'{if ($1=="asima) ($1=="desc");print $1}'
只显示修改记录:(只显示所需要的,区别上一条命令,注意{})
'{if ($1=="asima) {$1=="desc";print$1}}'
创建新的输出域:
'{$4=$3-$2; print $4}'
统计列值:
'(tot+=$3);END {print tot}' temp 会显示每列的内容
'{(tot+=$3)};END {print tot}' 只显示最后的结果
|
@H_
301_122@
文件长度相加:
lsl'/^[^d]/ {print $9"\t"$5} {tot+=$5} END{print "totKB:" tot}'
@H_301_122@
只列出文件名:
@H_301_122@
2.6. awk 内置字符串函数:
gsub(r,s) 在整个$0中用s替代r
awk 'gsub(/name/,"xingming") {print $0}' temp
gsub(r,s,t) 在整个t中用s替代r
index(s,t) 返回s中字符串t的第一位置
awk 'BEGIN {print index("Sunny","ny")}' temp 返回4
length(s) 返回s的长度
match(s,r) 测试s是否包含匹配r的字符串
awk '$1=="J.Lulu" {print match($1,"u")}' temp 返回4
split(s,a,fs) 在fs上将s分成序列a
awk 'BEGIN {print split("12#345#6789",myarray,"#")"'
返回3,同时myarray[1]="12", myarray[2]="345", myarray[3]="6789"
sprint(fmt,exp) 返回经fmt格式化后的exp
sub(r,s) 从$0中最左边最长的子串中用s代替r(只更换第一遇到的匹配字符串)
substr(s,p) 返回字符串s中从p开始的后缀部分
substr(s,p,n) 返回字符串s中从p开始长度为n的后缀部分
2.7. printf 函数的使用:
字符转换: echo "65" |awk '{printf "%c\n",$0}' 输出A
awk 'BEGIN {printf "%f\n",999}' 输出999.000000
格式化输出:awk '{printf "%-15s %s\n",$1,$3}' temp 将第一个域全部左对齐显示
2.8. 其他 awk 用法:
向一行 awk 命令传值:
'{if ($5<AGE) print $0}'AGE=10 temp
who|'{if ($1==user) print $1 " are in " $2 'user=$LOGNAME 使用环境变量
@H_301_122@
awk 脚本命令:
!/bin/awk -f
# all comment lines must start with a hash '#'
# name: student_tot.awk
# to call: student_tot.awk grade.txt
# prints total and average of club student points
# print a header first
BEGIN
{
print "Student Date Member No. Grade Age Points Max"
print "Name Joined Gained Point Available"
print"========================================================="
}
# let's add the scores of points gained
(tot+=$6);
# finished processing now let's print the total and average point
END
{
print "Club student total points :" tot
print "Average Club Student points :" tot/N
}
2.9. awk 数组:
awk 的循环基本结构
For (element in array) print array[element]
awk 'BEGIN {
record="123#456#789";split(record,myarray,"#")
}
END {
for (i in myarray) {print myarray[i]}
}
3.1. 条件判断语句(if)
if(表达式) #
if(Variable in Array
语句1else2
|
@H_301_122@
格式中"语句 1"可以是多个语句,如果你为了方便 Unix awk 判断也方便你自已阅读,你最好将多个语句用{}括起来。Unix awk 分枝结构允许嵌套,其格式为:
4
5
6
{语句1}
23}
|
@H_301_122@
awk 'BEGIN{
test=100;
if(test>90)
{
print "very good";
}
else if(test>60)
{
print "good";
}
else
{
print "no pass";
}
}'
very good
每条命令语句后面可以用“;”号结尾。
3.2. 循环语句(while,for,do)
1.while 语句
格式:
while(表达式)
{语句}
例子:
6
7
8
9
10
11
[chengmo@localhost Nginx]# awk 'BEGIN{
test100;
total0whilei<=test
{
+=i++
print total}'
5050
|
@H_301_122@
2.for 循环
for 循环有两种格式:
格式 1:
for(变量 in 数组)
{语句}
例子:
10
11
12
13
14
15
for(kENVIRON
printk"="k]
AWKPATH=.:/usrshare/OLDPWD=home/web97
SSH_ASKPASSlibexecopensshgnomessh-askpass
SELINUX_LEVEL_REQUESTED=SELINUX_ROLE_REQUESTEDLANGzh_CN.GB2312
。。。。。。
|
@H_301_122@
说明:ENVIRON 是 awk 常量,是子典型数组。
格式 2:
for(变量;条件;表达式)
{语句}
10
;<=
3.do 循环
格式:
do
{语句}while(条件)
do }以上为 awk 流程控制语句,从语法上面大家可以看到,与 c 语言是一样的。有了这些语句,其实很多 shell 程序都可以交给 awk,而且性能是非常快的。
break |
当break语句用于while或for语句时,导致退出程序循环。 |
continue |
当continue语句用于while或for语句时,使程序循环移动到下一个迭代。 |
next |
能能够导致读入下一个输入行,并返回到脚本的顶部。这可以避免对当前输入行执行其他的操作过程。 |
exit |
语句使主输入循环退出并将控制转移到 END,如果 END 存在的话。如果没有定义 END 规则,或在 END 中应用 exit 语句,则终止脚本的执行。 |
@H_301_122@
|