首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网络技术 > 网络基础 >

AIX上awk语言的内置字符串函数

2012-11-26 
AIX下awk语言的内置字符串函数awk语言内置的字符串函数————————————————————————————————————————————————

AIX下awk语言的内置字符串函数

awk语言内置的字符串函数——————————————————————————————————————————————————————————————gsub(r,s)在整个$0中用s替代rgsub(r,s,t)在整个t中用s替代rindex(s,t)返回s中字符串t的第一位置length(s)返回s的长度match(s,r)测试s是否包含匹配r的字符串split(s,a,fs)返回fs上将s分成序列asprint(fmt,exp)返回经fmt格式化后的expsub(r,s)用$0中最左边最长的子串替代ssubstr(s,p)返回字符串s中从p开始的后缀部分substr(s,p,n)返回字符串s中从p开始长度为n的后缀部分——————————————————————————————————————————————————————————————1.gsub函数将包含4842的行中的4842替换为4899:awk 'gsub(/4842/,4899) {print $0}' grade.txtJ.Troll    07/99  4899   Brown-3  12   26   26awk '{if($3~/4842/) print $0}' grade.txt[or awk '$0 ~/4842/ ' grade.txt]J.Troll    07/99  4842   Brown-3  12   26   262.index函数找出ny首次出现的位置:awk 'END {print index("Bunny","ny")}' grade.txt4在第一个域中包含Bunny中找出ny首次出现的位置,并打印此行:awk '$1~/Bunny/ {print index($1,"ny") ":" $0}' grade.txt6:P.Bunny    02/99  48     Yello    12   35   283.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.txtM.Tansley  05/99  48311  Green    8    40   44L.Tansley  05/99  4712   Brown-2  12   30   284.match函数awk 'BEGIN{print match("abcd",/A/)}'0awk 'BEGIN{print match("abcd",/c/)}'3awk '$1=="J.Lulu" {print match($1,"u")}' grade.txt45.split函数awk 'BEGIN {print split("123#456#789",myarray,"#")}'36.sub函数awk 'if($1~/Troll/) {print $0}' grade.txtif必须放在{}内,否则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.txtJ.Troll    07/99  4842   Brown-3  12   26   267.substr函数awk '$1=="L.Tansley" {print substr($1,1,5)}' grade.txtL.Tan没第三个参数awk '$1=="L.Tansley" {print substr($1,1)}' grade.txtL.Tansley地三个参数大于域长度awk '$1=="L.Tansley" {print substr($1,1,100)}' grade.txtL.Tansley指定长度awk '$1=="L.Tansley" {print substr($1,1,length($1)-1)}' grade.txtL.Tansle起始位置为0awk '$1=="L.Tansley" {print substr($1,0,length($1)-1)}' grade.txtL.Tansle截取整行awk '$1=="L.Tansley" {print substr($0,1,15)}' grade.txtL.Tansley  05/9打印截取的数据和原数据awk '$1=="L.Tansley" {print substr($0,1,15)} END{print $0}' grade.txtL.Tansley  05/9L.Tansley  05/99  4712   Brown-2  12   30   28将截取的字符串连接一个字符串awk '$1=="L.Tansley" {print substr($0,1,15) "___3 blanks"}' grade.txtL.Tansley  05/9___3 blanks取名字awk '{print substr($1,3)}' grade.txtTansleyLuluBunnyTrollTansley8.从shell中向awk传入字符串echo "_yeeXun" | awk '{print length($0)}'7STR="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)}'txtecho $STR | awk '{print substr($0,match($0,/\./)+1)}'txt取目录下的所有文件的后缀:ls -ltotal 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.txtdrwxr-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 myfiledrwxr-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 wowls -l | awk '{print substr($9,match($9,/\./)+1)}'cattxtc_srcfdelete_me_and_diefirst2txttxtmyfilesql_srcbaksqloutwow找到有后缀名的文件ls -l | awk '{if(match($9,/\./)>0) {print $9}}'cat_file.txtdata.fgrade.txtinfo.txttest.baktest.sqlwho.out然后截取后缀:ls -l | awk '{if(match($9,/\./)>0) {print substr($9,match($9,/\./)+1)}}'txtftxttxtbaksqlout将文件名字,和后缀名用“#”隔开:ls -l | awk '{if(match($9,/\./)>0) {print $9"#" substr($9,match($9,/\./)+1)}}'cat_file.txt#txtdata.f#fgrade.txt#txtinfo.txt#txttest.bak#baktest.sql#sqlwho.out#out使用“>”将数据写入文件,直接覆盖;“>>”,附加。

--the end--

热点排行