I want to view ~/.zsh_history
files the way they are displayed by the history
command with raw unixtimestamps converted to properly formatted dates.
: 1568128379:0;cp -a ~/.zshrc.pre-oh-my-zsh ~/.zshrc
: 1568128381:0;exit
So a command like readhist.sh.el < ~/.zsh_history
on the lines above would produce output like
1 2023-10-21 cp -a ~/.zshrc.pre-oh-my-zsh ~/.zshrc
2 2023-10-21 exit
The first part was to get it to work in the editor which I solved in the link - https://emacs.stackexchange.com/a/79254/15477.
In the editor
the search string is: \(: \)\([0-9]\{10\}\)\(:0;\)
the replacement string: \,(concat (format "%6d " 9999) (rgx-get-time-string (match-string 2)) " "))
with rgx-get-time-string
being a function to convert the unixtimestamp correctly.
(defun rgx-get-time-string (unixtimestr)
(format-time-string "%Y-%m-%d %H:%M" (string-to-number unixtimestr)))
My attempt to convert to a script results the replacement inserted literally rather than computed.
The 9999
in the concat
command is a place holder I will replace with an appropriate expression later.later.
!/usr/local/bin/emacs --script
;;-*- mode: emacs-lisp;-*-
(defun process (histline)
"return the usual string"
(replace-regexp-in-string "\\(: \\)\\([0-9]\\{10\\}\\)\\(:0;\\)"
"\,(concat (format \"%6d \"
9999)
(rgx-get-time-string (match-string 2)) \" \"))" histline))
(defun rgx-get-time-string (unixtimestr)
(format-time-string "%Y-%m-%d %H:%M" (string-to-number unixtimestr)))
(condition-case nil
(let (line)
(while (setq line (read-from-minibuffer ""))
;;(princ line)
(princ (process line))
(princ "\n")))
(error nil))
\,
syntax used inreplace-regexp
when the replacement is a lisp expression doesn't seem to work withreplace-regexp-in-string
\,
syntax inreplace-regexp
only works when you call it interactively. There is no such facility forreplace-regexp-in-string
(it's not an interactive function, i.e. a command) but you can use a function, instead of a string, as the replacement.C-h f replace-regexp-in-string
is, as always, your friend.