* Add +2 to all numbers in file
@ 2009-02-27 8:18 sharky
2009-02-27 8:32 ` Katsumi Yamaoka
2009-02-27 17:58 ` Pascal J. Bourguignon
0 siblings, 2 replies; 5+ messages in thread
From: sharky @ 2009-02-27 8:18 UTC (permalink / raw)
To: help-gnu-emacs
Hi!
I have a file with a lot of numbers. It looks like this:
xx, xx, xx--xx, xx, xx, x
xx, x, xx--xx, xx--xx, x
xx, xx
xx, xx, xx--xx, xx--xx, xx--xx
You get the picture. Can I make emacs add 2 to each and everyone of
these numbers?
Please help!
/Sharky
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Add +2 to all numbers in file
2009-02-27 8:18 Add +2 to all numbers in file sharky
@ 2009-02-27 8:32 ` Katsumi Yamaoka
2009-02-27 9:11 ` sharky
2009-02-27 17:58 ` Pascal J. Bourguignon
1 sibling, 1 reply; 5+ messages in thread
From: Katsumi Yamaoka @ 2009-02-27 8:32 UTC (permalink / raw)
To: help-gnu-emacs
>>>>> sharky wrote:
> I have a file with a lot of numbers. It looks like this:
> xx, xx, xx--xx, xx, xx, x
> xx, x, xx--xx, xx--xx, x
> xx, xx
> xx, xx, xx--xx, xx--xx, xx--xx
> You get the picture. Can I make emacs add 2 to each and everyone of
> these numbers?
> Please help!
Try `M-x all+2 RET':
(defun all+2 ()
(interactive "*")
(save-excursion
(goto-char (point-min))
(while (re-search-forward "[0-9]+" nil t)
(replace-match (number-to-string
(+ 2 (string-to-number (match-string 0))))))))
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Add +2 to all numbers in file
2009-02-27 8:32 ` Katsumi Yamaoka
@ 2009-02-27 9:11 ` sharky
0 siblings, 0 replies; 5+ messages in thread
From: sharky @ 2009-02-27 9:11 UTC (permalink / raw)
To: help-gnu-emacs
On Feb 27, 9:32 am, Katsumi Yamaoka <yama...@jpl.org> wrote:
> >>>>> sharky wrote:
> > I have a file with a lot of numbers. It looks like this:
> > xx, xx, xx--xx, xx, xx, x
> > xx, x, xx--xx, xx--xx, x
> > xx, xx
> > xx, xx, xx--xx, xx--xx, xx--xx
> > You get the picture. Can I make emacs add 2 to each and everyone of
> > these numbers?
> > Please help!
>
> Try `M-x all+2 RET':
>
> (defun all+2 ()
> (interactive "*")
> (save-excursion
> (goto-char (point-min))
> (while (re-search-forward "[0-9]+" nil t)
> (replace-match (number-to-string
> (+ 2 (string-to-number (match-string 0))))))))
I love you. It works perfectly.
Thanks for saving me from several hours of work!
/Sharky
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Add +2 to all numbers in file
2009-02-27 8:18 Add +2 to all numbers in file sharky
2009-02-27 8:32 ` Katsumi Yamaoka
@ 2009-02-27 17:58 ` Pascal J. Bourguignon
2009-02-27 21:55 ` David Kastrup
1 sibling, 1 reply; 5+ messages in thread
From: Pascal J. Bourguignon @ 2009-02-27 17:58 UTC (permalink / raw)
To: help-gnu-emacs
sharky <landhaj@yahoo.com> writes:
> Hi!
>
> I have a file with a lot of numbers. It looks like this:
>
> xx, xx, xx--xx, xx, xx, x
> xx, x, xx--xx, xx--xx, x
> xx, xx
> xx, xx, xx--xx, xx--xx, xx--xx
>
> You get the picture. Can I make emacs add 2 to each and everyone of
> these numbers?
>
> Please help!
If you have only one file, you can edit it interactively. With recent
emacs (eg. 22, perhaps even 21), you can substitute an expression.
Type:
M-x replace-regexp RET \([0-9][0-9]*\) RET \,(+ 2 (car (read-from-string \1))) RET
--
__Pascal Bourguignon__
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Add +2 to all numbers in file
2009-02-27 17:58 ` Pascal J. Bourguignon
@ 2009-02-27 21:55 ` David Kastrup
0 siblings, 0 replies; 5+ messages in thread
From: David Kastrup @ 2009-02-27 21:55 UTC (permalink / raw)
To: help-gnu-emacs
pjb@informatimago.com (Pascal J. Bourguignon) writes:
> sharky <landhaj@yahoo.com> writes:
>
>> Hi!
>>
>> I have a file with a lot of numbers. It looks like this:
>>
>> xx, xx, xx--xx, xx, xx, x
>> xx, x, xx--xx, xx--xx, x
>> xx, xx
>> xx, xx, xx--xx, xx--xx, xx--xx
>>
>> You get the picture. Can I make emacs add 2 to each and everyone of
>> these numbers?
>>
>> Please help!
>
> If you have only one file, you can edit it interactively. With recent
> emacs (eg. 22, perhaps even 21), you can substitute an expression.
>
> Type:
>
> M-x replace-regexp RET \([0-9][0-9]*\) RET \,(+ 2 (car (read-from-string \1))) RET
A bit complicated.
C-M-% [0-9]+ RET \,(+ 2 \#&) RET
--
David Kastrup
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2009-02-27 21:55 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-02-27 8:18 Add +2 to all numbers in file sharky
2009-02-27 8:32 ` Katsumi Yamaoka
2009-02-27 9:11 ` sharky
2009-02-27 17:58 ` Pascal J. Bourguignon
2009-02-27 21:55 ` David Kastrup
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).