unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Search and replace for a single file using a pattern file
@ 2018-01-04 10:02 Angus Comber
  2018-01-04 10:28 ` Robert Pluim
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Angus Comber @ 2018-01-04 10:02 UTC (permalink / raw)
  To: help-gnu-emacs

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?


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Search and replace for a single file using a pattern file
  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
  2018-01-04 15:05 ` Rusi
  2 siblings, 0 replies; 5+ messages in thread
From: Robert Pluim @ 2018-01-04 10:28 UTC (permalink / raw)
  To: Angus Comber; +Cc: help-gnu-emacs

Angus Comber <anguscomber@gmail.com> writes:

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

Which bit is laborious? The query portion of C-M-%? You can hit !
after the first match, or else do

M-: (replace-string "from" "to")

multiple times (or wrap that in a function over your list of
replacements)

Robert



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Search and replace for a single file using a pattern file
  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
  2018-01-04 15:05 ` Rusi
  2 siblings, 0 replies; 5+ messages in thread
From: tomas @ 2018-01-04 10:50 UTC (permalink / raw)
  To: help-gnu-emacs

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



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Search and replace for a single file using a pattern file
  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
@ 2018-01-04 15:05 ` Rusi
  2018-01-04 16:54   ` Rusi
  2 siblings, 1 reply; 5+ messages in thread
From: Rusi @ 2018-01-04 15:05 UTC (permalink / raw)
  To: help-gnu-emacs

On Thursday, January 4, 2018 at 3:32:32 PM UTC+5:30, 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?

I'd combine Tomás Robert's solutions:

(defvar my-codes
    '((1 . bread)
      (2 . cheese)
      (3 . wine))) 

(defun replace-all ()
  (interactive)
  (dolist (x my-codes)
    (replace-string (number-to-string (car x))
		    (symbol-name (cdr x)))))

After which M-x replace-all
should do it


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Search and replace for a single file using a pattern file
  2018-01-04 15:05 ` Rusi
@ 2018-01-04 16:54   ` Rusi
  0 siblings, 0 replies; 5+ messages in thread
From: Rusi @ 2018-01-04 16:54 UTC (permalink / raw)
  To: help-gnu-emacs

On Thursday, January 4, 2018 at 8:36:01 PM UTC+5:30, Rusi wrote:
> On Thursday, January 4, 2018 at 3:32:32 PM UTC+5:30, 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?
> 
> I'd combine Tomás Robert's solutions:
> 
> (defvar my-codes
>     '((1 . bread)
>       (2 . cheese)
>       (3 . wine))) 
> 
> (defun replace-all ()
>   (interactive)
>   (dolist (x my-codes)
>     (replace-string (number-to-string (car x))
> 		    (symbol-name (cdr x)))))
> 
> After which M-x replace-all
> should do it

Probably I should say that sed is the way to do this more than emacs/elisp

$ cat sedsc
s/1/bread/
s/2/cheese/
s/3/wine/

$ cat txt.txt
  1 was my first meal
  2 came after that and
  3 to rinse it all 

$ sed -f sedsc txt.txt
  bread was my first meal
  cheese came after that and
  wine to rinse it all 


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2018-01-04 16:54 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
2018-01-04 15:05 ` Rusi
2018-01-04 16:54   ` Rusi

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