0

I would like to sort all lines in a file based on where the text of the line starts, after a line index which is printed into the file, like this:

126.    This is the start of the line.

It was written with the function printf(%d.\t).

A sort function to sort lines with capital letters and not ending in a period I use is :sort /^[A-Z].*[^\.]/ r.

How can I adapt this sort function to ignore the number prefix at the beginning of each line? :sort /^\d*\.\t[A-Z].*[^\.]/ r should sort each line according to its number, rather than ignoring the number, so I think will need syntax for ignoring or skipping.

0

1 Answer 1

4

You want to sort starting with the text that follows the opening numbers?

So use a pattern that matches the numbers (and period and following whitespace)...

:sort /^\d\+\.\s*/

Note lack of r flag. The default behavior (i.e. with no flags) is to skip the text matched by the pattern and sort on text that follows it.

4
  • 1
    Nailed it. Thx. Commented Sep 20, 2021 at 12:13
  • I believe this will match with all the lines and then sort them in numerical order, but omitting the number; but I would like to sort the lines if they match my pattern above (a line that begins with a capital letter and doesn't end with a period). So I still think there will need to be some syntax to ignore the prefixes. But I think if I just write the prefix in in generic form: ^\d\+\.\s* followed by my pattern that would work. Commented Sep 20, 2021 at 16:31
  • It doesn't sort in numerical order. It's sorting in alphanumeric order based on the text that comes after the numbers. Assuming that's what you meant to say your next part suggests you want to sort only some lines in a given continuous range of lines. That's not possible with :sort. The pattern is only for determining which part of each and every line in the range is used for sorting. (The range in this case being the entire file since no explicit range is specified.)
    – B Layer
    Commented Sep 20, 2021 at 19:46
  • If that's not what you have in mind please explain in more detail what you're trying to do.
    – B Layer
    Commented Sep 20, 2021 at 20:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.