[Novalug] Please help with RegEx in GNU Gawk

Jim Ide jimsmaillists at yahoo.com
Sun Sep 25 17:35:40 EDT 2011


Goal: write a TRIM function that deletes leading whitespace
characters from the left end of a string.  See program below.

When I run this, I get:

$ awk -f trim-test.awk
>hello<
>    hello<

$

Why does [[:space:]] not work in trim_v2 as expected?

Thanks for your help.



BEGIN {
	temp = "    hello"
	print ">" ltrim_v1(temp) "<"
	print ">" ltrim_v2(temp) "<"
	exit
}

function ltrim_v1(str) {
	# Delete leading whitespace (spaces and tabs) from the beginning of each line
	sub(/^[ \t]+/, "", str)
	return str
}

function ltrim_v2(str) {
	# Delete leading whitespace (spaces and tabs) from the beginning of each line
	sub(/^[[:space:]]+/, "", str)
	return str
}




More information about the Novalug mailing list