unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* function to increase numbers in a buffer
@ 2003-04-22 13:50 Jordi Burguet Castell
  2003-04-23  6:04 ` Harry Putnam
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Jordi Burguet Castell @ 2003-04-22 13:50 UTC (permalink / raw)


Hello,

I am trying to write a function that would work like query-replace-regexp, 
but instead of replacing each match with a fixed string, I want the matched 
numbers to be increased by 3.

Or, easier, I have a file that looks like this:

20      1       23523
20      2       23874
20      3       23898
...

and I want to increase all the numbers in the third column by a certain 
amount, say 3, from (point) to (point-max). A Perl script would not help, 
because I need to do it several times and from different points.

I started following a tutorial in emacs-lisp, and also tried looking at the 
code of some ".el" files, but it seems that I am still far from being able 
to do such a simple(?) function. This post is in case some charitable 
emacs-lisp expert soul knows how to do it.

Thanks,
Jordi

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

* Re: function to increase numbers in a buffer
  2003-04-22 13:50 function to increase numbers in a buffer Jordi Burguet Castell
@ 2003-04-23  6:04 ` Harry Putnam
  2003-04-23  6:50 ` Joakim Hove
  2003-04-23 12:01 ` Stefan Reichör
  2 siblings, 0 replies; 4+ messages in thread
From: Harry Putnam @ 2003-04-23  6:04 UTC (permalink / raw)


Jordi Burguet Castell <jordi.burguet-castell@cern.ch> writes:

> Or, easier, I have a file that looks like this:
>
> 20      1       23523
> 20      2       23874
> 20      3       23898
> ...
>
> and I want to increase all the numbers in the third column by a certain 
> amount, say 3, from (point) to (point-max). A Perl script would not help, 
> because I need to do it several times and from different points.

Not a very satisfying answer I'm afraid but shell tools or perl can
handle this thru an emacs command `shell-command-on-region' (`M-S-|'),
by selecting the region needed and then use the `C-u' preface to make
the action change the current buffer.

For example, taking this buffer

  22   1  2233
  22   2  2244
  22   3  3355
  22   4  4455

Select a region of lines 2 thru 3 in the usual way by pressing C-spc
at the beginning of line 2 then moving cursor just below line 3.


Then C-u M-S-| awk '{printf "%-4d %-2d %d\n",  $1, $2, ($3 + 3)}' <RET>

Will give you 

  22   1  2233
  22   2  2247
  22   3  3358
  22   4  4455

Emacs allows one to work on a region employing shell or other tools to do
the work.

There are probably better ways, but this one is fairly quick if you
know basic shell/awk/perl stuff and are on a platform where those
tools can be found.

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

* Re: function to increase numbers in a buffer
  2003-04-22 13:50 function to increase numbers in a buffer Jordi Burguet Castell
  2003-04-23  6:04 ` Harry Putnam
@ 2003-04-23  6:50 ` Joakim Hove
  2003-04-23 12:01 ` Stefan Reichör
  2 siblings, 0 replies; 4+ messages in thread
From: Joakim Hove @ 2003-04-23  6:50 UTC (permalink / raw)



Jordi Burguet Castell <jordi.burguet-castell@cern.ch> writes:

> Hello,
>
> I am trying to write a function that would work like query-replace-regexp, 
> but instead of replacing each match with a fixed string, I want the matched 
> numbers to be increased by 3.

Try this small function:


(defun addnumbers-in-region (p1 p2)
  (interactive "r")
  (save-excursion
    (goto-char p1)
    (let ((delta (string-to-number (read-from-minibuffer "Increase with amount: "))))
      (while (re-search-forward "\\([0-9]+\\)" p2 't)
	(let* ((org-number (string-to-number (match-string 1)))
	       (new-number (+ delta org-number)))
	  (replace-match (format "%s" new-number)))))))
	  

> Or, easier, I have a file that looks like this:
>
> 20      1       23523
> 20      2       23874
> 20      3       23898

(defun add3-to-column3 (p1 p2)
  (interactive "r")
  (save-excursion
    (goto-char p1)
    (while (<= (point) p2)
      (beginning-of-line)
      (re-search-forward "^[ ]*[0-9]+[ ]+[0-9]+[ ]+\\([0-9]+\\)[ ]*$")
      (let ((new-number (+ 3 (string-to-number (match-string 1)))))
	(backward-kill-word 1)
	(insert (format "%4d" new-number)))
      (next-line 1))))

Good - Luck 

Joakim

-- 
Joakim Hove  / hove@ii.uib.no  /  (55 5) 84076

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

* Re: function to increase numbers in a buffer
  2003-04-22 13:50 function to increase numbers in a buffer Jordi Burguet Castell
  2003-04-23  6:04 ` Harry Putnam
  2003-04-23  6:50 ` Joakim Hove
@ 2003-04-23 12:01 ` Stefan Reichör
  2 siblings, 0 replies; 4+ messages in thread
From: Stefan Reichör @ 2003-04-23 12:01 UTC (permalink / raw)


On Tue, 22 Apr 2003, Jordi Burguet Castell uttered the following:

>  Hello,
>  
>  I am trying to write a function that would work like
>  query-replace-regexp, but instead of replacing each match with a
>  fixed string, I want the matched numbers to be increased by 3.
>  
>  Or, easier, I have a file that looks like this:
>  
>  20      1       23523
>  20      2       23874
>  20      3       23898
>  ...
>  

Use cua-mode:
 Hit Shift-Enter
 Mark the numbers with the cursor keys
 Type: M-3 M-i ... Increment the marked numbers by 3


Stefan.

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

end of thread, other threads:[~2003-04-23 12:01 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-04-22 13:50 function to increase numbers in a buffer Jordi Burguet Castell
2003-04-23  6:04 ` Harry Putnam
2003-04-23  6:50 ` Joakim Hove
2003-04-23 12:01 ` Stefan Reichör

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