Skip to content
Related Articles

Related Articles

Improve Article

Built-in Functions in AWK

  • Last Updated : 23 Feb, 2018

AWK has lots of built-in functions for numeric, string, input, and output operations. Awk has the following two types of high level built-in function categories:

  • Built-in functions for numeric operations.
  • Built-in functions for string operations.

Prerequisite – AWK command in Unix/Linux

Built-in functions for Numeric operations

1. Awk int(n) function: int() function gives us the integer part of the given argument. This produces the lowest integer part of given n. n is any number with or without floating point. If you the whole number as an argument, this function returns the same.


    Input : $ awk 'BEGIN{print int(3.534);print int(4);print int(-5.223);print int(-5);}'
    Output : 3

2. awk log(n) function: log() function provides natural logarithmic(with base e) of given amount n. log() returns logarithmic value only when n is positive number. If we give any invalid number(even negative), it throws an error.

    Input : $ awk 'BEGIN{print log(3.534);print log(4);print log(0);print log(-5);print log(-1);}'
    Output : 1.26243
             awk: cmd. line:1: warning: log: received negative argument -5
             awk: cmd. line:1: warning: log: received negative argument -1

Explanation: Returns -inf when given zero and gives nan error when negative number is given.

3. awk sqrt(n) function: sqrt() function gives the positive root for the given integer n. This function also accepts the positive number.

    Input : $ awk 'BEGIN{print sqrt(16);print sqrt(0);print sqrt(-12);}'
    Output : 4
             awk: cmd. line:1: warning: sqrt: called with negative argument -12

Explanation: It returns nan error if we give negative number as argument.

4. awk sin(n) function: sin() function gives sine value of n, with n in radians.


    Input : $ awk 'BEGIN{print sin(-60);print sin(90);print sin(45);}'
    Output :0.304811

5. awk cos(n) function: cos() function gives cosine value of n, with n in radians.

    Input : $ awk 'BEGIN{print cos(-60);print cos(90);print cos(45);}'
    Output :-0.952413

    Built-in Functions for String Operations

1. awk index(str1, str2) Function: This searches the string str1 for the first occurrences of the string str2, and returns the position in characters where that occurrence begins in the string str1. String indices in awk starts from 1.

    Input: awk 'BEGIN{print index("Graphic", "ph"); print index("University", "abc")}'
    Output: 4

Explanation: Returns 0 if str2 is not found in str1.

2. awk length(string) Function: The length() function calculates the length of a string.

    Input: $ awk 'BEGIN{print length("Graphic Era University")}'
    Output: 22

Explanation: Length of the string also includes spaces.

3. awk substr(s, p, n) Function: The length() function is used to extract substring function from a string. Returns substring of string s at beginning position p up to a maximum length of n. If n is not supplied, the rest of the string from p is used.


    Input: $ awk 'BEGIN{print substr("Graphic Era University", 9)}'
    Output: Era University
    Input: $ awk 'BEGIN{print substr("Graphic Era University", 9, 8)}'
    Output: Era Univ

4. awk tolower(s) Function: Translate all uppercase characters in string s to lowercase and returns the new string.

    Input: $ awk 'BEGIN{print tolower("GEEKSFORGEEKS")}'
    Output: geeksforgeeks

5. awk toupper(s) Function: Translate all lowercase characters in string s to uppercase and returns the new string.

    Input: $ awk 'BEGIN{print toupper("geeksforgeeks")}'

6. awk split(string, array, fieldsep) Function: This divides string into pieces separated by fieldsep, and stores the pieces in array. The first piece is stored in array[1], the second piece in array[2], and so forth. The string value of the third argument, fieldsep, describe where to split string.

    Input: $ awk 'BEGIN{string="My Nationality Is Indian"; fieldsep=" "; n=split(string, array, fieldsep); for(i=1; i<=n; i++){printf("%s\n", array[i]);}}'
    Output: My

Explanation: The above script breaks up the sentence into words, using a space as the character separating the words.

My Personal Notes arrow_drop_up
Recommended Articles
Page :