From owner-freebsd-questions@FreeBSD.ORG Fri May 12 17:02:34 2006 Return-Path: X-Original-To: freebsd-questions@freebsd.org Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 39A5616ADB2 for ; Fri, 12 May 2006 17:02:34 +0000 (UTC) (envelope-from keramida@ceid.upatras.gr) Received: from igloo.linux.gr (igloo.linux.gr [62.1.205.36]) by mx1.FreeBSD.org (Postfix) with ESMTP id 3797743D60 for ; Fri, 12 May 2006 17:02:23 +0000 (GMT) (envelope-from keramida@ceid.upatras.gr) Received: from gothmog.pc (aris.bedc.ondsl.gr [62.103.39.226]) (authenticated bits=128) by igloo.linux.gr (8.13.6/8.13.6/Debian-1) with ESMTP id k4CH1tkc004750 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT); Fri, 12 May 2006 20:02:01 +0300 Received: from gothmog.pc (gothmog [127.0.0.1]) by gothmog.pc (8.13.6/8.13.6) with ESMTP id k4CH467v026772; Fri, 12 May 2006 20:04:06 +0300 (EEST) (envelope-from keramida@ceid.upatras.gr) Received: (from giorgos@localhost) by gothmog.pc (8.13.6/8.13.6/Submit) id k4CH46UP026771; Fri, 12 May 2006 20:04:06 +0300 (EEST) (envelope-from keramida@ceid.upatras.gr) Date: Fri, 12 May 2006 20:04:06 +0300 From: Giorgos Keramidas To: Warren Block Message-ID: <20060512170406.GE26040@gothmog.pc> References: <200605121450.k4CEokhn022089@dc.cis.okstate.edu> <20060512150608.GB25497@gothmog.pc> <20060512102134.J14220@wonkity.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20060512102134.J14220@wonkity.com> X-Hellug-MailScanner: Found to be clean X-Hellug-MailScanner-SpamCheck: not spam, SpamAssassin (score=-3.397, required 5, autolearn=not spam, ALL_TRUSTED -1.80, AWL 0.80, BAYES_00 -2.60, DNS_FROM_RFC_ABUSE 0.20) X-Hellug-MailScanner-From: keramida@ceid.upatras.gr X-Spam-Status: No Cc: Martin McCormick , freebsd-questions@freebsd.org Subject: Re: Trimming Whitespace From Beginning and end of Text Lines X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 12 May 2006 17:02:34 -0000 On 2006-05-12 10:41, Warren Block 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 :)