From owner-freebsd-questions@FreeBSD.ORG Fri May 12 15:04:29 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 B997D16A437 for ; Fri, 12 May 2006 15:04:29 +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 15B8343D45 for ; Fri, 12 May 2006 15:04:26 +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 k4CF3vHQ029267 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT); Fri, 12 May 2006 18:03:59 +0300 Received: from gothmog.pc (gothmog [127.0.0.1]) by gothmog.pc (8.13.6/8.13.6) with ESMTP id k4CF68Vt025747; Fri, 12 May 2006 18:06:08 +0300 (EEST) (envelope-from keramida@ceid.upatras.gr) Received: (from giorgos@localhost) by gothmog.pc (8.13.6/8.13.6/Submit) id k4CF687N025746; Fri, 12 May 2006 18:06:08 +0300 (EEST) (envelope-from keramida@ceid.upatras.gr) Date: Fri, 12 May 2006 18:06:08 +0300 From: Giorgos Keramidas To: Martin McCormick Message-ID: <20060512150608.GB25497@gothmog.pc> References: <200605121450.k4CEokhn022089@dc.cis.okstate.edu> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <200605121450.k4CEokhn022089@dc.cis.okstate.edu> X-Hellug-MailScanner: Found to be clean X-Hellug-MailScanner-SpamCheck: not spam, SpamAssassin (score=-3.396, 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: 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 15:04:29 -0000 On 2006-05-12 09:50, Martin McCormick 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.