Site icon LinuxCommands.site

Awk built-in functions: split, tolower, toupper, sprintf

Today we will share four commonly used awk built-in string functions:

Syntax

awk 'pattern {  action }'

Example

In the following example, we use the awk split function to split the string str into an array according to the fixed delimiter “,”, and print the third item of the array.

awk 'BEGIN { 
    str = "a b c d";
    split(str, arr, " ");
    print arr[3];
}'

In the following example, we use the awk tolower function to convert all strings to lowercase.

awk 'BEGIN { 
    str = "Linux Command Site!";
    newStr = tolower(str);
    print newStr;
}'

In the following example, we use the awk toupper function to convert all strings to uppercase.

awk 'BEGIN { 
    str = "Linux Command Site!";
    newStr = toupper(str);
    print newStr;
}'

In the following example, we use the awk sprintf function to convert the string according to the format.

awk 'BEGIN { 
    str = "%s Command Site!";
    newStr = sprintf(str, "Linux");
    print newStr;
}'

Exit mobile version