unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: <tomas@tuxteam.de>
To: help-gnu-emacs@gnu.org
Subject: Re: How to do search and replace with a lookup
Date: Mon, 11 Dec 2017 11:03:08 +0100	[thread overview]
Message-ID: <20171211100308.GB30050@tuxteam.de> (raw)
In-Reply-To: <2a6008be-da30-401d-b48f-da36884bafbd@googlegroups.com>

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Mon, Dec 11, 2017 at 12:49:47AM -0800, Angus Comber wrote:
> 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?

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-----



      reply	other threads:[~2017-12-11 10:03 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-12-11  8:49 How to do search and replace with a lookup Angus Comber
2017-12-11 10:03 ` tomas [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20171211100308.GB30050@tuxteam.de \
    --to=tomas@tuxteam.de \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).