awk内建函数

字符串函数

sub(regular expression,substitution string);
sub(regular expression,substitution string,target string);
 [root@xieqichao ~]# awk '{sub("Tom","Tommy"); print}' employees   #这里使用Tommy替换了Tom。
  Tommy Jones       4424    5/12/66         543354

当正则表达式Tom在第一个域中第一次被匹配后,
他将被字符串"Tommy"替换,如果将sub函数的第三个参数改为$2,将不会有替换发生。

[root@xieqichao ~]# awk '{sub("Tom","Tommy",$1); print}' employees
  Tommy Jones       4424    5/12/66         543354

gsub(regular expression,substitution string);

gsub(regular expression,substitution string,target string);
和sub不同的是,如果第一个参数中正则表达式在记录中出现多次,那么gsub将完成多次替换,而sub只是替换第一次出现的。

index(string,substring)

该函数将返回第二个参数在第一个参数中出现的位置,偏移量从1开始。

   [root@xieqichao ~]# awk 'BEGIN{print index("hello","el")}'
    2

length(string)

该函数返回字符串的长度。

 [root@xieqichao ~]# awk 'BEGIN{print length("hello")}'
 5

substr(string,starting position)

substr(string,starting position,length of string)
该函数返回第一个参数的子字符串,其截取起始位置为第二个参数(偏移量为1),截取长度为第三个参数,如果没有该参数,则从第二个参数指定的位置起,直到string的末尾。

 [root@xieqichao ~]#  awk 'BEGIN{name = substr("Hello World",2,3); print name}'
 ell

match(string,regular expression)

该函数返回在字符串中正则表达式位置的索引,如果找不到指定的正则表达式就返回0.match函数设置内置变量RSTART为字符串中子字符串的开始位置,RLENGTH为到字字符串末尾的字符个数。

[root@xieqichao ~]# awk 'BEGIN{start=match("Good ole CHINA", /[A-Z]+$/); print start}'
   10

上例中的正则表达式[A-Z]+$表示在字符串的末尾搜索连续的大写字母。在字符串"Good ole CHINA"的第10个位置找到字符串"CHINA"。

[root@xieqichao ~]# awk 'BEGIN{start=match("Good ole CHINA", /[A-Z]+$/); print RSTART, RLENGTH}'
    10 5

RSTART表示匹配时的起始索引,RLENGTH表示匹配的长度。

[root@xieqichao ~]# awk 'BEGIN{string="Good ole CHINA";\
start=match(string, /[A-Z]+$/); print substr(string,RSTART, RLENGTH)}'
    CHINA

这里将match、RSTART、RLENGTH和substr巧妙的结合起来了。

toupper(string)

tolower(string)

以上两个函数分别返回参数字符串的大写和小写的形式。

  [root@xieqichao ~]# awk 'BEGIN {print toupper("hello"); print tolower("WORLD")}'
  HELLO
  world

split(string,array,field seperator)

split(string,array)

该函数使用作为第三个参数的域分隔符把字符串分隔为一个数组。如果第三个参数没有提供,则使用当前默认的FS值。

  [root@xieqichao ~]# awk 'BEGIN{split("11/20/2011",date,"/"); print date[2]}'
  20
  variable = sprintf("string with format specifiers ",expr1,expr2,...)

该函数和printf的差别等同于C语言中printf和sprintf的差别。前者将格式化后的结果输出到输出流,而后者输出到函数的返回值中。

 [root@xieqichao ~]# awk 'BEGIN{line = sprintf("%-15s %6.2f ", "hello",4.2); print line}'
 hello             4.20

时间函数:

systime()

该函数返回当前时间距离1970年1月1日之间相差的秒数。

   [root@xieqichao ~]# awk 'BEGIN{print systime()}'
   1321369554
strftime()

时间格式化函数,其格式化规则等同于C语言中的strftime函数提供的规则,见以下列表:

数据格式 含义
%a Abbreviated weekday name
%A Full weekday name
%b Abbreviated month name
%B Full month name
%c Date and time representation appropriate for locale
%d Day of month as decimal number (01 – 31)
%H Hour in 24-hour format (00 – 23)
%I Hour in 12-hour format (01 – 12)
%j Day of year as decimal number (001 – 366)
%m Month as decimal number (01 – 12)
%M Minute as decimal number (00 – 59)
%p Current locale’s A.M./P.M. indicator for 12-hour clock
%S Second as decimal number (00 – 59)
%U Week of year as decimal number, with Sunday as first day of week (00 – 53)
%w Weekday as decimal number (0 – 6; Sunday is 0)
%W Week of year as decimal number, with Monday as first day of week (00 – 53)
%x Date representation for current locale
%X Time representation for current locale
%y Year without century, as decimal number (00 – 99)
%Y Year with century, as decimal number
    [root@xieqichao ~]# awk 'BEGIN{ print strftime("%D",systime())}'
    11/15/11
    [root@xieqichao ~]# awk 'BEGIN{ now = strftime("%T"); print now}'
    23:17:29

内置数学函数:

名称 返回值
atan2(x,y) y,x范围内的余切
cos(x) 余弦函数
exp(x) 求幂
int(x) 取整
log(x) 自然对数
sin(x) 正弦函数
sqrt(x) 平方根

    [root@xieqichao ~]# awk 'BEGIN{print 31/3}'
    10.3333
    [root@xieqichao ~]# awk 'BEGIN{print int(31/3)}'
    10

自定义函数:

自定义函数可以放在awk脚本的任何可以放置模板和动作的地方。

 function name(parameter1,parameter2,...) {
     statements
     return expression
 }

给函数中本地变量传递值。只使用变量的拷贝。数组通过地址或者指针传递,所以可以在函数内部直接改变数组元素的值。函数内部使用的任何没有作为参数传递的变量都被看做是全局变量,也就是这些变量对于整个程序都是可见的。如果变量在函数中发生了变化,那么就是在整个程序中发生了改变。唯一向函数提供本地变量的办法就是把他们放在参数列表中,这些参数通常被放在列表的最后。如果函数调用没有提供正式的参数,那么参数就初始化为空。return语句通常就返回程序控制并向调用者返回一个值。

 [root@xieqichao ~]# cat grades
 20 10
 30 20
 40 30

 [root@xieqichao ~]# cat add.sc
 function add(first,second) {
         return first + second
 }
 { print add($1,$2) }

 [root@xieqichao ~]# awk -f add.sc grades
 30
 50
 70
发布了352 篇原创文章 · 获赞 52 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/xie_qi_chao/article/details/105037996
今日推荐