AIX下awk语言的内置字符串函数
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的后缀部分
——————————————————————————————————————————————————————————————
1.gsub函数
将包含4842的行中的4842替换为4899:
awk 'gsub(/4842/,4899) {print $0}' grade.txt
J.Troll 07/99 4899 Brown-3 12 26 26
awk '{if($3~/4842/) print $0}' grade.txt[or awk '$0 ~/4842/ ' grade.txt]
J.Troll 07/99 4842 Brown-3 12 26 26
2.index函数
找出ny首次出现的位置:
awk 'END {print index("Bunny","ny")}' grade.txt
4
在第一个域中包含Bunny中找出ny首次出现的位置,并打印此行:
awk '$1~/Bunny/ {print index($1,"ny") ":" $0}' grade.txt
6:P.Bunny 02/99 48 Yello 12 35 28
3.length函数
awk 'if(($1~/Tran/) || (length($1) > 7)) {print $0}' grade.txt
报错:
awk: Syntax error
at line 1 of program << if(($1~/Tran/) || ( ... >>
context is
>>> if <<< (($1~/Tran/) || (length($1) > 7)) {print $0}
awk: bailing out
at line 1 of program << if(($1~/Tran/) || ( ... >>
错误原因:所有的动作要包含在{}内。
正确:
awk '{if(($1~/Tran/) || (length($1) > 7)) {print $0}}' grade.txt
M.Tansley 05/99 48311 Green 8 40 44
L.Tansley 05/99 4712 Brown-2 12 30 28
4.match函数
awk 'BEGIN{print match("abcd",/A/)}'
0
awk 'BEGIN{print match("abcd",/c/)}'
3
awk '$1=="J.Lulu" {print match($1,"u")}' grade.txt
4
5.split函数
awk 'BEGIN {print split("123#456#789",myarray,"#")}'
3
6.sub函数
awk 'if($1~/Troll/) {print $0}' grade.txt
if必须放在{}内,否则
awk: Syntax error
at line 1 of program << if($1~/Troll/) {prin ... >>
context is
>>> if <<< ($1~/Troll/) {print $0}
awk: bailing out
at line 1 of program << if($1~/Troll/) {prin ... >>
awk '{if($1~/Troll/) {print $0}}' grade.txt
J.Troll 07/99 4842 Brown-3 12 26 26
7.substr函数
awk '$1=="L.Tansley" {print substr($1,1,5)}' grade.txt
L.Tan
没第三个参数
awk '$1=="L.Tansley" {print substr($1,1)}' grade.txt
L.Tansley
地三个参数大于域长度
awk '$1=="L.Tansley" {print substr($1,1,100)}' grade.txt
L.Tansley
指定长度
awk '$1=="L.Tansley" {print substr($1,1,length($1)-1)}' grade.txt
L.Tansle
起始位置为0
awk '$1=="L.Tansley" {print substr($1,0,length($1)-1)}' grade.txt
L.Tansle
截取整行
awk '$1=="L.Tansley" {print substr($0,1,15)}' grade.txt
L.Tansley 05/9
打印截取的数据和原数据
awk '$1=="L.Tansley" {print substr($0,1,15)} END{print $0}' grade.txt
L.Tansley 05/9
L.Tansley 05/99 4712 Brown-2 12 30 28
将截取的字符串连接一个字符串
awk '$1=="L.Tansley" {print substr($0,1,15) "___3 blanks"}' grade.txt
L.Tansley 05/9___3 blanks
取名字
awk '{print substr($1,3)}' grade.txt
Tansley
Lulu
Bunny
Troll
Tansley
8.从shell中向awk传入字符串
echo "_yeeXun" | awk '{print length($0)}'
7
STR="grade.txt"
echo $STR| awk '{print substr($STR,1,5)}'
awk: illegal field $()
input record number 1, file -
source line 1 of program << {print substr($STR,1 ... >>
错误原因:awk不认识$STR,管道命令传递过来的数据,被认为$0,所以应该将$STR替换为$0:
echo $STR| awk '{print substr($0,1,5)}'
grade
截取后缀
echo $STR| awk '{print substr($0,7)}'
txt
echo $STR | awk '{print substr($0,match($0,/\./)+1)}'
txt
取目录下的所有文件的后缀:
ls -l
total 28
-rw-r--r-- 1 xxxx group 0 Nov 19 10:12 cat
-rw-r--r-- 1 xxxx group 28 Nov 14 20:32 cat_file.txt
drwxr-xr-x 2 xxxx group 512 Nov 21 08:30 c_src
-rw-r--r-- 1 xxxx group 356 Nov 16 19:50 data.f
-rw-r--r-- 1 xxxx group 284 Nov 19 10:36 delete_me_and_die
-rwxr--r-- 1 xxxx group 61 Nov 8 09:15 first2
-rw-r--r-- 1 xxxx group 235 Nov 19 10:44 grade.txt
-rwxr--r-- 1 xxxx group 354 Nov 17 11:05 info.txt
-rwxr----- 1 xxxx group 23 Nov 7 19:12 myfile
drwxr-xr-x 2 xxxx group 512 Nov 21 08:30 sql_src
-rwxr--r-- 1 xxxx group 225 Nov 15 18:08 test.bak
-rwxr--r-- 1 xxxx group 225 Nov 15 14:12 test.sql
-rw-r--r-- 1 xxxx group 1998 Nov 15 14:15 who.out
-rw-r--r-- 1 xxxx group 229 Nov 19 14:44 wow
ls -l | awk '{print substr($9,match($9,/\./)+1)}'
cat
txt
c_src
f
delete_me_and_die
first2
txt
txt
myfile
sql_src
bak
sql
out
wow
找到有后缀名的文件
ls -l | awk '{if(match($9,/\./)>0) {print $9}}'
cat_file.txt
data.f
grade.txt
info.txt
test.bak
test.sql
who.out
然后截取后缀:
ls -l | awk '{if(match($9,/\./)>0) {print substr($9,match($9,/\./)+1)}}'
txt
f
txt
txt
bak
sql
out
将文件名字,和后缀名用“#”隔开:
ls -l | awk '{if(match($9,/\./)>0) {print $9"#" substr($9,match($9,/\./)+1)}}'
cat_file.txt#txt
data.f#f
grade.txt#txt
info.txt#txt
test.bak#bak
test.sql#sql
who.out#out
使用“>”将数据写入文件,直接覆盖;“>>”,附加。
——————————————————————————————————————————————————————————————
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的后缀部分
——————————————————————————————————————————————————————————————
1.gsub函数
将包含4842的行中的4842替换为4899:
awk 'gsub(/4842/,4899) {print $0}' grade.txt
J.Troll 07/99 4899 Brown-3 12 26 26
awk '{if($3~/4842/) print $0}' grade.txt[or awk '$0 ~/4842/ ' grade.txt]
J.Troll 07/99 4842 Brown-3 12 26 26
2.index函数
找出ny首次出现的位置:
awk 'END {print index("Bunny","ny")}' grade.txt
4
在第一个域中包含Bunny中找出ny首次出现的位置,并打印此行:
awk '$1~/Bunny/ {print index($1,"ny") ":" $0}' grade.txt
6:P.Bunny 02/99 48 Yello 12 35 28
3.length函数
awk 'if(($1~/Tran/) || (length($1) > 7)) {print $0}' grade.txt
报错:
awk: Syntax error
at line 1 of program << if(($1~/Tran/) || ( ... >>
context is
>>> if <<< (($1~/Tran/) || (length($1) > 7)) {print $0}
awk: bailing out
at line 1 of program << if(($1~/Tran/) || ( ... >>
错误原因:所有的动作要包含在{}内。
正确:
awk '{if(($1~/Tran/) || (length($1) > 7)) {print $0}}' grade.txt
M.Tansley 05/99 48311 Green 8 40 44
L.Tansley 05/99 4712 Brown-2 12 30 28
4.match函数
awk 'BEGIN{print match("abcd",/A/)}'
0
awk 'BEGIN{print match("abcd",/c/)}'
3
awk '$1=="J.Lulu" {print match($1,"u")}' grade.txt
4
5.split函数
awk 'BEGIN {print split("123#456#789",myarray,"#")}'
3
6.sub函数
awk 'if($1~/Troll/) {print $0}' grade.txt
if必须放在{}内,否则
awk: Syntax error
at line 1 of program << if($1~/Troll/) {prin ... >>
context is
>>> if <<< ($1~/Troll/) {print $0}
awk: bailing out
at line 1 of program << if($1~/Troll/) {prin ... >>
awk '{if($1~/Troll/) {print $0}}' grade.txt
J.Troll 07/99 4842 Brown-3 12 26 26
7.substr函数
awk '$1=="L.Tansley" {print substr($1,1,5)}' grade.txt
L.Tan
没第三个参数
awk '$1=="L.Tansley" {print substr($1,1)}' grade.txt
L.Tansley
地三个参数大于域长度
awk '$1=="L.Tansley" {print substr($1,1,100)}' grade.txt
L.Tansley
指定长度
awk '$1=="L.Tansley" {print substr($1,1,length($1)-1)}' grade.txt
L.Tansle
起始位置为0
awk '$1=="L.Tansley" {print substr($1,0,length($1)-1)}' grade.txt
L.Tansle
截取整行
awk '$1=="L.Tansley" {print substr($0,1,15)}' grade.txt
L.Tansley 05/9
打印截取的数据和原数据
awk '$1=="L.Tansley" {print substr($0,1,15)} END{print $0}' grade.txt
L.Tansley 05/9
L.Tansley 05/99 4712 Brown-2 12 30 28
将截取的字符串连接一个字符串
awk '$1=="L.Tansley" {print substr($0,1,15) "___3 blanks"}' grade.txt
L.Tansley 05/9___3 blanks
取名字
awk '{print substr($1,3)}' grade.txt
Tansley
Lulu
Bunny
Troll
Tansley
8.从shell中向awk传入字符串
echo "_yeeXun" | awk '{print length($0)}'
7
STR="grade.txt"
echo $STR| awk '{print substr($STR,1,5)}'
awk: illegal field $()
input record number 1, file -
source line 1 of program << {print substr($STR,1 ... >>
错误原因:awk不认识$STR,管道命令传递过来的数据,被认为$0,所以应该将$STR替换为$0:
echo $STR| awk '{print substr($0,1,5)}'
grade
截取后缀
echo $STR| awk '{print substr($0,7)}'
txt
echo $STR | awk '{print substr($0,match($0,/\./)+1)}'
txt
取目录下的所有文件的后缀:
ls -l
total 28
-rw-r--r-- 1 xxxx group 0 Nov 19 10:12 cat
-rw-r--r-- 1 xxxx group 28 Nov 14 20:32 cat_file.txt
drwxr-xr-x 2 xxxx group 512 Nov 21 08:30 c_src
-rw-r--r-- 1 xxxx group 356 Nov 16 19:50 data.f
-rw-r--r-- 1 xxxx group 284 Nov 19 10:36 delete_me_and_die
-rwxr--r-- 1 xxxx group 61 Nov 8 09:15 first2
-rw-r--r-- 1 xxxx group 235 Nov 19 10:44 grade.txt
-rwxr--r-- 1 xxxx group 354 Nov 17 11:05 info.txt
-rwxr----- 1 xxxx group 23 Nov 7 19:12 myfile
drwxr-xr-x 2 xxxx group 512 Nov 21 08:30 sql_src
-rwxr--r-- 1 xxxx group 225 Nov 15 18:08 test.bak
-rwxr--r-- 1 xxxx group 225 Nov 15 14:12 test.sql
-rw-r--r-- 1 xxxx group 1998 Nov 15 14:15 who.out
-rw-r--r-- 1 xxxx group 229 Nov 19 14:44 wow
ls -l | awk '{print substr($9,match($9,/\./)+1)}'
cat
txt
c_src
f
delete_me_and_die
first2
txt
txt
myfile
sql_src
bak
sql
out
wow
找到有后缀名的文件
ls -l | awk '{if(match($9,/\./)>0) {print $9}}'
cat_file.txt
data.f
grade.txt
info.txt
test.bak
test.sql
who.out
然后截取后缀:
ls -l | awk '{if(match($9,/\./)>0) {print substr($9,match($9,/\./)+1)}}'
txt
f
txt
txt
bak
sql
out
将文件名字,和后缀名用“#”隔开:
ls -l | awk '{if(match($9,/\./)>0) {print $9"#" substr($9,match($9,/\./)+1)}}'
cat_file.txt#txt
data.f#f
grade.txt#txt
info.txt#txt
test.bak#bak
test.sql#sql
who.out#out
使用“>”将数据写入文件,直接覆盖;“>>”,附加。
相关推荐
chenpro 2020-07-04
fendou00sd 2020-06-16
RealJianyuan 2020-06-14
cwgxiaoguizi 2020-06-05
chenpro 2020-06-02
Neptune 2020-05-31
老谢的自留地 2020-05-09
YukiRain 2020-05-08
baobaozai 2020-04-29
Proudoffaith 2020-04-08
fenxinzi 2020-03-01
zhiliang 2020-01-31
wannagonna 2020-01-13
wandererdl 2019-12-25
chenchuang 2020-01-25
jyj00 2020-01-09
fendou00sd 2020-01-07
fendou00sd 2020-01-06
PHP学习笔记 2020-01-06