Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;;; foo.el begins here
- ;; input.txt :
- ;; mmmm;NEW;24
- ;; nnnn;OLD;25
- ;; nnnn;NEW;26
- ;; no pair oooo;NEW;28
- ;; pppp;OLD;29
- ;; pppp;NEW;30
- ;; no pair qqqq;NEW;32
- ;; rrrr;OLD;33
- ;; rrrr;NEW;34
- ;; ssss;OLD;35
- ;; ssss;NEW;36
- ;; aaaa;OLD;1
- ;; aaaa;NEW;2
- ;; bbbb;OLD;3
- ;; bbbb;NEW;4
- ;; cccc;OLD;5
- ;; cccc;NEW;6
- ;; dddd;OLD;7
- ;; dddd;NEW;8
- ;; ffff;OLD;9
- ;; ffff;NEW;10
- ;; no pair gggg;OLD;11
- ;; hhhh;OLD;13
- ;; hhhh;NEW;14
- ;; iiii;OLD;15
- ;; iiii;NEW;16
- ;; jjjj;OLD;17
- ;; jjjj;NEW;18
- ;; kkkk;OLD;19
- ;; kkkk;NEW;20
- ;; no pair llll;OLD;21
- ;; no pair mmmm;OLD;23
- ;; tttt;OLD;37
- ;; tttt;NEW;38
- ;; run:
- ;; $ emacs -batch -l ./foo.el -eval '(foo "./input.txt" "./output.txt")'
- ;; output.txt :
- ;; 25 = 26
- ;; 29 = 30
- ;; 33 = 34
- ;; 35 = 36
- ;; 1 = 2
- ;; 3 = 4
- ;; 5 = 6
- ;; 7 = 8
- ;; 9 = 10
- ;; 13 = 14
- ;; 15 = 16
- ;; 17 = 18
- ;; 19 = 20
- ;; 37 = 38
- (defun foo (src trg)
- ;(interactive "fSource file :")
- (let ((result ""))
- (with-temp-buffer
- (insert-file-contents src)
- (beginning-of-buffer)
- (let (line text type id p)
- (while (not (eobp))
- (setq line (thing-at-point 'line))
- (if (string-match "\\([a-zA-Z ]+\\);\\(OLD\\|NEW\\);\\([0-9]+\\)" line)
- (progn
- (setq text (match-string 1 line)
- type (match-string 2 line)
- id (match-string 3 line)
- pair nil)
- (cond
- ((string= type "OLD")
- (setq pair "NEW"))
- ((string= type "NEW")
- (setq pair "OLD")))
- (progn
- (next-line)
- (let ((nline (thing-at-point 'line))
- (nid nil))
- (if (string-match (format "%s;%s;\\([0-9]+\\)" text pair) nline)
- (progn
- (setq nid (match-string 1 nline))
- (setq result (concat result (format "%s = %s\n" id nid))))
- (previous-line))))))
- (next-line))))
- (with-temp-file trg
- (insert result))))
- ;; foo.el ends here
Add Comment
Please, Sign In to add comment