AWK Built-in Functions
Category Programming Techniques
The main types of AWK built-in functions are as follows:
Arithmetic Functions
Function Name | Description | Example |
---|---|---|
atan2( y, x ) | Returns the arctangent of y/x. | $ awk 'BEGIN {<br> PI = 3.14159265<br> x = -10<br> y = 10<br> result = atan2 (y,x) * 180 / PI;<br> printf "The arc tangent for (x=%f, y=%f) is %f degrees\n", x, y, result<br>}' The output is: The arc tangent for (x=-10.000000, y=10.000000) is 135.000000 degrees |
cos( x ) | Returns the cosine of x; x is in radians. | $ awk 'BEGIN {<br> PI = 3.14159265<br> param = 60<br> result = cos(param * PI / 180.0);<br> printf "The cosine of %f degrees is %f.\n", param, result<br>}' The output is: The cosine of 60.000000 degrees is 0.500000. |
sin( x ) | Returns the sine of x; x is in radians. | $ awk 'BEGIN {<br> PI = 3.14159265<br> param = 30.0<br> result = sin(param * PI /180)<br> printf "The sine of %f degrees is %f.\n", param, result<br>}' The output is: The sine of 30.000000 degrees is 0.500000. |
exp( x ) | Returns the exponential value of x. | $ awk 'BEGIN {<br> param = 5<br> result = exp(param);<br> printf "The exponential value of %f is %f.\n", param, result<br>}' The output is: The exponential value of 5.000000 is 148.413159. |
log( x ) | Returns the natural logarithm of x. | $ awk 'BEGIN {<br> param = 5.5<br> result = log (param)<br> printf "log(%f) = %f\n", param, result<br>}' The output is: log(5.500000) = 1.704748 |
sqrt( x ) | Returns the square root of x. | $ awk 'BEGIN {<br> param = 1024.0<br> result = sqrt(param)<br> printf "sqrt(%f) = %f\n", param, result<br>}' The output is: sqrt(1024.000000) = 32.000000 |
int( x ) | Returns the truncated integer value of x. | $ awk 'BEGIN {<br> param = 5.12345<br> result = int(param)<br> print "Truncated value =", result<br>}' The output is: Truncated value = 5 |
rand( ) | Returns a random number n, where 0 <= n < 1. | $ awk 'BEGIN {<br> print "Random num1 =" , rand()<br> print "Random num2 =" , rand()<br> print "Random num3 =" , rand()<br>}' The output is: Random num1 = 0.237788<br>Random num2 = 0.291066<br>Random num3 = 0.845814 |
srand( [Expr] ) | Sets the seed value for the rand function to the value of the Expr argument, or if the Expr argument is omitted, it uses the current time of day. Returns the previous seed value. | $ awk 'BEGIN {<br> param = 10<br> printf "srand() = %d\n", srand()<br> printf "srand(%d) = %d\n", param, srand(param)<br>}' The output is: srand() = 1<br>srand(10) = 1417959587 |
String Functions
Function | Description | Example |
---|---|---|
gsub( Ere, Repl, [ In ] ) |
This is an English translation of the provided Chinese text. Please find the translation below:
| split(String, A, [Ere]) | Splits the parameter specified by String into array elements A[1], A[2], ..., A[n], and returns the value of the n variable. This split can be done using the extended regular expression specified by the Ere parameter, or with the current field separator (FS special variable) if the Ere parameter is not provided. Unless the context indicates that a specific element should also have a numerical value, the elements in the A array are created with string values. | $ awk 'BEGIN {<br> str = "One,Two,Three,Four"<br> split(str, arr, ",")<br> print "Array contains following values"<br> for (i in arr) {<br> print arr[i]<br> }<br>}' The output is: Array contains following values<br>One<br>Two<br>Three<br>Four | | tolower(String) | Returns the string specified by the String parameter, with each uppercase character changed to lowercase. The mapping of uppercase and lowercase is defined by the current locale's LC_CTYPE category. | $ awk 'BEGIN {<br> str = "HELLO, WORLD !!!"<br> print "Lowercase string = " tolower(str)<br>}' The output is: Lowercase string = hello, world !!! | | toupper(String) | Returns the string specified by the String parameter, with each lowercase character changed to uppercase. The mapping of uppercase and lowercase is defined by the current locale's LC_CTYPE category. | $ awk 'BEGIN {<br> str = "hello, world !!!"<br> print "Uppercase string = " toupper(str)<br>}' The output is: Uppercase string = HELLO, WORLD !!! | | sprintf(Format, Expr, Expr, ...) | Formats the expressions specified by the Expr parameters according to the printf subroutine format string specified by the Format parameter and returns the final generated string. | $ awk 'BEGIN {<br> str = sprintf("%s", "Hello, World !!!")<br> print str<br>}' The output is: Hello, World !!! | | strtonum(str) | strtonum converts the string str to a numeric value. If the string starts with 0, it is treated as an octal number; if it starts with 0x or 0X, it is treated as a hexadecimal number; otherwise, it is treated as a floating-point number. | $ awk 'BEGIN {<br> print "Decimal number = " strtonum("123")<br> print "Octal number = " strtonum("0123")<br> print "Hexadecimal number = " strtonum("0x123")<br>}' The output is: Decimal number = 123<br>Octal number = 83<br>Hexadecimal number = 291 |
Note: The Ere
part can be a regular expression.
1. Usage of gsub and sub
$ awk 'BEGIN{info="this is a test2012test!";gsub(/[0-9]+/,"||",info);print info}'
this is a test||test!
2. Finding a string (using index)
The ternary operator is used: expression ? action1 : action2
$ awk 'BEGIN{info="this is a test2012test!";print index(info,"11111")?"ok":"no found";}'
no found
$ awk 'BEGIN{info="this is a test2012test!";print index(info,"is")?"ok":"no found";}'
ok
$ awk 'BEGIN{info="this is a test2012test!";print index(info,"test")?"ok":"no found";}'
ok
3. Regular expression matching (using match)
$ awk 'BEGIN{info="this is a test2012test!";print match(info,/[0-9]+/)?"ok":"no found";}'
ok
4. String slicing (using substr)
Slice a string starting from the 4th character, with a length of 10 characters.
$ awk 'BEGIN{info="this is a test2012test!";print substr(info,4,10);}'
s is a tes
5. String splitting (using split)
``` $ awk 'BEGIN{info="this is a test";split(info,tA," ");print length(tA);for(k in tA){print k,tA[k];}}' 4 2 is 3 a 4 test
This is a Chinese to English translation, please provide the English translation for this text. Do not provide any explanations or text apart from the translation. Chinese: | and | Bitwise AND operation. | $ awk 'BEGIN {<br> num1 = 10<br> num2 = 6<br> printf "(%d AND %d) = %d\n", num1, num2, and(num1, num2)<br>}' The output is: (10 AND 6) = 2 | | compl | Bitwise complement. | $ awk 'BEGIN {<br> num1 = 10<br> printf "compl(%d) = %d\n", num1, compl(num1)<br>}' The output is: compl(10) = 9007199254740981 | | lshift | Left shift operation | $ awk 'BEGIN {<br> num1 = 10<br> printf "lshift(%d) by 1 = %d\n", num1, lshift(num1, 1)<br>}' The output is: lshift(10) by 1 = 20 | | rshift | Right shift operation | $ awk 'BEGIN {<br> num1 = 10<br> printf "rshift(%d) by 1 = %d\n", num1, rshift(num1, 1)<br>}' The output is: rshift(10) by 1 = 5 | | or | Bitwise OR operation | $ awk 'BEGIN {<br> num1 = 10<br> num2 = 6<br> printf "(%d OR %d) = %d\n", num1, num2, or(num1, num2)<br>}' The output is: (10 OR 6) = 14 | | xor | Bitwise XOR operation | $ awk 'BEGIN {<br> num1 = 10<br> num2 = 6<br> printf "(%d XOR %d) = %d\n", num1, num2, xor(num1, num2)<br>}' The output is: (10 bitwise xor 6) = 12 |
Other Functions
Function Name | Description | Example | ||||
---|---|---|---|---|---|---|
close(expr) | Closes a pipe file | $ awk 'BEGIN {<br> cmd = "tr [a-z] [A-Z]"<br> print "hello, world !!!" | & cmd<br> close(cmd, "to")<br> cmd | & getline out<br> print out;<br> close(cmd);<br>}' The output is: HELLO, WORLD !!! The first statement cmd = "tr [a-z] [A-Z]" establishes a two-way communication channel in AWK.<br>The second statement print provides input for the tr command. & | indicates two-way communication. <br>The third statement close(cmd, "to") closes the to process after execution. <br>The fourth statement cmd | & getline out uses the getline function to store the output in the out variable. <br>The following output statement prints the output content, and finally, the close function closes cmd. |
delete | Used to delete elements from an array | $ awk 'BEGIN {<br> arr[0] = "One"<br> arr[1] = "Two"<br> arr[2] = "Three"<br> arr[3] = "Four"<br> print "Array elements before delete operation:"<br> for (i in arr) {<br> print arr[i]<br> }<br> delete arr[0]<br> delete arr[1]<br> print "Array elements after delete operation:"<br> for (i in arr) {<br> print arr[i]<br> }<br>}' The output is: Array elements before delete operation:<br>One<br>Two<br>Three<br>Four<br>Array elements after delete operation:<br>Three<br>Four | ||||
exit | Terminates script execution, it can accept an optional parameter expr to pass AWK return status. | $ awk 'BEGIN {<br> print "Hello, World !!!"<br> exit 10<br> print "AWK never executes this statement."<br>}' The output is: Hello, World !!! | ||||
flush | Flushes the buffer of an open file or pipe | |||||
getline | Reads the next line | Using getline to read a line from the file marks.txt and output: $ awk '{getline; print $0}' marks.txt , AWK reads a line from the file marks.txt and stores it in variable 0. In the next statement, we use getline to read |