Sorting all lines matching a pattern

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Sorting all lines matching a pattern

Stephen R Laniel
Suppose that interspersed throughout my document, I have a
bunch of lines that look like

* Abbey, Edward. "The Monkey Wrench Gang"[book]
* Adams, Henry. "The Education of Henry Adams"[book]
* Atwood, Margaret. "The Handmaid's Tale"[book]
* Aurelius, Marcus, "Somethingorother"[book]

I'd like to sort all these lines together. I thought that

:g/^\*/!sort

would group all of these lines together and sort them
relative to one another. But it's not so -- I think the 'g'
command is treating them one by one, yes?

Is there any way to do what I want?

--
Stephen R. Laniel
[hidden email]
+(617) 308-5571
http://laniels.org/
PGP key: http://laniels.org/slaniel.key

signature.asc (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Sorting all lines matching a pattern

Alan G Isaac
On Sun, 24 Jul 2005, Stephen R Laniel apparently wrote:

> Suppose that interspersed throughout my document, I have a
> bunch of lines that look like

> * Abbey, Edward. "The Monkey Wrench Gang"[book]
> * Adams, Henry. "The Education of Henry Adams"[book]
> * Atwood, Margaret. "The Handmaid's Tale"[book]
> * Aurelius, Marcus, "Somethingorother"[book]

> I'd like to sort all these lines together. I thought that

> :g/^\*/!sort

> would group all of these lines together and sort them
> relative to one another. But it's not so -- I think the 'g'
> command is treating them one by one, yes?

> Is there any way to do what I want?

This is probably not what you wanted, but
Go<esc>:g/^\*/co $<enter>V{:!sort

Of course you can move them instead of copy them if you
wish.

fwiw,
Alan


Reply | Threaded
Open this post in threaded view
|

Re: Sorting all lines matching a pattern

Tim Chase-2
In reply to this post by Stephen R Laniel
> I'd like to sort all these lines together. I thought that
>
> :g/^\*/!sort
>
> would group all of these lines together and sort them
> relative to one another. But it's not so -- I think the 'g'
> command is treating them one by one, yes?
>
> Is there any way to do what I want?

Yes, it is treating each line individually.  A solution might be
something like the following, assuming

0) the 1st character in the file isn't an asterisk (the first
line isn't one of interest)
1) you want the lines sorted at the top

You can do

        :1ka | exec('g/^\*/m0') | 1,'a-1 !sort

or

        :1ka | exec('g/^\*/t0') | 1,'a-1 !sort

depending on whether you want to move the lines to the top of the
file and sort them (the first one) or copy the lines to the top
of the file and sort them (the second one).  It does tromp on the
"a" bookmark, but I couldn't find a nice way around that.

If you want to break it down, it marks the first line in the
file, then either copies or moves each matching line before the
first line.  It now knows that the range from the first line,
through the line preceeding the mark we dropped are the lines you
want to sort, so it can then pass that range through your
system's sort command.

If you want them elsewhere, that may be another matter.  Or you
can yank or delete all the germane lines into a register and then
manipulate them outside your document, with either

        :let @a='' | g/^\*/d A

to delete those lines, or

        :let @a='' | g/^\*/y A

You'll then have all the resulting lines in the "a" register, to
paste in another buffer and run the sort command over if you like:

        :%!sort

you can then re-yank and drop the results where you please in
your original file.

HTH,

-tim