Using built-in functions for string manipulation, math, and more in AWK

In AWK, there are many built-in functions that can be used for string manipulation, math, and more. Here are some commonly used functions:

– **String manipulation functions:**

– `length(str)`: Returns the length of a string.
– `substr(str, start, length)`: Returns a substring of a string starting from `start` and with a length of `length`.
– `index(str, substr)`: Returns the position of the first occurrence of `substr` in `str`.
– `tolower(str)`: Converts a string to lowercase.
– `toupper(str)`: Converts a string to uppercase.
– `split(str, arr, sep)`: Splits a string into an array `arr` using a separator `sep`. Returns the number of elements in the array.

– **Math functions:**

– `sqrt(x)`: Returns the square root of `x`.
– `exp(x)`: Returns the exponential of `x`.
– `log(x)`: Returns the natural logarithm of `x`.
– `sin(x)`, `cos(x)`, `tan(x)`: Returns the sine, cosine, and tangent of `x`, respectively.
– `rand()`: Returns a random number between 0 and 1.

– **Formatting functions:**

– `sprintf(format, value1, value2, …)`: Returns a formatted string according to the specified `format` string and values. This function works like the `printf()` function in C, but instead of printing the formatted string to the standard output, it returns the string.

– **Array functions:**

– `delete arr[index]`: Deletes the element at `index` from the array `arr`.
– `length(arr)`: Returns the number of elements in the array `arr`.
– `in arr[index]`: Returns true if the element at `index` exists in the array `arr`, false otherwise.

Here are some examples of using built-in functions in AWK:

– Using `length` to count characters in a string:

`
  str = "hello world"
  len = length(str)
  print "Length of string:", len
  

This program uses the `length` function to count the number of characters in the string “hello world”, and prints the result.

– Using `substr` to extract a substring:

`
  str = "hello world"
  substr = substr(str, 7, 5)
  print "Substring:", substr
  

This program uses the `substr` function to extract a substring of length 5 starting from position 7 in the string “hello world”, and prints the result.

– Using `split` to split a string into an array:

`
  str = "apple,banana,orange"
  split(str, arr, ",")
  for (i in arr) {
    print "Array element", i, ":", arr[i]
  }
  

This program uses the `split` function to split the string “apple,banana,orange” into an array `arr` using the separator “,”. It then loops over the elements of the array and prints each element.

– Using `sqrt` to calculate the square root of a number:

`
  x = 25
  sqrt_x = sqrt(x)
  print "Square root of", x, "is", sqrt_x
  

This program uses the `sqrt` function to calculate the square root of the number 25, and prints the result.

– Using `sprintf` to format a string:

`
  x = 10
  y = 3.14159
  str = sprintf("x = %d, y = %.2f", x, y)
  print str
  

This program uses the `sprintf` function to format a string with the values of the variables `x` and `y`, and stores the result in the variable `str`. It then prints the formatted string.

These are just a few examples of the many built-in functions available in AWK. Refer to the AWK documentation for a complete list of functions and their usage.