CRS
CRS

Reputation: 75

Sort a list by digits appearing after trash digits in VIM

I need to sort and diff a few lists that have 1,000s of entries on them. The lists looks like:

11-2-3049_2007_squib.pdf
11-11-5476_2004_squib.pdf
13-2-5477_2006_squib.pdf
14-3-3045A_2004_squib.pdf
14-CMF-3046_2004_squib.pdf
14-2-3047_2005_squib.pdf
14-4-3048_2004_squib.pdf
15-7-3050P_2004_squib.pdf

I'm looking to sort by the number between the second - and before the _, such as 3049 in the first example.

I've not been able to combine the sort by column and regex stuff with any success. What do you all suggest?

Upvotes: 5

Views: 733

Answers (2)

sehe
sehe

Reputation: 393467

This should do the trick:

:sort r  /\v^(.{-}-){2}\zs.{-}\ze_/

See

:help sort

for background here

The regex contains a few twists and turns:

  • \v to engage very magic mode (reducing the need for escaping)
  • \zs and \ze to mark the begin and end of the actual match result
  • {-} to perform non-greedy kleene-star match (in Perl notation, .{-} would be .*?)

Upvotes: 8

William Pursell
William Pursell

Reputation: 212404

Use the external sort program:

:%!sort -n -t- -k3,3

Upvotes: 4

Related Questions