awk的一些简单的使用
awk的基本的格式,例如:
awk -F 分格符号 命令 文件
举例:
[liuqi@ljjk bash]$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
:
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
rpm:x:37:37::/var/lib/rpm:/bin/bash
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
nscd:x:28:28:NSCD Daemon:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
rpc:x:32:32:Portmapper RPC user:/:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
mailnull:x:47:47::/var/spool/mqueue:/sbin/nologin
smmsp:x:51:51::/var/spool/mqueue:/sbin/nologin
pcap:x:77:77::/var/arpwatch:/sbin/nologin
xfs:x:43:43:X Font Server:/etc/X11/fs:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
gdm:x:42:42::/var/gdm:/sbin/nologin
ljjk:x:500:500:lingjiujianke:/home/ljjk:/bin/bash
liuqi:x:501:501::/home/liuqi:/bin/bash
[liuqi@ljjk bash]$ awk -F : '{print $1}' /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
news
uucp
operator
games
gopher
ftp
nobody
rpm
vcsa
nscd
sshd
rpc
rpcuser
nfsnobody
mailnull
smmsp
pcap
xfs
ntp
gdm
ljjk
liuqi
[liuqi@ljjk bash]$
可以看到这个例子是以":"符号为分格符号的,$1的意思是第一列的内容,;同理,$3是第三列的内容,例如:
[liuqi@ljjk bash]$ awk -F : '{print $3}' /etc/passwd
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
99
37
69
28
74
32
29
65534
47
51
77
43
38
42
500
501
[liuqi@ljjk bash]$
如果想同时读取两列的内容,那就如下例:
[liuqi@ljjk bash]$ awk -F : '{print $3,$1}' /etc/passwd
0 root
1 bin
2 daemon
3 adm
4 lp
5 sync
6 shutdown
7 halt
8 mail
9 news
10 uucp
11 operator
12 games
13 gopher
14 ftp
99 nobody
37 rpm
69 vcsa
28 nscd
74 sshd
32 rpc
29 rpcuser
65534 nfsnobody
47 mailnull
51 smmsp
77 pcap
43 xfs
38 ntp
42 gdm
500 ljjk
501 liuqi
[liuqi@ljjk bash]$
这个时候看到的输出内容不够美观,可以使用转意符号输出,例如:
[liuqi@ljjk bash]$ awk -F : '{print $3,"\t",$1}' /etc/passwd
0 root
1 bin
2 daemon
3 adm
4 lp
5 sync
6 shutdown
7 halt
8 mail
9 news
10 uucp
11 operator
12 games
13 gopher
14 ftp
99 nobody
37 rpm
69 vcsa
28 nscd
74 sshd
32 rpc
29 rpcuser
65534 nfsnobody
47 mailnull
51 smmsp
77 pcap
43 xfs
38 ntp
42 gdm
500 ljjk
501 liuqi
[liuqi@ljjk bash]$
这样看起来会好受一点.
$1 $3等都知道了,但是$0有点不一样,就是全部输出,例如:
[liuqi@ljjk bash]$ awk -F : '{print $0}' /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
:
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
rpm:x:37:37::/var/lib/rpm:/bin/bash
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
nscd:x:28:28:NSCD Daemon:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
rpc:x:32:32:Portmapper RPC user:/:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
mailnull:x:47:47::/var/spool/mqueue:/sbin/nologin
smmsp:x:51:51::/var/spool/mqueue:/sbin/nologin
pcap:x:77:77::/var/arpwatch:/sbin/nologin
xfs:x:43:43:X Font Server:/etc/X11/fs:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
gdm:x:42:42::/var/gdm:/sbin/nologin
ljjk:x:500:500:lingjiujianke:/home/ljjk:/bin/bash
liuqi:x:501:501::/home/liuqi:/bin/bash
[liuqi@ljjk bash]$
;为了美观和方便还可以使用BEGIN END模式,例如
[liuqi@ljjk bash]$ awk -F : 'BEGIN {print "username\n------"}{print $1} ' /etc/passwd
username
------
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
news
uucp
operator
games
gopher
ftp
nobody
rpm
vcsa
nscd
sshd
rpc
rpcuser
nfsnobody
mailnull
smmsp
pcap
xfs
ntp
gdm
ljjk
liuqi
[liuqi@ljjk bash]$ awk -F : 'BEGIN {print "username\n------"}{print $1} END {print "-------\nend of file"}' /etc/passwd
username
------
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
news
uucp
operator
games
gopher
ftp
nobody
rpm
vcsa
nscd
sshd
rpc
rpcuser
nfsnobody
mailnull
smmsp
pcap
xfs
ntp
gdm
ljjk
liuqi
-------
end of file
[liuqi@ljjk bash]$
Awk的条件操作,~是匹配,例如:
[liuqi@ljjk bash]$ awk -F : 'BEGIN {print
"username\n------"} {if ($1 ~/ljjk/) print $1} END {print "-------\nend
of file"}' /etc/passwd
username
------
ljjk
-------
end of file
[liuqi@ljjk bash]$
| 是或的意思
[liuqi@ljjk
bash]$ awk -F : 'BEGIN {print "username\n------"} {if ($1
!~/ljjk|liuqi/) print $1} END {print "-------\nend of file"}'
/etc/passwd
username
------
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
news
uucp
operator
games
gopher
ftp
nobody
rpm
vcsa
nscd
sshd
rpc
rpcuser
nfsnobody
mailnull
smmsp
pcap
xfs
ntp
gdm
-------
end of file
[liuqi@ljjk bash]$
!~是不匹配的
[liuqi@ljjk bash]$ awk -F : 'BEGIN {print "username\n------"} {if ($1 !~/ljjk/)
print $1} END {print "-------\nend of file"}' /etc/passwd
username
------
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
news
uucp
operator
games
gopher
ftp
nobody
rpm
vcsa
nscd
sshd
rpc
rpcuser
nfsnobody
mailnull
smmsp
pcap
xfs
ntp
gdm
liuqi
-------
end of file
[liuqi@ljjk bash]$
==是判断是否是相等,例如:
[liuqi@ljjk bash]$ awk -F : 'BEGIN {print
"username\n------"} {if ($3==500) print $1} END {print "-------\nend of
file"}' /etc/passwd
username
------
ljjk
-------
end of file
[liuqi@ljjk bash]$
根据参数之间作判断,例如第三个参数小于第四个参数:
[liuqi@ljjk bash]$ awk -F : 'BEGIN {print "username\n------"} {if ($3<$4) print
$1} END {print "-------\nend of file"}' /etc/passwd
username
------
adm
lp
mail
news
uucp
games
gopher
ftp
-------
end of file
[liuqi@ljjk bash]$
按照多条件查询,例如:
[liuqi@ljjk bash]$ awk -F : 'BEGIN {print
"username\n------"} {if ($1=="ljjk"&&$3==501) print $1} END
{print "-------\nend of file"}' /etc/passwd
username
------
-------
end of file
[liuqi@ljjk
bash]$ awk -F : 'BEGIN {print "username\n------"} {if
($1=="ljjk"&&$3==500) print $1} END {print "-------\nend of
file"}' /etc/passwd
username
------
ljjk
-------
end of file
[liuqi@ljjk
bash]$ awk -F : 'BEGIN {print "username\n------"} {if
($1=="ljjk"||$3==501) print $1} END {print "-------\nend of file"}'
/etc/passwd
username
------
ljjk
liuqi
-------
end of file
[liuqi@ljjk
bash]$ awk -F : 'BEGIN {print "username\n------"} {if
($1=="ljjk"||$3==500) print $1} END {print "-------\nend of file"}'
/etc/passwd
username
------
ljjk
-------
end of file
[liuqi@ljjk bash]$
awk 还有一些内置变量:
ARGC 命令行参数个数
ARGV 命令行参数排列
ENVIRON 支持队列中系统环境变量的使用
FILENAME awk浏览的文件名
FNR 浏览文件的记录数
FS 设置输入域分格符,等价于命令行F的选项
NF 浏览记录的域个数
NR 已读的记录数
OFS 输出域分格符
ORS 输出记录分格符
RS 控制记录分格符
先举个简单的例子:
[liuqi@ljjk bash]$ awk -F : 'BEGIN {print
"username\n------"} {if ($1=="ljjk"||$3==501) print $1,"\t\t", NR} END
{print "-------\nend of file"}' /etc/passwd
username
------
ljjk 30
liuqi 31
-------
end of file
[liuqi@ljjk bash]$
这里的30和31是在文件中的30和31行,也就是读了30,31次记录
也可以重新付值一个域,例如:
[liuqi@ljjk
bash]$ awk -F : 'BEGIN {print "username\n------"} {if
($1=="ljjk"||$3==501) name= $1; print $1,"\t\t", NR, "\t\t",name} END
{print "-------\nend of
file"}' /etc/passwd
username
------
root 1
bin 2
daemon 3
adm 4
lp 5
sync 6
shutdown 7
halt 8
mail 9
news 10
uucp 11
operator 12
games 13
gopher 14
ftp 15
nobody 16
rpm 17
vcsa 18
nscd 19
sshd 20
rpc 21
rpcuser 22
nfsnobody 23
mailnull 24
smmsp 25
pcap 26
xfs 27
ntp 28
gdm 29
ljjk 30 ljjk
liuqi 31 liuqi
-------
end of file
[liuqi@ljjk bash]$
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的后缀部分
awk还可以和管道一起使用,例如:
[liuqi@ljjk bash]$ df | awk '{print $1,"\t\t",$4}'
文件系统 可用
/dev/hda1 1353736
none 62996
//192.168.5.89/linux 2787584
[liuqi@ljjk bash]$ awk -F : '{print $1,$3,$4,$6,$7}' /etc/passwd|grep ljjk
ljjk 500 500 /home/ljjk /bin/bash
[liuqi@ljjk bash]$
也可以man来查看awk的详细介绍
阅读(1868) | 评论(0) | 转发(1) |