Guest User

Untitled

a guest
Dec 12th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. ;;; foo.el begins here
  2.  
  3. ;; input.txt :
  4.  
  5. ;; mmmm;NEW;24
  6. ;; nnnn;OLD;25
  7. ;; nnnn;NEW;26
  8. ;; no pair oooo;NEW;28
  9. ;; pppp;OLD;29
  10. ;; pppp;NEW;30
  11. ;; no pair qqqq;NEW;32
  12. ;; rrrr;OLD;33
  13. ;; rrrr;NEW;34
  14. ;; ssss;OLD;35
  15. ;; ssss;NEW;36
  16. ;; aaaa;OLD;1
  17. ;; aaaa;NEW;2
  18. ;; bbbb;OLD;3
  19. ;; bbbb;NEW;4
  20. ;; cccc;OLD;5
  21. ;; cccc;NEW;6
  22. ;; dddd;OLD;7
  23. ;; dddd;NEW;8
  24. ;; ffff;OLD;9
  25. ;; ffff;NEW;10
  26. ;; no pair gggg;OLD;11
  27. ;; hhhh;OLD;13
  28. ;; hhhh;NEW;14
  29. ;; iiii;OLD;15
  30. ;; iiii;NEW;16
  31. ;; jjjj;OLD;17
  32. ;; jjjj;NEW;18
  33. ;; kkkk;OLD;19
  34. ;; kkkk;NEW;20
  35. ;; no pair llll;OLD;21
  36. ;; no pair mmmm;OLD;23
  37. ;; tttt;OLD;37
  38. ;; tttt;NEW;38
  39.  
  40. ;; run:
  41. ;; $ emacs -batch -l ./foo.el -eval '(foo "./input.txt" "./output.txt")'
  42.  
  43. ;; output.txt :
  44.  
  45. ;; 25 = 26
  46. ;; 29 = 30
  47. ;; 33 = 34
  48. ;; 35 = 36
  49. ;; 1 = 2
  50. ;; 3 = 4
  51. ;; 5 = 6
  52. ;; 7 = 8
  53. ;; 9 = 10
  54. ;; 13 = 14
  55. ;; 15 = 16
  56. ;; 17 = 18
  57. ;; 19 = 20
  58. ;; 37 = 38
  59.  
  60. (defun foo (src trg)
  61. ;(interactive "fSource file :")
  62. (let ((result ""))
  63. (with-temp-buffer
  64. (insert-file-contents src)
  65. (beginning-of-buffer)
  66. (let (line text type id p)
  67. (while (not (eobp))
  68. (setq line (thing-at-point 'line))
  69. (if (string-match "\\([a-zA-Z ]+\\);\\(OLD\\|NEW\\);\\([0-9]+\\)" line)
  70. (progn
  71. (setq text (match-string 1 line)
  72. type (match-string 2 line)
  73. id (match-string 3 line)
  74. pair nil)
  75. (cond
  76. ((string= type "OLD")
  77. (setq pair "NEW"))
  78. ((string= type "NEW")
  79. (setq pair "OLD")))
  80. (progn
  81. (next-line)
  82. (let ((nline (thing-at-point 'line))
  83. (nid nil))
  84. (if (string-match (format "%s;%s;\\([0-9]+\\)" text pair) nline)
  85. (progn
  86. (setq nid (match-string 1 nline))
  87. (setq result (concat result (format "%s = %s\n" id nid))))
  88. (previous-line))))))
  89. (next-line))))
  90. (with-temp-file trg
  91. (insert result))))
  92.  
  93. ;; foo.el ends here
Add Comment
Please, Sign In to add comment