Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to do search and replace with a lookup

5 views
Skip to first unread message

Angus Comber

unread,
Dec 11, 2017, 3:49:50 AM12/11/17
to
I have log file output like this:

Received device state message: pen state = 11 - uig state 10

where pen state and uig state are enums.

I want to do a search and replace where if for example pen state 11 enum is diagnostics and uig state 10 enum is starting, then I want the line to be updated to:

Received device state message: pen state = diagnostics - uig state starting

What is the easiest way to do this?

I can do search and replace using c-m-% and change for each permutation of number to string but that would be fairly labour intensive if enums were fairly long. Can I combine that with a lookup somehow? What is best way to do it?

to...@tuxteam.de

unread,
Dec 11, 2017, 5:04:09 AM12/11/17
to help-gn...@gnu.org
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
If you do query-replace-regexp, you can use \,(...) in the
substitution text for a Lisp expression to be evaluated at
substitution time. You can pass it (parts of) the matched
string with the usual \0, \1, etc.

Thus if you have a mapping available for your enums which
is accessible from Lisp (an alist, a hash, whatever) you
should be half way there.

The manual[1] has a little example (it starts at the point
"Most readers can skip it", so make sure to *not* skip it :-)

The Emacs Wiki[2] has a couple of nice examples.

Just yell if you need more help.

Cheers
[1] https://www.gnu.org/software/emacs/manual/html_node/emacs/Regexp-Replace.html
[2] https://www.emacswiki.org/emacs/ReplaceRegexpWithLispExpressions

- -- tomás
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEARECAAYFAlouV9wACgkQBcgs9XrR2kasoQCfTqWd/At13mpCPtAZn+9VgM65
J+gAn3/AFIu+nwhfK4IJE6db0unak4qt
=0GSb
-----END PGP SIGNATURE-----

0 new messages