0

I have a buffer with the following content and I need to delete the duplicate lines.

57,04/15/2020 03:02PM,'Weight'='2.478 lb','tare'='0.918 lb'
57,04/15/2020 03:02PM,'Weight'='2.478 lb','tare'='0.918 lb'
57,04/15/2020 03:02PM,'Weight'='2.478 lb','tare'='0.918 lb'
57,04/15/2020 03:02PM,'Weight'='2.478 lb','tare'='0.918 lb'
57,04/15/2020 03:02PM,'Weight'='2.478 lb','tare'='0.918 lb'
58,04/15/2020 03:03PM,'Weight'='2.441 ','tare'='0.969 '
58,04/15/2020 03:03PM,'Weight'='2.441 ','tare'='0.969 '
58,04/15/2020 03:03PM,'Weight'='2.441 ','tare'='0.969 '
58,04/15/2020 03:03PM,'Weight'='2.441 ','tare'='0.969 '
59,04/15/2020 03:03PM,'Weight'='2.452 kg','tare'='0.517 kg'
59,04/15/2020 03:03PM,'Weight'='2.452 kg','tare'='0.517 kg'
60,04/15/2020 03:04PM,'Weight'='2.403 lb','tare'='0.526 lb'
60,04/15/2020 03:04PM,'Weight'='2.403 lb','tare'='0.526 lb'
60,04/15/2020 03:04PM,'Weight'='2.403 lb','tare'='0.526 lb'
60,04/15/2020 03:04PM,'Weight'='2.403 lb','tare'='0.526 lb'
61,04/15/2020 03:04PM,'Weight'='2.497 kg','tare'='0.738 kg'
61,04/15/2020 03:04PM,'Weight'='2.497 kg','tare'='0.738 kg'
61,04/15/2020 03:04PM,'Weight'='2.497 kg','tare'='0.738 kg'
61,04/15/2020 03:04PM,'Weight'='2.497 kg','tare'='0.738 kg'
62,04/15/2020 03:05PM,'Weight'='2.47 ','tare'='0.625 '
62,04/15/2020 03:05PM,'Weight'='2.47 ','tare'='0.625 '
62,04/15/2020 03:05PM,'Weight'='2.47 ','tare'='0.625 '
62,04/15/2020 03:05PM,'Weight'='2.47 ','tare'='0.625 '
63,04/15/2020 03:05PM,'Weight'='2.425 lb','tare'='0.515 lb'
63,04/15/2020 03:05PM,'Weight'='2.425 lb','tare'='0.515 lb'
63,04/15/2020 03:05PM,'Weight'='2.425 lb','tare'='0.515 lb'
63,04/15/2020 03:05PM,'Weight'='2.425 lb','tare'='0.515 lb'
63,04/15/2020 03:05PM,'Weight'='2.425 lb','tare'='0.515 lb'
64,04/15/2020 03:06PM,'Weight'='2.431 oz','tare'='0.703 oz'
64,04/15/2020 03:06PM,'Weight'='2.431 oz','tare'='0.703 oz'
64,04/15/2020 03:06PM,'Weight'='2.431 oz','tare'='0.703 oz'
64,04/15/2020 03:06PM,'Weight'='2.431 oz','tare'='0.703 oz'

For this I was thinking of using 2 keyboard macros. The first one starts with 57, in the kill ring, searches for the next occurrence of the piece of strings and deletes the line. This keyboard macro I saved as del

(fset 'del
   (lambda (&optional arg) "Keyboard macro." (interactive "p") (kmacro-exec-ring-item (quote ([19 25 return 11 11] 0 "%d")) arg)))

The second keyboard macro encompasses the first one. It starts on line 0, column 0. It sets the marker and searches for the next , and copies the string (so you have 57,), and I enter M-0 M-x del to invoke the first one until it doesn't find 57, anymore. This deletes all the lines starting with 57,

(setq last-kbd-macro
   [?\C-  ?\C-s ?, C-return ?\M-w ?\M-0 ?\M-x ?d ?e ?l return])

However the termination condition of the first keyboard macro also stops with recording of the second one. How to keep the recording open?

For now, it isn't a problem, but what if I want to add some other stuff after deleting all the same lines? I know there is kmacro-edit-macro, can it also be done without this and during the recording?

4
  • 1
    I have no answer for the general question of interacting macros, but for the particular problem at hand (deleting duplicate lines), I would recommend marking the region and then running M-x shell-command-on-region RET uniq RET and after verifying that it does what you expect, doing the same thing but with a C-u prefix to replace the region with the output of the command. – NickD Apr 15 '20 at 14:38
  • @NickD thank you for your suggestion. I did not know uniq and never userd shell-command-on-region. Yet another thing to get familiar with. However I would like an answer on my question to keep recording. I provided my example situation for context, -and for possible other solutions proposed like yours. – Swedgin Apr 15 '20 at 14:48
  • 1
    Understood. BTW, uniq is a Unix command - do man uniq at a shell prompt to read its manual page and find out how to use it. The main point of my comment above is that there are tools outside of emacs that can be used to solve problems within emacs. – NickD Apr 15 '20 at 14:57
  • There is also M-x delete-duplicate-lines, @NickD. – Omar Nov 15 '20 at 5:30

Your Answer

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

Browse other questions tagged or ask your own question.