| Author |
strip off trailing blanks
|
|
| Lasse Kliemann 2006-09-21, 7:33 am |
| Task: strip trailing blanks off some variable.
How to do it in shell without sed or alike?
The best thing I came up with is:
var='abc ' &&
while :; do
var="${var%% }" &&
case "$var" in
*\ ) continue ;;
*) break ;;
esac
done &&
echo "${var}"x
But this looks very inefficient.
TIA, Lasse
| |
|
| Lasse Kliemann wrote:
> Task: strip trailing blanks off some variable.
>
> How to do it in shell without sed or alike?
>
> The best thing I came up with is:
>
> var='abc ' &&
> while :; do
> var="${var%% }" &&
> case "$var" in
> *\ ) continue ;;
> *) break ;;
> esac
> done &&
> echo "${var}"x
>
> But this looks very inefficient.
>
> TIA, Lasse
See http://www.unix.com/showthread.php?t=29599
--.
| |
| Lasse Kliemann 2006-09-21, 7:33 am |
| Vino <no@replies.com> wrote:
> Lasse Kliemann wrote:
> See http://www.unix.com/showthread.php?t=29599
Thank you for your answer!
The patterns used in this solution, +([ ]), are not portable, however.
They work in /bin/ksh, but not in /bin/sh. Is there any portable
solution better than mine?
| |
| Rakesh Sharma 2006-09-21, 7:33 am |
|
Lasse Kliemann wrote:
> Task: strip trailing blanks off some variable.
>
> How to do it in shell without sed or alike?
>
If there are no leading blanks :
var='asd '
var=`echo $var`
otherwise:
expr "x$var" : 'x\([^ ]*\)'
| |
| Lasse Kliemann 2006-09-21, 7:33 am |
| Rakesh Sharma <sharma__r@hotmail.com> wrote:
>
> Lasse Kliemann wrote:
>
>
> If there are no leading blanks :
>
> var='asd '
> var=`echo $var`
There might be leading blanks, but they have to be removed in my
application as well, so this is all fine. However, this approach fails
if the variable starts with something that is an option to echo, e.g., -n.
> otherwise:
>
> expr "x$var" : 'x\([^ ]*\)'
I didn't know that expr can do that. Interesting! However, I try to get
along without external commands. This seems to be not too easy. Let's
see what else will be suggested.
| |
| Chris F.A. Johnson 2006-09-21, 7:29 pm |
| On 2006-09-21, Lasse Kliemann wrote:
> Task: strip trailing blanks off some variable.
>
> How to do it in shell without sed or alike?
>
> The best thing I came up with is:
>
> var='abc ' &&
> while :; do
> var="${var%% }" &&
> case "$var" in
> *\ ) continue ;;
> *) break ;;
> esac
> done &&
> echo "${var}"x
>
> But this looks very inefficient.
These functions will work in any POSIX shell.
_ltrim() #@ Trim spaces (or char in $2) from left-hand side of $1
{
_LTRIM=$1
trim_string=${_LTRIM%%[!${2:- }]*}
_LTRIM=${_LTRIM#"$trim_string"}
}
ltrim()
{
_ltrim "$@"
printf "%s\n" "$_LTRIM"
}
_rtrim() #@ Trim spaces (or char in $2) from right-hand side of $1
{
_RTRIM=$1
trim_string=${_RTRIM##*[!${2:- }]}
_RTRIM=${_RTRIM%"$trim_string"}
}
rtrim()
{
_rtrim "$@"
printf "%s\n" "$_RTRIM"
}
_trim() #@ Trim spaces (or char in $2) from both ends of $1
{
_ltrim "$@"
shift
_rtrim "$_LTRIM" "$@"
_TRIM=$_RTRIM
}
_trim() #@ Trim spaces (or char in $2) from both ends of $1 (faster)
{
_TRIM=$1
trim_string=${_TRIM%%[!${2:- }]*}
_TRIM=${_TRIM#"$trim_string"}
trim_string=${_TRIM##*[!${2:- }]}
_TRIM=${_TRIM%"$trim_string"}
}
trim()
{
_trim "$@"
printf "%s\n" "$_TRIM"
}
--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
| |
|
|
Lasse Kliemann wrote:
> Task: strip trailing blanks off some variable.
>
> How to do it in shell without sed or alike?
>
> The best thing I came up with is:
>
> var='abc ' &&
> while :; do
> var="${var%% }" &&
> case "$var" in
> *\ ) continue ;;
> *) break ;;
> esac
> done &&
> echo "${var}"x
>
> But this looks very inefficient.
>
> TIA, Lasse
if you have Python:
[vbcol=seagreen]
'somestring'
|
|
|
|