Main BLOGGER
Saturday, April 21, 2007
all trim of a string in bash
str=" \t \n1234 dd 56 \t\n "
trim_str=`expr match "$str" '[ \t\n]*\(.*[^ \t\n]\)[ \t\n]*$'`
echo "'$trim_str'" #’1234 dd 56’
[ \t\n]* for leading white spaces
\(.*[^ \t\n]\) for substring in between with non-whitespace ending
[ \t\n]*$ for trailing white spaces
expr match "$string" '\($substring\)'
Extracts $substring at beginning of $string, where $substring is a regular expression.
expr "$string" : '\($substring\)'
Extracts $substring at beginning of $string, where $substring is a regular expression.