all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: <tomas@tuxteam.de>
To: help-gnu-emacs@gnu.org
Subject: Re: Search and replace for a single file using a pattern file
Date: Thu, 4 Jan 2018 11:50:20 +0100	[thread overview]
Message-ID: <20180104105020.GB18163@tuxteam.de> (raw)
In-Reply-To: <240d8da5-5078-4ecc-a285-0719841d124a@googlegroups.com>

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



  parent reply	other threads:[~2018-01-04 10:50 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-01-04 10:02 Search and replace for a single file using a pattern file Angus Comber
2018-01-04 10:28 ` Robert Pluim
2018-01-04 10:50 ` tomas [this message]
2018-01-04 15:05 ` Rusi
2018-01-04 16:54   ` Rusi

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

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

  git send-email \
    --in-reply-to=20180104105020.GB18163@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.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.