logo       

penultimate line script: msg#00074

editors.sed.user

Subject: penultimate line script

On 21 Dec 2005 at 11:01, Angus Leeming wrote:

> Eric Pement wrote:
[ ... ]

> > The following sed script will print
> > the penultimate line (the next to last line) on a file of any length
> > except a file which has only one line, in which case it prints
> > nothing:
> >
> > sed -e '$!{h;d;};x'
>
> How about adding
>
> 1{
> ${
> p
> q
> }
> }
>
> To overcome that limitation?

Well, that would depend on what the user wanted to do with one-line
input; some would want nothing to be printed, others might want the
last line printed anyway. I guess I could put *both* of them in the
one-liners file and let the user decide.

For your recommended script, it would work at the end of the file. A
bit more efficient would be to put it at the head of the script,
where it could be reduced to:

sed '1{$q;};$!{h;d;};x' file

in GNU syntax. I'm not sure how to break it up for other seds.

--
Eric Pement - pemente@xxxxxxxxxxxxx
home page: http://www.pement.org
2 Cor. 4:5




------------------------ Yahoo! Groups Sponsor --------------------~-->
Get Bzzzy! (real tools to help you find a job). Welcome to the Sweet Life.
http://us.click.yahoo.com/KIlPFB/vlQLAA/TtwFAA/dkFolB/TM
--------------------------------------------------------------------~->

--

Yahoo! Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/sed-users/

<*> To unsubscribe from this group, send an email to:
sed-users-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/






<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise