From owner-freebsd-questions@FreeBSD.ORG Fri May 12 16:41:54 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 8D51D16A444 for ; Fri, 12 May 2006 16:41:54 +0000 (UTC) (envelope-from wblock@wonkity.com) Received: from wonkity.com (wonkity.com [67.158.26.137]) by mx1.FreeBSD.org (Postfix) with ESMTP id D073843D72 for ; Fri, 12 May 2006 16:41:53 +0000 (GMT) (envelope-from wblock@wonkity.com) Received: from wonkity.com (localhost [127.0.0.1]) by wonkity.com (8.13.4/8.13.3) with ESMTP id k4CGfYbO014678; Fri, 12 May 2006 10:41:34 -0600 (MDT) (envelope-from wblock@wonkity.com) Received: from localhost (wblock@localhost) by wonkity.com (8.13.4/8.13.3/Submit) with ESMTP id k4CGfYvU014675; Fri, 12 May 2006 10:41:34 -0600 (MDT) (envelope-from wblock@wonkity.com) Date: Fri, 12 May 2006 10:41:34 -0600 (MDT) From: Warren Block To: Giorgos Keramidas In-Reply-To: <20060512150608.GB25497@gothmog.pc> Message-ID: <20060512102134.J14220@wonkity.com> References: <200605121450.k4CEokhn022089@dc.cis.okstate.edu> <20060512150608.GB25497@gothmog.pc> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-2.0.2 (wonkity.com [127.0.0.1]); Fri, 12 May 2006 10:41:34 -0600 (MDT) 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 16:41:55 -0000 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 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 -Warren Block * Rapid City, South Dakota USA