Built-in Functions in AWK - GeeksforGeeks

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.
Examples:Input : $ awk 'BEGIN{print int(3.534);print int(4);print int(-5.223);print int(-5);}'
Output : 3
         4
         -5
         -5

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

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

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

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

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

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


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


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


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


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

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.
linux-command
Linux-Unix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
nohup Command in Linux with Examples
scp command in Linux with Examples
Thread functions in C/C++
mv command in Linux with examples
chown command in Linux with Examples
SED command in Linux | Set 2
Docker - COPY Instruction
Array Basics in Shell Scripting | Set 1
Basic Operators in Shell Scripting
nslookup command in Linux with Examples