awk built-in functions

String Functions

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);
and the sub is different, if the first regular expression parameter appears multiple times in the record, it will complete several gsub Alternatively, the sub simply replaces the first occurrence of.

index(string,substring)

The function returns the position of the second parameter appears in the first parameter, the offset from the beginning.

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

length(string)

This function returns the length of the string.

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

substr(string,starting position)

substr (string, starting position, length of string)
This function returns the substring of the first argument, its initial position, taken as the second parameter (offset 1), a length, taken as the third argument, if without this parameter, starting from a position specified by the second argument, until the end of the string.

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

match(string,regular expression)

This function returns the string of regular expression index position, if not find the specified regular expression returns 0.match function sets the built-in variable RSTART for the beginning of the string neutron string, RLENGTH is to string words the number of character at the end.

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

Examples of the regular expression [AZ] + $ capital letters indicates a continuous search at the end of the string. Find the string "CHINA" in the string "Good ole CHINA" 10th position.

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

RSTART indicates when starting index matching, RLENGTH represents the length matching.

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

Here the match, RSTART, RLENGTH and substr clever combination up.

toupper(string)

tolower(string)

Two or more functions return form of uppercase and lowercase string parameters.

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

split(string,array,field seperator)

split(string,array)

This function is used as the third argument field delimiter string divided into an array. If the third parameter is not provided, the current default FS value is used.

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

Printf function and the difference equivalent to the difference in the C language printf and a sprintf. The former results formatted output stream, which is output to the return value of the function.

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

Time function:

Systime ()

This function returns the number of seconds difference between the current time from January 1, 1970 of.

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

Formatting function of time, which is equivalent formatting rule in the rule language C strftime function provided, see the list below:

Data Format meaning
%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

Built-in mathematical functions:

name return value
atan2 (x, y) y, x within the scope of the cotangent
cos(x) Cosine function
exp(x) Exponentiation
int(x) ToSei
log(x) Natural logarithm
sin (x) Sine function
sqrt(x) Square root

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

Custom Functions:

Custom Functions can be placed anywhere in the template can be placed and actions awk script.

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

Value to a local variable transfer function. Use only copy of the variable. By address or pointer array transfer, it is possible to directly change the value of the array element within the function. Any variable is not passed as a parameter of the internal functions used are seen as global variables, that is, these variables are visible to the entire program. If the variable has changed in the function, then that changed throughout the program. The sole provider of local variables to function way is to put them in the parameter list, these parameters are usually placed at the end of the list. If the function does not provide a formal argument, the argument is initialized to empty. The return statement usually return program control returns a value to the caller.

 [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
Published 352 original articles · won praise 52 · views 30000 +

Guess you like

Origin blog.csdn.net/xie_qi_chao/article/details/105037996