This is part of The Pile, a partial archive of some open source mailing lists and newsgroups.
Subject: Re: Trimming Space From Variable From: Bruce Richardson <brichardson@lineone.net> Date: Wed, 5 May 1999 20:57:41 +0100 (GMT) On Wed, 5 May 1999, SoloCDM wrote: > How do I trim the spaces from an object going into a variable in a > script? > You can do this in one line using sed and echo. In bash, echo removes all leading spaces and all but one trailing space, so VARIABLE=`echo " your string goes here " | sed 's/ $//'` will do the trick. For reference, sed 's/ *//' removes all leading spaces from a string, so in a situation where you can't/won't use echo then the same can be done by piping the string through both the above sed commands. ====== Subject: Re: Trimming Space From Variable From: "Enrico Payne" <enricop@pharma.co.za> Date: Wed, 5 May 1999 12:41:59 +0200 Try this for size.... ---------------------------------------------------------------------------- D1="How Do you Do ." echo $D1 D1=`echo $D1| tr " " ,` #Convert spaces to , D1=`echo $D1| tr "\012" ,` #Convert L/F to , while [ `echo $D1|grep ","` ] do D2=$D2`echo $D1|cut -f1 -d","` #Concatenate the first Field D1=`echo $D1|cut -f2-250 -d","` #Remove the first Field done echo $D2 ---------------------------------------------------------------------------- I find converting spaces to some other character that will not be in the text, such as commas, will prevent any anomalies from creeping in, this also includes line feeds and carriage returns ========= Subject: Re: Trimming Space From Variable From: Paul Mezzanini <paul.mezzanini@saintleo.edu> Date: Wed, 05 May 1999 09:02:02 -0400 Where are the spaces? In the middle of the string or on the end(s)? I ran into an interesting problem where I had a bunch of white space on the beginning of a string. I just reset the variable to itself without quoting it and it took care of itself. Looked like this; ActualBudget=$ActualBudget at first it was breaking lots of stuff on me, but then I got it figured out and used that 'break' as a feature :) === Subject: Re: Trimming Space From Variable URGENT CORRECTION From: Bruce Richardson <brichardson@lineone.net> Date: Wed, 5 May 1999 21:29:02 +0100 (GMT) On Wed, 5 May 1999, Bruce Richardson wrote: > > > On Wed, 5 May 1999, SoloCDM wrote: > > > How do I trim the spaces from an object going into a variable in a > > script? > > > > > You can do this in one line using sed and echo. In bash, echo removes all > leading spaces and all but one trailing space, so > > VARIABLE=`echo " your string goes here " | sed 's/ $//'` > > will do the trick. > > For reference, > > sed 's/ *//' that should be sed 's/^ *//' > > removes all leading spaces from a string, so in a situation where you > can't/won't use echo then the same can be done by piping the string > through both the above sed commands. actually no, it should be piped through the amended sed command immediately above and then through sed 's/ *$//' Apologies for any confusion caused. ===