AWK - String Functions

AWK has the following built-in String functions −
This function sorts the contents of arr using GAWK's normal rules for comparing values, and replaces the indexes of the sorted values arr with sequential integers starting with 1.
[jerry]$ awk 'BEGIN {
   arr[0] = "Three"
   arr[1] = "One"
   arr[2] = "Two"
   print "Array elements before sorting:"
   
   for (i in arr) {
      print arr[i]
   }
   asort(arr)
   print "Array elements after sorting:"
   
   for (i in arr) {
      print arr[i]
   }
}'
On executing this code, you get the following result −
Array elements before sorting:
Three
One
Two
Array elements after sorting:
One
Three
Two

The behavior of this function is the same as that of asort(), except that the array indexes are used for sorting.
[jerry]$ awk 'BEGIN {
   arr["Two"] = 1
   arr["One"] = 2
   arr["Three"] = 3
   asorti(arr)
   print "Array indices after sorting:"
   
   for (i in arr) {
      print arr[i]
   }
}'
On executing this code, you get the following result −
Array indices after sorting:
One
Three
Two

gsub stands for global substitution. It replaces every occurrence of regex with the given string (sub). The third parameter is optional. If it is omitted, then $0 is used.
[jerry]$ awk 'BEGIN {
   str = "Hello, World"
   print "String before replacement = " str
   
   gsub("World", "Jerry", str)
   print "String after replacement = " str
}'
On executing this code, you get the following result −
String before replacement = Hello, World
String after replacement = Hello, Jerry

It checks whether sub is a substring of str or not. On success, it returns the position where sub starts; otherwise it returns 0. The first character of str is at position 1.
[jerry]$ awk 'BEGIN {
   str = "One Two Three"
   subs = "Two"
   ret = index(str, subs)
   
   printf "Substring \"%s\" found at %d location.\n", subs, ret
}'
On executing this code, you get the following result −
Substring "Two" found at 5 location.

It returns the length of a string.
[jerry]$ awk 'BEGIN {
   str = "Hello, World !!!"
   print "Length = ", length(str)
}'
On executing this code, you get the following result −
Length = 16

It returns the index of the first longest match of regex in string str. It returns 0 if no match found.
[jerry]$ awk 'BEGIN {
   str = "One Two Three"
   subs = "Two"
   ret = match(str, subs)
  
   printf "Substring \"%s\" found at %d location.\n", subs, ret
}'
On executing this code, you get the following result −
Substring "Two" found at 5 location

This function splits the string str into fields by regular expression regex and the fields are loaded into the array arr. If regex is omitted, then FS is used.
[jerry]$ awk 'BEGIN {
   str = "One,Two,Three,Four"
   split(str, arr, ",")
   print "Array contains following values"
   
   for (i in arr) {
      print arr[i]
   }
}'
On executing this code, you get the following result −
Array contains following values
One
Two
Three
Four

This function returns a string constructed from expr-list according to format.
[jerry]$ awk 'BEGIN {
   param = 1024.0
   result = sqrt(param)
   
   printf "sqrt(%f) = %f\n", param, result
}'
On executing this code, you get the following result −
sqrt(1024.000000) = 32.000000

This function examines str and return its numeric value. If str begins with a leading 0, it is treated as an octal number. If str begins with a leading 0x or 0X, it is taken as a hexadecimal number. Otherwise, assume it is a decimal number.
[jerry]$ awk 'BEGIN {
   print "Decimal num = " strtonum("123")
   print "Octal num = " strtonum("0123")
   print "Hexadecimal num = " strtonum("0x123")
}'
On executing this code, you get the following result −
Decimal num = 123
Octal num = 83
Hexadecimal num = 291

This function performs a single substitution. It replaces the first occurrence of the regex pattern  with the given string (sub). The third parameter is optional. If it is omitted, $0 is used.
[jerry]$ awk 'BEGIN {
   str = "Hello, World"
   print "String before replacement = " str
   
   sub("World", "Jerry", str)
   print "String after replacement = " str
}'
On executing this code, you get the following result −
String before replacement = Hello, World
String after replacement = Hello, Jerry

This function returns the substring of string str, starting at index start of length l. If length is omitted, the suffix of str starting at index start is returned.
[jerry]$ awk 'BEGIN {
   str = "Hello, World !!!"
   subs = substr(str, 1, 5)

   print "Substring = " subs
}'
On executing this code, you get the following result −
Substring = Hello

This function returns a copy of string str with all upper-case characters converted to lower-case.
[jerry]$ awk 'BEGIN {
   str = "HELLO, WORLD !!!"
   print "Lowercase string = " tolower(str)
}'
On executing this code, you get the following result −
Lowercase string = hello, world !!!

This function returns a copy of string str with all lower-case characters converted to upper case.
[jerry]$ awk 'BEGIN {
   str = "hello, world !!!"
   print "Uppercase string = " toupper(str)
}'
On executing this code, you get the following result −
Uppercase string = HELLO, WORLD !!!

 Print
 Add Notes
 Bookmark this page