Date: Fri, 12 May 2006 18:06:08 +0300 From: Giorgos Keramidas <keramida@ceid.upatras.gr> To: Martin McCormick <martin@dc.cis.okstate.edu> Cc: freebsd-questions@freebsd.org Subject: Re: Trimming Whitespace From Beginning and end of Text Lines Message-ID: <20060512150608.GB25497@gothmog.pc> In-Reply-To: <200605121450.k4CEokhn022089@dc.cis.okstate.edu> References: <200605121450.k4CEokhn022089@dc.cis.okstate.edu>
next in thread | previous in thread | raw e-mail | index | archive | help
On 2006-05-12 09:50, Martin McCormick <martin@dc.cis.okstate.edu> wrote: > This looks like something sed should be able to do, but I > haven't had any luck at all. I wanted to remove any whitespace > that has accidentally gotten added to the beginning or end of > some lines of text. I made a test file that looks like: > > left justified. > lots of spaces. > > and the best I have done so far is to get rid of about 3 spaces. > > Attempt 1. > > #! /usr/bin/sed -f > s/ \+//g > s/^ //g > s/ $//g This fails to remove TAB characters from either the start or the end of a line. > This looks like it should do the job, but the leading and > trailing spaces are still mostly there. > > I wrote another script. Attempt 2. > > #! /bin/sh > > sed 's/^[[:space:]]//g' \ > |sed 's/[[:space:]]$//g' This fails to remove multiple occurences of the [[:space:]] class. There are at least the following ways: sed -i -e 's/^[[:space:]]*' -e 's/[[:space:]]*$//' file ... perl -pi -e 's/^\s*(\S.*\S)[ \t]*$/$1/' file ... The first one seems more straightforward to me most of the time, but there are times I find Perl's `-pi -e ...' idiom very convenient.
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20060512150608.GB25497>