awk String manipulation functions Substring extraction


Example

GNU awk supports a sub-string extraction function to return a fixed length character sequence from a main string. The syntax is

*substr(string, start [, length ])* 

where, string is source string and start marks the start of the sub-string position you want the extraction to be done for an optional length length characters. If the length is not specified, the extraction is done up to the end of the string.

The first character of the string is treated as character number one.

awk '
BEGIN {
    testString = "MyTESTstring"
    substring  =  substr(testString, 3, 4)    # Start at character 3 for a length of 4 characters
    print substring
}'

will output the sub-string TEST.

awk '
BEGIN {
    testString = "MyTESTstring"
    substring  =  substr(testString, 3)    # Start at character 3 till end of the string
    print substring
}'

this extracts the sub-string from character position 3 to end of the whole string, returning TESTstring

Note:-

  • If start is given a negative value, GNU awk prints the whole string and if length is given a non-zero value GNU awk behavior returns a null string and the behavior varies among different implementations of awk.