Sorting lines with numbers and word characters
I recently wrote a simple utility in Perl to 开发者_开发百科count words in a file to determinate its frequency, that's how many times it appears.
It's all fine, but I'd like to sort the result to make it easier to read. An output example would be:
4:an
2:but
5:does
10:end
2:etc
2:for
As you can see, it's ordered by word, not frequency. But with a little help of :sort
I could reorganize that. Using n
, numbers like 10 go to the right place (even though it start with 1), plus a little !
and the order gets reversed, so the word that appears more is the first one.
:sort! n
10:end
5:does
4:an
2:for
2:etc
2:but
The problem is: when the number is repeated it gets sorted by word — which is nice — but remember, the order was reversed!
for -> etc -> but
How can I fix that? Will I have to use some Vim scripting to iterate over each line checking whether it starts with the previous number, and marking relevant lines to sort them after the number changes?
tac | sort -nr
does this, so select the lines with shift+V and use !
From the vim
:help sort
:
The details about sorting depend on the library function used. There is no
guarantee that sorting is "stable" or obeys the current locale. You will have
to try it out.
As a result, you might want to perform the sorting in your Perl script instead; it wouldn't be hard to extend your Perl sort
to be stable, see perldoc sort
for entirely too many details.
If you just want this problem finished, then you can replace your :sort
command with this:
!sort -rn --stable
(it might be easiest to use Shift-V to visually select the lines first, or use a range for the sort, or something similar, but if you're writing vim
scripts, none of this will be news to you. :)
精彩评论