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: Search and replace for a single file using a pattern file Date: Thu, 4 Jan 2018 11:50:20 +0100 Message-ID: <20180104105020.GB18163@tuxteam.de> References: <240d8da5-5078-4ecc-a285-0719841d124a@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 1515062958 30582 195.159.176.226 (4 Jan 2018 10:49:18 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Thu, 4 Jan 2018 10:49:18 +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 Thu Jan 04 11:49:13 2018 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 1eX35C-0007Nh-9b for geh-help-gnu-emacs@m.gmane.org; Thu, 04 Jan 2018 11:49:10 +0100 Original-Received: from localhost ([::1]:48245 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eX379-0006PG-Un for geh-help-gnu-emacs@m.gmane.org; Thu, 04 Jan 2018 05:51:11 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:44881) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eX36Z-0006Ja-9A for help-gnu-emacs@gnu.org; Thu, 04 Jan 2018 05:50:36 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eX36T-0006SO-Be for help-gnu-emacs@gnu.org; Thu, 04 Jan 2018 05:50:35 -0500 Original-Received: from mail.tuxteam.de ([5.199.139.25]:50559) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eX36S-0006NJ-Ub for help-gnu-emacs@gnu.org; Thu, 04 Jan 2018 05:50:29 -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=2YLAi1lv7ZdYrMdU9dInrJn/2komEDvoqiuWKbHOPA4=; b=CdUNQVgJS6no1APhPk2WxiCK5fu0Kmlfy99diG5tEfG2I8D44NejV4YOBu6Fpk6nsoeUOkqSzJnUguoNe8IAHWQrJZ7taVk4rK6eWR5q7ktpx86rfebfs6ItIpks46OP2P1/2MSi4Ci3aqoRQQOAc2Cysb7DzGCr3jHy+7O1fp1up6uFww5J21FYA/uz8j+QYwQXl4Oh9GiZXT1PBPmz6H1XPIPKYVO+hFsxNAh2Q+WAn1CWOxjMHGUqp50DJV0o2VliEuR0NxZjHTmUYGcPdULnsA6QrPw5YPOTE6wRuFs7MlWaE2Ok7ZmR804JbGSoxIbf2/NTLzU8FR1XbsFJqw==; Original-Received: from tomas by mail.tuxteam.de with local (Exim 4.80) (envelope-from ) id 1eX36K-0005FU-QA for help-gnu-emacs@gnu.org; Thu, 04 Jan 2018 11:50:20 +0100 In-Reply-To: <240d8da5-5078-4ecc-a285-0719841d124a@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:115568 Archived-At: -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Thu, Jan 04, 2018 at 02:02:29AM -0800, Angus Comber wrote: > I have some horrible logs where integers are printed for states and I want to do a global search and replace on the file to eg replace integer x with a string. > > I can obviously do individually using c-m-% but that is fairly laborious. So use of a search and replace mapping in a text file would be really convenient. > > Is this possible? any suggestions? Yes, enter the \,(...) replacement magic. This construct means "evaluate the expression after the \, as an elisp expression". Here's some shortened version of your problem. Assume I want to replace the digits 1, 2, 3 at the beginning of the line by words assigned to them. An association list seems to be a simple representation for that mapping: #+BEGIN_SRC emacs-lisp (defvar my-codes '((1 . bread) (2 . cheese) (3 . wine))) #+END_SRC Add associations to taste ;-P Now check that our alist is working as supposed: to get the "value" part for a "key", there's alist-get: #+BEGIN_SRC emacs-lisp (alist-get 2 my-codes) #+END_SRC And that results in... #+RESULTS: : cheese Seems fine. Our test data (NOTE in real life not indented. I indent it here to ease reading): 1 was my first meal 2 came after that and 3 to rinse it all Apply the following "query-replace regexp": ^\([0-9]\) → \,(alist-get (string-to-number \1) my-codes) Note two things: - within the lisp expression you have access to the partial matches as \1, \2 etc. They are substituted as strings (that's why there is the (string-to-number ...) there: our assoc list above has numbers as keys) - a more convenient way of writing (string-to-number \1) in the replacement string exists: it's just \#1 (likewise for 2, 3, etc, of course). HTH - -- tomás -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.12 (GNU/Linux) iEYEARECAAYFAlpOBuwACgkQBcgs9XrR2kbRewCfe9niOSCPHVolaWH8q0C7kVh8 kvUAn1wIkX1lxQiQD0EZTZhZih96JLjz =RzFW -----END PGP SIGNATURE-----