How to replace sting by column?

How to replace sting by column?

Post by Ensoul Che » Thu, 10 Jul 2003 10:31:23



How to replace sting by column?

Such as:

COL1          COL2            COL3
AAA           XXX             AAA3
AAA           XXX             AAA2
AAA           XXX             AAA1

I just want to replace the "AAA" only in COL1 by "bbb". How can I do it?

Thanks in Advance.

--

--

ICQ: 316069549
FTP: ftp://gidc.vicp.net
--

 
 
 

How to replace sting by column?

Post by Pierre Gasto » Thu, 10 Jul 2003 14:08:28



> COL1          COL2            COL3
> AAA           XXX             AAA3
> AAA           XXX             AAA2
> AAA           XXX             AAA1

> I just want to replace the "AAA" only in COL1 by "bbb". How can I do it?

use M-x RET query-replace-regexp RET ^AAA RET bbb RET !

^i ndicates the beginning of the line

 
 
 

How to replace sting by column?

Post by Leo » Thu, 10 Jul 2003 16:17:16




> > COL1          COL2            COL3
> > AAA           XXX             AAA3
> > AAA           XXX             AAA2
> > AAA           XXX             AAA1

> > I just want to replace the "AAA" only in COL1 by "bbb". How can I do it?

> use M-x RET query-replace-regexp RET ^AAA RET bbb RET !

> ^i ndicates the beginning of the line

or more general: find out how the columns are seperated (eg. tab) and then
use

M-x RET query-replace-regexp RET ^AAA[tab] RET bbb[tab] RET !

cheers, leooo

 
 
 

How to replace sting by column?

Post by Ralf Fasse » Thu, 10 Jul 2003 17:57:05



| I just want to replace the "AAA" only in COL1 by "bbb". How can I do
| it?

In addition to the `regexp' solutions, you might want to look at the
`rectangle' commands: kill-rectangle, string-rectangle and
yank-rectangle.

R'

 
 
 

How to replace sting by column?

Post by Olivier Aumag » Thu, 10 Jul 2003 18:07:41



> How to replace sting by column?

> COL1          COL2            COL3
> AAA           XXX             AAA3
> AAA           XXX             AAA2
> AAA           XXX             AAA1

> I just want to replace the "AAA" only in COL1 by "bbb". How can I do it?

You can do that using the 'string-rectangle' function.

COL1          COL2            COL3
mAA           XXX             AAA3
AAA           XXX             AAA2
AAAp          XXX             AAA1

Just put the mark where I put the 'm' and the point where I put the
'p'. Then type: C-x r t bbb RET

See 'Rectangles' in the Emacs info manual.

--
Olivier

 
 
 

How to replace sting by column?

Post by Sergei Pokrovsk » Thu, 10 Jul 2003 18:22:39



  >> I just want to replace the "AAA" only in COL1 by "bbb". How can I
  >> do it?

  Ralf> In addition to the `regexp' solutions, you might want to look
  Ralf> at the `rectangle' commands: kill-rectangle, string-rectangle
  Ralf> and yank-rectangle.

A few years ago I've tried to write something la Multiedit; here is
an extended version by Eli Tziperman:

; ------------------------------------------------------------------------
; replace-string and query-replace-string limited to a rectangle:
; bind "ctrl-x r &" and "ctrl-x r %" to the replace in rectangle functions:
(define-key ctl-x-map "r&" 'replace-string-in-rectangle)
(define-key ctl-x-map "r%" 'query-replace-in-rectangle)
(require 'rect)
; ------------------------------------------------------------------------
(defun replace-string-in-rectangle-line (startpos ignore ignore)
; 1) make sure replacement occurs only within rectangle:
; narrow to region:
(narrow-to-region startpos (point))
; go to begining of present line:
(goto-char startpos)
; 2) replace
(while (search-forward from-string nil t)
    (replace-match to-string nil t))
(widen))

(defun replace-string-in-rectangle(from-string to-string &optional ignore)
  "Replace occurrences of FROM-STRING with TO-STRING in a rectangle"
(interactive (query-replace-read-args "Replace string" nil))
(setq start (mark))
(setq end (point))
(operate-on-rectangle 'replace-string-in-rectangle-line start end t)
)
; ------------------------------------------------------------------------
(defun query-replace-in-rectangle-line (startpos ignore ignore)
; 1) make sure replacement occurs only within rectangle:
  (setq endpos (point))
  (setq transient-mark-mode t)
  (set-mark startpos)
  (goto-char endpos)
; 2) replace
  (perform-replace from-string to-string t nil nil 1)
  (widen)
  (setq replace-count-rectangle-lines (+ replace-count-rectangle-lines 1)))

(defun query-replace-in-rectangle(from-string to-string &optional ignore)
 "Query-replace occurrences of FROM-STRING with TO-STRING in a rectangle.  
query-replace is done on one line of the rectangle at a time.  To
abort the query-replace for entire rectangle, use ESC-ESC-ESC or ^G."
(interactive (query-replace-read-args "Replace string" nil))
(setq start (mark))
(setq end (point))
(setq replace-count-rectangle 0)
(setq replace-count-rectangle-lines 0)
(operate-on-rectangle 'query-replace-in-rectangle-line start end t)
(message "Completed query-replace in a rectangle of %d line%s"
                 replace-count-rectangle-lines
                 (if (= replace-count-rectangle-lines 1) "" "s")))
; ------------------------------------------------------------------------

But it is less convenient than what I've used to use in Multiedit, and
somehow I've never used these functions for a serious task -- a regexp
solution proved to be more practical in Emacs.

--
Sergei