Alexei
Alexei

Reputation: 15726

Emacs. How remove duplicate lines that contain some text

Windows 10 (64 bit), Emacs 25.1

Suppose I has text:

111111111 aaaaaaaa bbbbbbbbbb

222222 3333333333 44444444

111111111 aaaaaaaa bbbbbbbbbb

111111111 aaaaaaaa bbbbbbbbbb

44444444 666666666 777777777777

111111111 aaaaaaaa bbbbbbbbbb

So I want to remove duplicate lines that contain aaaaaaaa.

Result must be this:

111111111 aaaaaaaa bbbbbbbbbb

222222 3333333333 44444444

44444444 666666666 777777777777

I want to use built-in capabilities of Emacs (without write custom elisp script).

Upvotes: 3

Views: 963

Answers (1)

phils
phils

Reputation: 73344

If you wish to remove all duplicate lines from the buffer (regardless of whether or not they contain "aaaaaaaa"), then use:

  • C-xh
  • M-x delete-duplicate-lines RET

Note that your desired result includes the removal of all blank lines (rather than retaining one of them), so Emacs' result is different on that account.


If you want to remove all lines containing "aaaaaaaa" then use:

  • M-x flush-lines RET aaaaaaaa RET

If you issue this with point after the first instance, then it won't remove that first instance.


If you want the behaviour of delete-duplicate-lines but restricted to acting only on lines containing "aaaaaaaa", then I don't know of a standard command for that (although it would be a relatively simple enhancement to delete-duplicate-lines to introduce such a feature).

Upvotes: 6

Related Questions