Trimming Whitespace From Beginning and end of Text Lines

Chuck Swiger cswiger at mac.com
Fri May 12 08:27:14 PDT 2006


Giorgos Keramidas wrote:
> 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.
>   
It is, and I wish to acknowledge the above are entirely valid solutions 
to the problem, but...

   python -c 'import sys; print sys.stdin.read().strip()' < file...

...has the advantage of being human readable.  My old 300-baud accoustic 
modem used to generate output which in hindsight looks astonishingly 
close to regex character classes.  :-)

-- 
-Chuck



More information about the freebsd-questions mailing list