The sub() function replaces the first occurrence of the pattern within the source string.
result = sub (pattern, replacement, sourcestring);
|pattern||Required. The regular expression pattern to search for.|
Required. The replacement string.
|sourcestring||Required. The source string to search for the first occurrence of pattern.|
The resulting string
newstring = sub("\n$", "", textstring)
In this example, the first occurrence of a trailing new line is replaced with nothing, effectively chopping it off.
For more information, please see gsub.