sorting/randomizing text blocks separated by empty lines

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

sorting/randomizing text blocks separated by empty lines

Chris Lott
Given a document with text blocks separated by empty lines:

```
foo
chunk one

bar
chunk two

baz
chunk three
```

I need to both (for different purposes) 1) sort the blocks by their
first lines and then 2) randomize the blocks by their first line.

How might one (I) do this?

c
--
Chris Lott <[hidden email]>

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/groups/opt_out.
Reply | Threaded
Open this post in threaded view
|

Re: sorting/randomizing text blocks separated by empty lines

John Little-4
If you can write a regex that matches the first lines, I've posted a "somewhat brutish" method for doing the sort in the past:

https://groups.google.com/forum/#!searchin/vim_use/sort$20John$20Little/vim_use/EHULejNIHz4/p9Dh6IILRtMJ

(If your chunks are long, tread carefully with :set ul=-1).

It's a variation on the decorate-sort-undecorate method. If your first lines aren't distinctive other than being preceded by an empty line, I found I had to do two decorate steps, and other fiddling:

    :normal ggO                 " start file with blank line, so that the first
                                " first line has a blank line before it
    :g/\(^\n\)\@<=./s/$/xyzzy   " mark first lines
    :v/xyzzy/s/$/plugh/         " mark the others
    :1dd                        " delete the added line
    :g/plugh/-j!                " join non-first lines to the one before it
    :sort
    :%s/plughplugh/\r/g         " split lines again
    :%s/plugh/\r/g        
    :%s/xyzzy/\r/g        

To sort the chunks randomly, you'd have to decorate the lines with a random number.  If you're on something Gnu-ish, piping through shuf would do; replace the :sort with

    :%!shuf

Regards, John Little

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/groups/opt_out.
Reply | Threaded
Open this post in threaded view
|

Re: sorting/randomizing text blocks separated by empty lines

Charles E Campbell Jr
In reply to this post by Chris Lott
Chris Lott wrote:

> Given a document with text blocks separated by empty lines:
>
> ```
> foo
> chunk one
>
> bar
> chunk two
>
> baz
> chunk three
> ```
>
> I need to both (for different purposes) 1) sort the blocks by their
> first lines and then 2) randomize the blocks by their first line.
>
> How might one (I) do this?
>
You could try using my vissort plugin
(http://www.drchip.org/astronaut/vim/index.html#VISSORT).  It contains a
function, BlockSort, which will sort blocks of text. From its help:

    :'<,'>BS
    :'< '>BS nextblock endblock findtag tagpat tagsub
    :[range]call BlockSort(nextblock,endblock,findtag,tagpat,tagsub)
    :[range]call BlockSort(...)

               If any arguments are missing, BlockSort()
                     will query the user for them.

     This function's purpose is to sort blocks of text based on tags
     contained within the blocks.

To do a randomization, you'd need to prepend/append randomized tags for
BlockSort to work with.

Regards,
C Campbell

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/groups/opt_out.