linux shell学习笔记 3
时间:2007-03-11 来源:wangbinde
八.Awk高级应用
条件操作符
操作符 |
描述 |
< |
小于 |
>= |
大于等于 |
<= |
小于等于 |
= = |
等于 |
!= |
不等于 |
~ |
匹配正则表达式 |
!~ |
不匹配正则表达式 |
逻辑操作符
操作符 |
描述 |
&& |
and |
|| |
or |
! |
not |
[root@localhost ~]# cat awkfi.sh
#!/bin/bash
echo "210~219网段的访问量是?`awk'{f $1~/^21[0-9]/) print $0}' www.log|wc -l`"
echo "非210~219网段的访问量是:`awk '{if ($1!~/^21[0-9]/) print $0}' www.log|wc -l`"
echo "2004年07月07日的访问量是: `awk '{if ($4~/^\[07\/Jul\/2004/) print $0}' www.log|wc -l`"
echo "2004年07月07日/htm/free_call.php的访问量是? `awk '{if ($4~/^\[07\/Jul\/2004/) print $0}' www.log|awk '{if ($7=="/htm/free_call.php") print $)}' print $0}'|wc -l`"
awk的内置变量
ARGC |
命令行参数个数 |
ARGV |
命令行参数列数 |
ENVIRON |
支持队列中系统环境变量的使用 |
FILENAME |
Awk浏览的文件名 |
FNR |
浏览文件的记录数 |
FS |
设置输入域分割符,等价于命令行的-F选项 |
NF |
浏览记录的域个数 |
NR |
已读的记录数 |
OFS |
输出域分割符 |
ORS |
输出记录分割符 |
RS |
控制记录分割符 |
[root@localhost ~]# awk -F '#' '{print NF,NR, $0}' grade.txt F定义域的分隔符,NF代表$0的域的个数,NR代表已经读取的记录数
2 1 85#senior
2 2 87#junior
2 3 75#senior
2 4 75#junior
2 5 86#junior
2 6 97#senior
2 7 56#junior
2 8 76#senior
2 9 52#junior
2 10 65#senior
1 11 you have new mail in /var/spool/mail/root
[root@localhost ~]# awk -F '#' '{print NF,NR$0}' grade.txt
2 185#senior
2 287#junior
2 375#senior
2 475#junior
2 586#junior
2 697#senior
2 756#junior
2 876#senior
2 952#junior
2 1065#senior
1 11you have new mail in /var/spool/mail/root
[root@localhost ~]# cat grade.txt
85#senior
87#junior
75#senior
75#junior
86#junior
97#senior
56#junior
76#senior
52#junior
65#senior
you have new mail in /var/spool/mail/root
[root@localhost ~]# awk -F '#' '{print NF,NR,ENVIRON["USER"],$0}' grade.txt
2 1 root 85#senior
2 2 root 87#junior
2 3 root 75#senior
2 4 root 75#junior
2 5 root 86#junior
2 6 root 97#senior
2 7 root 56#junior
2 8 root 76#senior
2 9 root 52#junior
2 10 root 65#senior
1 11 root you have new mail in /var/spool/mail/root
[root@localhost ~]# awk -F '#' '{print NF,NR,ENVIRON["user"],$0,FILENAME}' grade.txt
2 1 85#senior grade.txt
2 2 87#junior grade.txt
2 3 75#senior grade.txt
2 4 75#junior grade.txt
2 5 86#junior grade.txt
2 6 97#senior grade.txt
2 7 56#junior grade.txt
2 8 76#senior grade.txt
2 9 52#junior grade.txt
2 10 65#senior grade.txt
1 11 you have new mail in /var/spool/mail/root grade.txt
[root@localhost ~]# awk -F '#' '{print NF,NR,ENVIRON["user"],$0,FILENAME,ARGC}' grade.txt
2 1 85#senior grade.txt 2 列出命令行的参数,在这个命令中分别是awk和grade.txt
2 2 87#junior grade.txt 2
2 3 75#senior grade.txt 2
2 4 75#junior grade.txt 2
2 5 86#junior grade.txt 2
2 6 97#senior grade.txt 2
2 7 56#junior grade.txt 2
2 8 76#senior grade.txt 2
2 9 52#junior grade.txt 2
2 10 65#senior grade.txt 2
1 11 you have new mail in /var/spool/mail/root grade.txt 2
[root@localhost ~]# awk -F '#' '{print NF,NR,ENVIRON["user"],$0,FILENAME,ARGC,AR
GV[0]}' grade.txt
2 1 85#senior grade.txt 2 awk ARGV[0]列出命令行参数中的第一个参数awk
2 2 87#junior grade.txt 2 awk
2 3 75#senior grade.txt 2 awk
2 4 75#junior grade.txt 2 awk
2 5 86#junior grade.txt 2 awk
2 6 97#senior grade.txt 2 awk
2 7 56#junior grade.txt 2 awk
2 8 76#senior grade.txt 2 awk
2 9 52#junior grade.txt 2 awk
2 10 65#senior grade.txt 2 awk
1 11 you have new mail in /var/spool/mail/root grade.txt 2 awk
字符串函数
gsub(r,s) |
在整个$0中用s替代r |
gsub(r,s,t) |
在整个t中用s替代r,t可以是整条记录或一个域 |
index(s,t) |
返回s中字符串t的第一位置 |
length(s) |
返回s长度 |
match(s,r) |
测试s是否包含匹配的字符串 |
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@localhost ~]# awk -F '#' '{if (gsub("#","||")) print $0}' grade.txt
85||senior if (gsub("#","||"))用||代替$0中的#
87||junior
75||senior
75||junior
86||junior
97||senior
56||junior
76||senior
52||junior
65||senior