2

Emacs 26.1. Windows 10

In buffer I has text like this:

    3333333
    1111111
    $PAC_LTC
    1337_BTC
    1337_LTC
    404_BTC
    42_BTC
    222222
    42_LTC
    808_ETH
    8BIT_BTC
    8BIT_BTC
    8BIT_BTC
    8BIT_DOGE
    8BIT_ETH
    8BIT_LTC
    ACC_DOGE
    ACC_USDT
    ACC_USDT
    ADC_DOGE
    ADC_DOGE
    ADC_DOGE
    ADC_ETH
    ADC_USDT
    8BIT_USDT
    8BIT_USDT
    42_ETH

I need to keep only duplicate lines. The result must be like this:

8BIT_BTC
8BIT_BTC
8BIT_BTC
8BIT_USDT
8BIT_USDT
ACC_USDT
ACC_USDT
ADC_DOGE
ADC_DOGE
ADC_DOGE

P.S. I try by this command: delete-non-matching-lines

enter image description here

But after press Enter whole text delete from buffer.

  • All repeated lines in your example are consecutive. Is that always the case? (In that case one can skip the sorting.) Does the order of the lines matter? – Tobias Feb 22 at 16:47
  • No. It's not always the case. Lines can be not sorted. – a_subscriber Feb 22 at 16:48
2

Here's a command that finds all duplicate lines in the buffer and deletes the rest:

(defun keep-duplicate-lines ()
  (interactive)
  (let (lines dups)
    (save-excursion
      (goto-char (point-max))
      (when (/= (char-after (1- (point-max))) ?\n)
        (newline))
      (goto-char (point-min))
      (while (not (eobp))
        (forward-line 1)
        (push (buffer-substring-no-properties (line-beginning-position)
                                              (line-end-position))
              lines))
      (dolist (line lines)
        (when (and (> (cl-count line lines :test 'equal) 1)
              (not (string= "" line)))
          (push (regexp-quote line) dups)))
      (goto-char (point-min))
      (keep-lines (mapconcat #'identity
                  dups
                  "\\|")))))
  • I get error: Symbol’s function definition is void: count – a_subscriber Feb 22 at 17:05
  • 1
    @jagrg You should use cl-count instead of count. cl-count is autoloaded. – Tobias Feb 22 at 17:10
0

The following answer addresses the original version of the question "How keep only duplicate lines?". It shows the duplicates in an auxiliary buffer and keeps the original buffer unmodified.

  1. Copy the whole buffer with C-x h M-w.
  2. Create a new buffer with C-x b *work* RET. (Replace *work* with a buffer name to your likings.)
  3. Yank the text there with C-y
  4. Mark the whole buffer *work* by C-x h and sort its lines with M-x sort-lines RET
  5. Mark the whole buffer by C-x h and delete all lines that do not occur multiple times in sequence with M-x delete-non-matching-lines RET ^\(.*\)\( C-q C-j \1$\)+ RET.

Note: The sequence C-q C-j inserts a verbatim newline in the minibuffer.
Only the gray sections ^\(.*\)\( and \1$\)+ are intended for copy-paste. You must type the part C-q C-j in the middle of the regexp as key sequence with the Ctrl button pressed.


If you want to modify the buffer such that you only keep the replicated lines you can skip steps 1, 2, and 3. Furthermore, use the original buffer instead of *work* in step 4. I.e.:

  1. Mark the whole buffer by C-x h and sort its lines with M-x sort-lines RET
  2. Mark the whole buffer by C-x h and delete all lines that do not occur multiple times in sequence with M-x delete-non-matching-lines RET ^\(.*\)\( C-q C-j \1$\)+ RET.
  • @Alexei It should work. I have tested it. Make sure to sort before applying delete-non-matching-lines. Be careful when typing the regexp (note that you cannot copy-paste but must type at least the C-q C-j). Please try again. – Tobias Feb 22 at 16:54
  • @Alexei You may not copy-paste the regexp as is but you must type C-q C-j as key sequence with C- standing for the pressed control key. Only the two gray sections ^\(.*\)\( and \1$\)+ of the regexp are intended for copy paste. – Tobias Feb 22 at 16:56
  • Thanks. Now it's work. I was copy paste C-q C-j. But I MUST input C-q C-j – a_subscriber Feb 22 at 16:59
  • @Alexei Yes, you need to use the key sequence C-q C-j. I modified my answer to state that more clear. Please remove the note about the non-working answer from your question – Tobias Feb 22 at 17:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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