Sunday, October 10, 2021

Re: Sort lines, then tag, then unsort

Julius Hamilton said on Tue, 14 Sep 2021 13:57:34 +0200

>I would like to sort lines in a text document and then tag them, and
>then revert the file to its original order.
>
>First of all, is there a way to tag a line, so that lines with a given
>tag can be quickly referred to, but the tag is not in the actual text?
>
>Secondly, I noticed with my sort command ":sort \^[A-Z].*[^\.]$\ r"
>(sort all lines beginning with a capital letter and not ending in a
>period) it sends the matches in alphabetical order to the bottom of
>the file. Is there a way to specify a second-order sort by which all
>the matches are sub-sorted, rather than the default alphabetical? And,
>is there a way to send them to the top of the document rather than the
>bottom?

I think you're using the wrong tool. Also, you haven't said why you
need Vim tags on lines. If I'm not mistaken, there are only a limited
number of tags, and if you have that few lines, you're better off doing
this all manually.

Anyway, the tool I'd use is AWK. Not one liners, but real AWK programs.

My first step would be to put every about 3 underscores followed by
the line number at the end of every line, but add one million to
the line number so all line numbers have the same length. This is
trivial in AWK. Now you have a record of the original sort order.

Next, I'd perform whatever kind of two stage or branched sorting you
need to do, once again with AWK.

Next, do whatever you need to do in order to simulate Vim tags. Because
you haven't told us what the tags are used for, I can't comment further.

Next, do whatever you need to do with those simulated tags.

Finally, within Vim, move the ending line numbers to the front of each
line, and re-sort again, to put it in the original order. Finally, use
Vim to get rid of those line numbers.

SteveT

Steve Litt
Spring 2021 featured book: Troubleshooting Techniques of the Successful
Technologist http://www.troubleshooters.com/techniques

--
--
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 vim_use+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20211010042520.5103b5c2%40mydesk.domain.cxm.

No comments: