From owner-freebsd-questions@FreeBSD.ORG Fri May 12 16:51:31 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 A6B3D16AB74 for ; Fri, 12 May 2006 16:51:31 +0000 (UTC) (envelope-from kyrreny@broadpark.no) Received: from osl1smout1.broadpark.no (osl1smout1.broadpark.no [80.202.4.58]) by mx1.FreeBSD.org (Postfix) with ESMTP id 32F6F43D6D for ; Fri, 12 May 2006 16:51:31 +0000 (GMT) (envelope-from kyrreny@broadpark.no) Received: from osl1sminn1.broadpark.no ([80.202.4.59]) by osl1smout1.broadpark.no (Sun Java System Messaging Server 6.1 HotFix 0.05 (built Oct 21 2004)) with ESMTP id <0IZ500AXXW5UG690@osl1smout1.broadpark.no> for freebsd-questions@freebsd.org; Fri, 12 May 2006 18:51:30 +0200 (CEST) Received: from urban.broadpark.no ([80.203.212.30]) by osl1sminn1.broadpark.no (Sun Java System Messaging Server 6.1 HotFix 0.05 (built Oct 21 2004)) with ESMTP id <0IZ500CHWW5TGH90@osl1sminn1.broadpark.no> for freebsd-questions@freebsd.org; Fri, 12 May 2006 18:51:30 +0200 (CEST) Date: Fri, 12 May 2006 18:51:30 +0200 From: Kyrre Nygard In-reply-to: <200605121450.k4CEokhn022089@dc.cis.okstate.edu> To: Martin McCormick , freebsd-questions@freebsd.org Message-id: <7.0.1.0.2.20060512184738.022275a8@broadpark.no> MIME-version: 1.0 X-Mailer: QUALCOMM Windows Eudora Version 7.0.1.0 Content-type: text/plain; charset=us-ascii; format=flowed Content-transfer-encoding: 7BIT References: <200605121450.k4CEokhn022089@dc.cis.okstate.edu> Cc: 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:51:32 -0000 At 16:50 12.05.2006, 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 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' > > If I cat the test file through this script, it also >removes one or two spaces, but not all the leading and trailing >whitespace I put there. I can write a program in C to do this, >but is there a sed script or other native application in FreeBSD that >can do this? > > Thank you. > >Martin McCormick WB5AGZ Stillwater, OK >Systems Engineer >OSU Information Technology Department Network Operations Group What's up man? Here's a script I use to remove trailing whitespace. It also reduces two or more empty lines like this: -- -- To just one: -- -- And it converts ASCII files to UNIX format (that is without ^M). Then for pretty sake, it adds an empty line to the end of each file. -- #!/usr/local/bin/bash # # Remove CRLF, trailing whitespace and double lining. # $MERHABA: ascii_clean.sh,v 1.0 2007/11/11 15:09:05 kyrre Exp $ # for file in `find -s . -type f`; do if file -b $file | grep -q 'text'; then echo >> $file tr -d '\r' < $file | cat -s | sed -E -e 's/[[:space:]]+$//' > $file.tmp mv -f $file.tmp $file echo "$file: Done" fi done -- I'd be interested in knowing if you manage to improve this script. Take care, Kyrre