From owner-freebsd-questions@FreeBSD.ORG Tue Sep 25 16:58:57 2007 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 47DC016A418 for ; Tue, 25 Sep 2007 16:58:57 +0000 (UTC) (envelope-from kline@tao.thought.org) Received: from tao.thought.org (dsl231-043-140.sea1.dsl.speakeasy.net [216.231.43.140]) by mx1.freebsd.org (Postfix) with ESMTP id 7890A13C44B for ; Tue, 25 Sep 2007 16:58:56 +0000 (UTC) (envelope-from kline@tao.thought.org) Received: from tao.thought.org (localhost [127.0.0.1]) by tao.thought.org (8.13.8/8.13.1) with ESMTP id l8PGwrOM057023; Tue, 25 Sep 2007 09:58:54 -0700 (PDT) (envelope-from kline@tao.thought.org) Received: (from kline@localhost) by tao.thought.org (8.13.8/8.13.1/Submit) id l8PGwjFc057018; Tue, 25 Sep 2007 09:58:45 -0700 (PDT) (envelope-from kline) Date: Tue, 25 Sep 2007 09:58:44 -0700 From: Gary Kline To: Nikos Vassiliadis Message-ID: <20070925165844.GD50519@thought.org> References: <20070925013723.GA50027@thought.org> <46F87B68.6090607@queue.to> <200709250931.05367.nvass@teledomenet.gr> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <200709250931.05367.nvass@teledomenet.gr> User-Agent: Mutt/1.4.2.2i X-Organization: Thought Unlimited. Public service Unix since 1986. X-Of_Interest: With 21 of service to the Unix community. Cc: Howard Goldstein , Gary Kline , freebsd-questions@freebsd.org Subject: Re: sed question... 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: Tue, 25 Sep 2007 16:58:57 -0000 On Tue, Sep 25, 2007 at 09:31:04AM +0300, Nikos Vassiliadis wrote: > On Tuesday 25 September 2007 06:07, Howard Goldstein wrote: > > Gary Kline wrote: > > > My earlier post about deleting the first N lines was answered by > > > this one-liner site {below}. I wasn't including any > > > redirection; doing so finally resolved the problem. Now I need > > > to delete every line from the 19th or so to the last line. > > sed -e 18q > that is, quit after processing line 18. This quits after line 18, as you say. Given a file of 100 lines, I was everything from line 81,100d. Which is what "#method 1" does. But trying to parse this from man sed is more than difficule. And I have yet to find "ba" in the man page. That is why I asked for some insights rather that to be told to "go read the man page"; to me, that's dismissing the issue rather than addressing it. > > > > Question one, can anybody explain the following syntax? What do > > > "P", "D" "ba" represent, in other words? > > The manual page explains sed in a very good way. For sure, better > than I could describe it here. You'd better read it. > > > > > > > > > > # delete the last 10 lines of a file > > > sed -e :a -e '$d;N;2,10ba' -e 'P;D' # method 1 > > > sed -n -e :a -e '1,10!{P;N;D;};N;ba' # method 2 > > > > > > > > > Question two, can sed do its thing inline? > > Yes. > -i extension > Edit files in-place, saving backups with the specified extension. > If a zero-length extension is given, no backup will be saved. It > is not recommended to give a zero-length extension when in-place > editing files, as you risk corruption or partial content in situ- > ations where disk space is exhausted, etc. > Right. I always do a perl -pi.bak [...] mostly out of habit. With sed, redirection saved the new output, leaving the original in ``.'' FWIW, I was using the sed on my Ubuntu server. It is different from the BSD sed that I've used now/then since 1978. The linux sed man page is just slightly more readable that the BSD. Probably newer. gary > > > > Wouldn't it be easier to use head -n 18 ? > > No, it's the same. Some sed operation are trivial to read/write, > others aren't. > > HTH > > Nikos -- Gary Kline kline@thought.org www.thought.org Public Service Unix http://jottings.thought.org http://transfinite.thought.org