awk(1) function in awk

Functions in awk
1. String
1.1 index (original string, substring)
If the substring appears at the first position in the original string, if it does not exist, return 0

1.2 length (string) Return the length of the string

1.3 match(original string, regular expression)
awk will batch match the substrings of the expression in the original string. If there are multiple substrings, the leftmost substring of the original string shall prevail.
After awk finds the string, it will perform the following operations according to the string
1) Set the awk built-in variable RSTART RLENGTH
RSTART = the position of the matched substring in the original string, if it is 0, it means that there is no matching substring
RLENGTH = Length of matched substring, -1 means no matched substring
2) Return the value of RSTART
Example :
$ awk 'BEGIN{match("banana",/(an)+/);print RSTART,RLENGTH} '
2 4

1.4 split(original string, array name, separator (can also be regular)) The
original string is divided into fields according to the separator, and an
example of fields separated by array records:
$ awk 'BEGIN{ a ="1P2p3";split(a,arr,/[pP]/);for(i in arr){print arr[i]} }'
1
2
3

1.5 sub (regular expression, new string, original string)
The original string is matched according to the regular expression (the leftmost match), if it matches, it will be replaced with a new string.
If the specified new string is empty, the specified string is removed.
If the original string is not specified, the default is $0
1.5.1 The new string can use & to indicate the matched string.
Example:
$ awk 'BEGIN{a="banan12a.34an6a";sub(/(an)+[0-9]*/,"[&]",a);print a}'
b[anan12]a.34an6a
1.5.2 The combination of sub and match can extract the substring that meets the conditions in the original string
Example :
awk '
BEGIN{
    data = "p12-P34 P56-p61"
    while( match( data ,/[0-9]+/) >0 ){
        print substr(data,RSTART, RLENGTH )
        sub(/[0-9]+/,"",data)
    }
}
' $
result:
12
34
56
61
Description: match verifies that data contains numbers (RSTART>0)
substr performs string interception
sub deletes the leftmost matched number

1.6 The difference between gsub (regular expression, new string, original string)
and sub()
1) will
Replace all substrings that meet

the
conditions to the end of the character.

1.8. sprintf (format, data column, data column, data column, .....)
is the same as awk printf, the difference is that this function will return the printed string

2. The mathematical function
int(x) returns the integer of the value Part int(7.8)=7 int(-7.89)=-7
sqrt(x) square root
of x exp(x) e to the power of x
rand() Random number from 0 to 1. Unless the user specifies the starting seed of the rand() function
, rand() will use the same default seed to generate random numbers each time awk is executed.

srand(x) specifies the starting seed of the rand() function. If x is not specified, awk will use the date and time of execution as the seed of the rand() function. The seed must be specified every time a random number is generated, otherwise the random number will be the same every time. And if seed is specified, the seed will not change unless specified again

Example: $ awk 'BEGIN{srand();x=rand();print x}'














Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326934537&siteId=291194637
awk