Built-in Functions in AWK

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.

    Examples:

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

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.

    Examples:

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

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.

    Examples:

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

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.

    Examples:


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

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

    Examples:

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

    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.

    Example:

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

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

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

    Example:

    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.

    Example:


    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.

    Example:

    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.

    Example:

    Input: $ awk 'BEGIN{print toupper("geeksforgeeks")}'
    Output: 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.

    Example:

    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
            Nationality
            Is
            Indian
    

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

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.