47371Re: sort & remove duplicate lines?
- Feb 1, 2004Tim Musson <Tim@...> wrote:
> vim,Unix has a filter called "uniq" to remove duplicate lines, so you may do
> I use Vim for everything, but find myself going to TextPad for one
> or 2 things. I was wondering if there is a Vim equivalent?
> The first one and the one I need most often is "sort & remove
> duplicate lines". I know I can sort easily with ":n,n!sort", but I
> don't know how to remove dups. Can anyone point me in the right
> The other thing I do is repetitive processing (called Macros in
> TextPad). For example, I have a list of files in my http/pix
> directory. So I list them into the file;
> Next I make it a html document displaying the image and it's name.
> <img src=img1.phg width=99>img1.png<br>
> <img src=img2.phg width=99>img2.png<br>
> (I don't do this any more, but it was a simple example)
> Thanks in advance for any pointers!
> Tim Musson
> Flying with The Bat! eMail v2.01.3
> Windows 2000 5.0.2195 (Service Pack 3)
> Printed on 100% recycled electrons.
(outside of Vim)
sort < inputfilename | uniq > outputfilename
If Windows has anything similar to that "uniq" filter, I'm not aware of it
(the "sort" filter exists, with somewhat different command-line parameters
than under Unix).
Vim has utility scripts for sorting without calling an external program.
IIRC, one such has been "announced" recently on this list. Or you might
browse the "scripts" at vim-online ( http://vim.sourceforge.net/ ) using the
site's search engine, looking for "sort".
Once the lines are sorted, removing duplicates should be rather easy, maybe
by building a user-command and/or a user-function (with range defaulting to
the whole file) which would call getline() as many times as there are lines
in the range, keeping the first line and, starting at the 2nd line of the
range, removing any line which is equal to the previous one.
Let's have a try at it (untested)
command Uniq -range=% call RemoveDuplicates()
function RemoveDuplicates() range
let i = a:firstline + 1
let endl = a:lastline
while i <= endl
if getline(i) == getline(i-1)
exe i . "," . i . " delete"
let endl = endl - 1
let i = i+1
Repetitive processing in Vim uses user-commands, user-functions, mappings,
and abbreviations. Those are four different things. See the help items
mentioned above, and also the various sections of
Oh, yes, and there are also autocommands (for repetitive processing whenever
some "events" are triggered) but maybe you will want to read about that
later, when you have a lot of time for it.
- << Previous post in topic Next post in topic >>