# AWK - String Functions

AWK has the following built-in String functions −

## asort(arr [, d [, how] ])

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.

### Example

```[jerry]\$ awk 'BEGIN {
arr = "Three"
arr = "One"
arr = "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 −

### Output

```Array elements before sorting:
Three
One
Two
Array elements after sorting:
One
Three
Two
```

## asorti(arr [, d [, how] ])

The behavior of this function is the same as that of asort(), except that the array indexes are used for sorting.

### Example

```[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 −

### Output

```Array indices after sorting:
One
Three
Two
```

## gsub(regex, sub, string)

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.

### Example

```[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 −

### Output

```String before replacement = Hello, World
String after replacement = Hello, Jerry
```

## index(str, sub)

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.

### Example

```[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 −

### Output

```Substring "Two" found at 5 location.
```

## length(str)

It returns the length of a string.

### Example

```[jerry]\$ awk 'BEGIN {
str = "Hello, World !!!"
print "Length = ", length(str)
}'
```

On executing this code, you get the following result −

```Length = 16
```

## match(str, regex)

It returns the index of the first longest match of regex in string str. It returns 0 if no match found.

### Example

```[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 −

### Output

```Substring "Two" found at 5 location
```

## split(str, arr, regex)

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.

### Example

```[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 −

### Output

```Array contains following values
One
Two
Three
Four
```

## printf(format, expr-list)

This function returns a string constructed from expr-list according to format.

### Example

```[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 −

### Output

```sqrt(1024.000000) = 32.000000
```

## strtonum(str)

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.

### Example

```[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 −

### Output

```Decimal num = 123
Octal num = 83
```

## sub(regex, sub, string)

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.

### Example

```[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 −

### Output

```String before replacement = Hello, World
String after replacement = Hello, Jerry
```

## substr(str, start, l)

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.

### Example

```[jerry]\$ awk 'BEGIN {
str = "Hello, World !!!"
subs = substr(str, 1, 5)

print "Substring = " subs
}'
```

On executing this code, you get the following result −

### Output

```Substring = Hello
```

## tolower(str)

This function returns a copy of string str with all upper-case characters converted to lower-case.

### Example

```[jerry]\$ awk 'BEGIN {
str = "HELLO, WORLD !!!"
print "Lowercase string = " tolower(str)
}'
```

On executing this code, you get the following result −

### Output

```Lowercase string = hello, world !!!
```

## toupper(str)

This function returns a copy of string str with all lower-case characters converted to upper case.

### Example

```[jerry]\$ awk 'BEGIN {
str = "hello, world !!!"
print "Uppercase string = " toupper(str)
}'
```

On executing this code, you get the following result −

### Output

```Uppercase string = HELLO, WORLD !!!
```
awk_built_in_functions.htm
{"ft_lang_label":"__label__en","ft_lang_prob":0.6940965,"math_prob":0.8802958,"size":4915,"snap":"2020-34-2020-40","text_gpt3_token_len":1244,"char_repetition_ratio":0.14192629,"word_repetition_ratio":0.35831383,"special_character_ratio":0.28016278,"punctuation_ratio":0.12568305,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9932661,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-08-05T22:07:36Z\",\"WARC-Record-ID\":\"<urn:uuid:c1d56914-b1e2-4272-8718-9763bff794f2>\",\"Content-Length\":\"30231\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:f26399b3-74dd-481b-94fc-aae28f3132d0>\",\"WARC-Concurrent-To\":\"<urn:uuid:edaadd42-b3b2-4df5-a586-f1c010d18f27>\",\"WARC-IP-Address\":\"72.21.91.42\",\"WARC-Target-URI\":\"https://www.tutorialspoint.com/awk/awk_string_functions.htm\",\"WARC-Payload-Digest\":\"sha1:F7SFQNZXZ6COUW7T47YRN2FFOXFH2FRR\",\"WARC-Block-Digest\":\"sha1:J5HSMCJLXF5UXTJDSEWZDVHNAQFV7VQT\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-34/CC-MAIN-2020-34_segments_1596439735989.10_warc_CC-MAIN-20200805212258-20200806002258-00310.warc.gz\"}"}