Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 25 Sep 2007 10:08:51 -0700
From:      Gary Kline <kline@tao.thought.org>
To:        Giorgos Keramidas <keramida@ceid.upatras.gr>
Cc:        Howard Goldstein <hg@queue.to>, Gary Kline <kline@tao.thought.org>, freebsd-questions@freebsd.org
Subject:   Re: sed question...
Message-ID:  <20070925170851.GE50519@thought.org>
In-Reply-To: <20070925162424.GA13463@kobe.laptop>
References:  <20070925013723.GA50027@thought.org> <46F87B68.6090607@queue.to> <20070925035217.GC50519@thought.org> <20070925162424.GA13463@kobe.laptop>

next in thread | previous in thread | raw e-mail | index | archive | help
On Tue, Sep 25, 2007 at 07:24:25PM +0300, Giorgos Keramidas wrote:
> On 2007-09-24 20:52, Gary Kline <kline@tao.thought.org> wrote:
> >On Mon, Sep 24, 2007 at 11:07:20PM -0400, Howard Goldstein wrote:
> >>>  # 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?
> >> 
> >> Wouldn't it be easier to use  head -n 18 ?
> 
> If you _know_ that the file has 28 lines, yes.  If you don't,
> then itmay be tricky to 'guess' that -n 18 is the right option.
> 
> > No, because most of these files are between 40 and 50 lines.  I only
> > care about the first 30 or 40; everything below has to be deleted.  By
> > hand, using vi, I might type :31,$d that fixes that one file.  Of
> > course, I could simply edit in "19" for "10" above.  It would be more
> > savvy to understand the sed syntax.
> 
> You don't need to manually edit files with vi(1) if all you want to do
> is type ``:31,$d<RET>:wq<RET>'' ...
> 
> 	sed -i '' -e '31,$d' file.txt


	The catch is that I don't always know the linecount; the only
	thing I have found--by examing ALL hundreds of files (briefly:)
	--is that I was to delete the last 19 lines.  

	If sed can understand negative indexing, then would -e '-19,$d'
	work?  That would makr sense from a human standpoint; not sure
	how that would fit the sed model, tho.

	gary


> 

-- 
  Gary Kline  kline@thought.org   www.thought.org  Public Service Unix
      http://jottings.thought.org   http://transfinite.thought.org




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20070925170851.GE50519>