Trimming Whitespace From Beginning and end of Text Lines

Giorgos Keramidas keramida at ceid.upatras.gr
Fri May 12 10:02:34 PDT 2006


On 2006-05-12 10:41, Warren Block <wblock at wonkity.com> wrote:
>On Fri, 12 May 2006, Giorgos Keramidas wrote:
>> 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.
>
> Neither of those work here:
>
> The first sed expression is missing "//".  Correcting that:
>   sed -i -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' test.txt
>   sed: lstat: No such file or directory

Yeah, I noticed the missing // in the first regexp, but only
after I had posted the message.  You're right, of course :)

It seems odd that the fixed expression doesn't work though.
Which version of FreeBSD is this and what sed are you running?

	$ uname -v
	$ type sed

> The Perl version shows no difference between the original and
> processed file.  It's complex, too.  This one works:
>
>   perl -pi -le 's/^\s+//; s/\s+$//' test.txt
>
> Notes:
> 1. sed always seems to be a pain.  My compliments to those who use it
>    regularly; the only time I use it at all is when Perl (or something
>    else with better handling of regular expressions) is not available.
> 2. The -l option to perl is needed to preserve line endings.
> 3. The last version is based on the more efficient way of doing it as
>    per: man -P 'less +/trim' perlop

Great!  Thanks for all the useful tips :)



More information about the freebsd-questions mailing list