[lug] Stripping whitespace in a shell script

rm at fabula.de rm at fabula.de
Wed Feb 20 10:44:36 MST 2002


On Wed, Feb 20, 2002 at 10:21:39AM -0700, Riggs, Rob wrote:
> The only command that I know where the "[:space:]" construct has any meaning
> is 'tr', and I'm not sure how I'd use 'tr' to strip spaces. Is it usable
> elsewhere?

[:space:] is one of the POSIX character classes (others would be
[:alpha:] or [:digit:] etc.). They are meant to be used in regular
expressions as a locale aware replacement for things like [0-9] or
the dreaded [A-Za-z0-9] to match alphanumeric characters.  If your
copy of  the POSIX standard isn't within reach you can try 'man perlre'
for a quick reference.

   Ralf

> -----Original Message-----
> From: Scott A. Herod [mailto:herod at interact-tv.com]
> Sent: Wednesday, February 20, 2002 10:18 AM
> To: lug at lug.boulder.co.us
> Subject: Re: [lug] Stripping whitespace in a shell script
> 
> 
> List-Archive: <http://lists.lug.boulder.co.us/pipermail/lug/>
> Date: Wed, 20 Feb 2002 10:16:37 -0700
> 
> Won't [:space:] also do it?
> 
> rm at fabula.de wrote:
> > 
> > On Wed, Feb 20, 2002 at 10:04:46AM -0700, Riggs, Rob wrote:
> > > Thanks Chip. But that sed script will only strip spaces. I'm looking to
> > > strip all whitespace, not just " ", but also newlines, tabs, etc.
> > 
> > How about:
> > 
> >   sed -e 's/[\n\t\r ]*$//'
> > 
> > You can insert all sorts of characters into the '[  ... ]' construct,
> > depending on your dfinition of whitespace.
> > 
> >    Ralf
> > 
> > > -----Original Message-----
> > > From: Chip Atkinson [mailto:chip at rmpg.org]
> > > Sent: Wednesday, February 20, 2002 9:55 AM
> > > To: 'lug at lug.boulder.co.us'
> > > Subject: Re: [lug] Stripping whitespace in a shell script
> > >
> > >
> > > You could use sed.  For example,
> > > sed -e "s/  *$//"
> > > This works in the test that I just ran.
> > >
> > > Chip
> > >
> > > On Wed, 20 Feb 2002, Riggs, Rob wrote:
> > >
> > > > I'm looking for an effecient way to strip whitespace from the trailing
> end
> > > > of a line of text in a shell script.
> > > >
> > > > I was using "awk '{print $1}'", but it breaks down when there is
> embedded
> > > > whitespace in the text, spitting out only the portion prior to the
> first
> > > > embedded whitespace. While whitespace should not be embedded, it
> occurs
> > > > frequently enough that I need to deal with it.
> > > >
> > > > Any suggestions on how to do it in Bash script?
> > > >
> > > > -Rob
> _______________________________________________
> Web Page:  http://lug.boulder.co.us
> Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug
> _______________________________________________
> Web Page:  http://lug.boulder.co.us
> Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug



More information about the LUG mailing list