awk内置字符串函数
内置字符串函数
gsub(r,s) 在输入文件中用s替换r
gsub(r,s,t) 在t中用s替换r
index(s,t) 返回s中字符串第一个t的位置
length(s) 返回s的长度
match(s,t) 测试s是否包含匹配t的字符串
split(r,s,t) 在t上将r分成序列s
sub(r,s,t) 将t中第一次出现的r替换成s
substr(r,s) 返回字符串中r中从s开始的后缀部分
substr(r,s,t) 返回字符串中r中从s开始长度为t的后缀部分
gsub函数执行字符串替换功能,将第一个字符串替换为第二个字符串,gsub函数有两种形式,第一个是作用于全部域,即$0,第二个是作用于域t
示例:
gsub(r,s,t) 在t中用s替换r
将passwd文件使用:分隔符分隔,然后将第一个域的root使用gridsphere进行替换,然后在打印全域
awk 'BEGIN {FS=":";OFS=":"} gsub(/root/,"gridsphere",$1) {print $0}' passwd
[root@oraclehost tmp]# awk 'BEGIN {FS=":";OFS=":"} gsub(/root/,"gridsphere",$1) {print $0}' passwd
gridsphere:x:0:0:root:/root:/bin/bash
<gridsphere>:x:0:0:root:/root:/bin/bash
gsub(r,s) 在输入文件中用s替换r
将passwd文件使用:分隔符分隔,然后将全部域的root使用gridsphere进行替换,然后在打印全域
[root@oraclehost tmp]# awk 'BEGIN {FS=":";OFS=":"} gsub(/root/,"gridsphere") {print $0}' passwd
gridsphere:x:0:0:gridsphere:/gridsphere:/bin/bash
<gridsphere>:x:0:0:gridsphere:/gridsphere:/bin/bash
operator:x:11:0:operator:/gridsphere:/sbin/nologin
index(s,t) 返回s中字符串第一个t的位置,即t第一次出现在s中的位置
[root@oraclehost tmp]# awk 'BEGIN {print index("gridsphere","ph")}'
6
[root@oraclehost tmp]# awk 'BEGIN {print length("gridsphere")}'
10
match(s,t) 测试s是否包含匹配t的字符串,注意返回的是匹配的字符串出现的位置
[root@oraclehost tmp]# awk 'BEGIN {print match("gridsphere","aa")}'
0
[root@oraclehost tmp]# awk 'BEGIN {print match("gridsphere","phere")}'
6
[root@oraclehost tmp]# awk 'BEGIN {print match("gridsphere","ere")}'
8
//不区分大小写IGNORECASE=1
[root@oraclehost tmp]# awk 'BEGIN {IGNORECASE=1;print match("gridsphere",/D/)}'
4
[root@oraclehost tmp]# awk 'BEGIN {print match("gridsphere",/D/)}'
0
split(r,s,t) 在t上将r分成序列s,简单解释就是使用t这个分隔符将r这个字符串分隔开,然后每个分隔开的放到数据s[]当中,split (string, array) -->如果第三个参数没有提供,awk就默认使用当前FS值
awk '{split($0,a,":");for(i=1; i<=3; i++) print a[i]}' passwd
[root@oraclehost tmp]# awk '{split($0,sting,":"); for(i=1;i<=3;i++) print sting[i]}' passwd
root
x
0
<root>
x
0
bin
x
1
daemon
x
2
adm
x
3
lp
x
4
sync
x
5
shutdown
x
6
halt
x
7
mail
x
8
operator
x
11
games
x
12
ftp
x
14
nobody
x
99
systemd-network
x
192
dbus
x
81
polkitd
x
999
sshd
x
74
postfix
x
89
mysql
x
1000
oracle
x
1001
vastbase
x
1002
tss
x
59
postgres
x
2000
ntp
x
38
atlasdb
x
998
haproxy
x
188
geoclue
x
997
rpc
x
32
gluster
x
996
libstoragemgmt
x
995
qemu
x
107
unbound
x
994
usbmuxd
x
113
chrony
x
993
saslauth
x
992
saned
x
991
abrt
x
173
colord
x
990
radvd
x
75
rtkit
x
172
pulse
x
171
setroubleshoot
x
989
sssd
x
988
rpcuser
x
29
nfsnobody
x
65534
gdm
x
42
gnome-initial-setup
x
987
tcpdump
x
72
avahi
x
70
grid
x
2001
取第一列
[root@oraclehost tmp]# awk '{split($0,sting,":");print sting[1]}' passwd
root
<root>
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody
systemd-network
dbus
polkitd
sshd
postfix
mysql
oracle
vastbase
tss
postgres
ntp
atlasdb
haproxy
geoclue
rpc
gluster
libstoragemgmt
qemu
unbound
usbmuxd
chrony
saslauth
saned
abrt
colord
radvd
rtkit
pulse
setroubleshoot
sssd
rpcuser
nfsnobody
gdm
gnome-initial-setup
tcpdump
avahi
grid
sub(r,s,t) 将t中第一次出现的r替换成s
[root@oraclehost tmp]# awk 'BEGIN {str="abcdnjhednfgjrehgir";sub(/jhed/,"JHED",str);printf("%s",str)}'
abcdnJHEDnfgjrehgir
substr(r,s) 返回字符串中r中从s开始的后缀部分
[root@oraclehost tmp]# awk 'BEGIN {str="abcdnjhednfgjrehgir";print substr(str,6)}'
jhednfgjrehgir
substr(r,s,t) 返回字符串中r中从s开始长度为t的后缀部分
[root@oraclehost tmp]# awk 'BEGIN {str="abcdnjhednfgjrehgir";print substr(str,6,10)}'
jhednfgjre
浙公网安备 33010602011771号