From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Newsgroups: gmane.emacs.help Subject: Re: How to do search and replace with a lookup Date: Mon, 11 Dec 2017 11:03:08 +0100 Message-ID: <20171211100308.GB30050@tuxteam.de> References: <2a6008be-da30-401d-b48f-da36884bafbd@googlegroups.com> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; x-action=pgp-signed Content-Transfer-Encoding: 8bit X-Trace: blaine.gmane.org 1512986670 15919 195.159.176.226 (11 Dec 2017 10:04:30 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Mon, 11 Dec 2017 10:04:30 +0000 (UTC) User-Agent: Mutt/1.5.21 (2010-09-15) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Mon Dec 11 11:04:26 2017 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eOKwj-0003vh-UB for geh-help-gnu-emacs@m.gmane.org; Mon, 11 Dec 2017 11:04:26 +0100 Original-Received: from localhost ([::1]:52006 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eOKwr-0005uv-8j for geh-help-gnu-emacs@m.gmane.org; Mon, 11 Dec 2017 05:04:33 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:60269) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eOKwJ-0005mS-3c for help-gnu-emacs@gnu.org; Mon, 11 Dec 2017 05:04:05 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eOKvf-0006F3-E8 for help-gnu-emacs@gnu.org; Mon, 11 Dec 2017 05:03:59 -0500 Original-Received: from mail.tuxteam.de ([5.199.139.25]:36990) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eOKvf-00064i-0P for help-gnu-emacs@gnu.org; Mon, 11 Dec 2017 05:03:19 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tuxteam.de; s=20171004; h=From:In-Reply-To:Content-Transfer-Encoding:Content-Type:MIME-Version:References:Message-ID:Subject:To:Date; bh=5zTdwqFkkqEyE52KKLXoHijc9sdU+MAPAaTNvPVsALY=; b=CNtRVjz5bv4j7x+y6TqG67hHHAPTjwqrZNuhLRKK/yDG4+VIxfeNAcM5DS8Hj/A3MCzWfz+mr0+PDjCZ453Ltbf3Twc5VYTiQ6rIbAH6Gk3BJ0NfBm8vxny8QL5jVuz4h6mR+0wI6gCdl+TcG+OP+JUSXrosHc/rY1S/5KY8lgks6P9nlyB3AVakG5wYUkdHZpp/Fn9nisz202KhvBopJrlNYnztQ/Vp+6f1bzixgWUMEjBbHTJNxHJ3EGZR8JfACKao9OwzuEImm889djL+BCz3Hm2H1FIu7YTwM4MXCGq1n0rVqF7tlebF6FFH03GoXdazd19aQD0gbbC3oWSX0g==; Original-Received: from tomas by mail.tuxteam.de with local (Exim 4.80) (envelope-from ) id 1eOKvU-0000Rt-Hi for help-gnu-emacs@gnu.org; Mon, 11 Dec 2017 11:03:08 +0100 In-Reply-To: <2a6008be-da30-401d-b48f-da36884bafbd@googlegroups.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 5.199.139.25 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.org gmane.emacs.help:115332 Archived-At: -----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-----