[comp.unix.questions] Sed to make global replacements.

gahooten@orion.arc.nasa.gov (Gregory A. Hooten) (12/02/89)

I am working with sed on a file, and need to make a global replacement, but 
what I am replacing are special characters to sed.  I do not know how to 
replace them.  

I would like to change the characters \(12 to 1/2, but every thing I try
thinks the \( is the start of a character group.  I would like any help 
possible on this problem.  

Thanks.

Greg Hooten
GAHOOTEN@ames.arc.nasa.gov

ssdbruce@jarthur.Claremont.EDU (Bruce Crabtree) (12/02/89)

In article <37090@ames.arc.nasa.gov> gahooten@orion.arc.nasa.gov (Gregory A. Hooten) writes:
>I would like to change the characters \(12 to 1/2, but every thing I try
>thinks the \( is the start of a character group.

Try
                sed 's/[\\](12/1\/2/g'

By bracketing the backslash characters you are preventing sed from seeing
the '\(' combination.  Two backslashes are used since the backslash has
special meaning to sed and must be escaped (with another backslash).  The
slash (or virgule, if you will) that is used to separate the '1/2' must
also be escaped to prevent sed from using it as the delimiter of the 
substitution command.  I added the 'g' or global option since I assume
you might have multiple occurrances of \(12 on a line.

Bruce Crabtree

-- 
--------------------------------------------------------------------
Bruce Crabtree			      ssdbruce@jarthur.claremont.edu
Software Systems Design, Inc          [W] 1-714-624-2306
Claremont, CA                         [H] 1-714-738-6026

ssdbruce@jarthur.Claremont.EDU (Bruce Crabtree) (12/02/89)

In article <37090@ames.arc.nasa.gov> gahooten@orion.arc.nasa.gov (Gregory A. Hooten) writes:
>I would like to change the characters \(12 to 1/2, but every thing I try
>thinks the \( is the start of a character group.

Try
           sed 's/[\\](12/1\/2/g'

The brackets ('[]') are used to prevent sed from seeing the '\('
character combination and interpreting them as such.  Two backslashes
are used since the backslash must be escaped in sed.  The slash or
virgule in the '1/2' must also be escaped since sed would take it to
be the delimiter of the substitute command.  I've added the 'g' or
global option since I assume you might have more than one instance
of \(12 per line.

Bruce Crabtree

-- 
--------------------------------------------------------------------
Bruce Crabtree			      ssdbruce@jarthur.claremont.edu
Software Systems Design, Inc          [W] 1-714-624-2306
Claremont, CA                         [H] 1-714-738-6026

cpcahil@virtech.uucp (Conor P. Cahill) (12/02/89)

In article <37090@ames.arc.nasa.gov>, gahooten@orion.arc.nasa.gov (Gregory A. Hooten) writes:
> I would like to change the characters \(12 to 1/2, but every thing I try
> thinks the \( is the start of a character group.  I would like any help 
> possible on this problem.  

How about:	sed -e 's;\\(12;1/2;'

Works for me.


-- 
+-----------------------------------------------------------------------+
| Conor P. Cahill     uunet!virtech!cpcahil      	703-430-9247	!
| Virtual Technologies Inc.,    P. O. Box 876,   Sterling, VA 22170     |
+-----------------------------------------------------------------------+

merlyn@iwarp.intel.com (Randal Schwartz) (12/03/89)

In article <37090@ames.arc.nasa.gov>, gahooten@orion (Gregory A. Hooten) writes:
| I am working with sed on a file, and need to make a global replacement, but 
| what I am replacing are special characters to sed.  I do not know how to 
| replace them.  
| 
| I would like to change the characters \(12 to 1/2, but every thing I try
| thinks the \( is the start of a character group.  I would like any help 
| possible on this problem.  

In sed, you need to escape the backslash, as in:

% sed 's,\\(12,1/2,g' <old >new

Now, with Perl (of course, you knew it was coming :-), if you had a
whole directory full of these files, you could perform this
substitution, saving the original files as filename.bak, with:

% perl -p -i.bak -e 's,\\(12,1/2,g;' *

Cool, eh?  No fuss, no muss, and all in one process.

Just another Perl hacker, waiting for comp.lang.perl...
-- 
/== Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ====\
| on contract to Intel's iWarp project, Hillsboro, Oregon, USA, Sol III  |
| merlyn@iwarp.intel.com ...!uunet!iwarp.intel.com!merlyn	         |
\== Cute Quote: "Welcome to Oregon... Home of the California Raisins!" ==/

cpcahil@virtech.uucp (Conor P. Cahill) (12/03/89)

In article <5306@omepd.UUCP>, merlyn@iwarp.intel.com (Randal Schwartz) writes:
> In article <37090@ames.arc.nasa.gov>, gahooten@orion (Gregory A. Hooten) writes:
> | I would like to change the characters \(12 to 1/2, but every thing I try
> | thinks the \( is the start of a character group.  I would like any help 
> | possible on this problem.  
> 
> Now, with Perl (of course, you knew it was coming :-), if you had a
> whole directory full of these files, you could perform this
> substitution, saving the original files as filename.bak, with:
> 
> % perl -p -i.bak -e 's,\\(12,1/2,g;' *

First, you forgot an extra \ for the (.
Second, this silently deletes the original files if the filename is
too long on a system V system.


-- 
+-----------------------------------------------------------------------+
| Conor P. Cahill     uunet!virtech!cpcahil      	703-430-9247	!
| Virtual Technologies Inc.,    P. O. Box 876,   Sterling, VA 22170     |
+-----------------------------------------------------------------------+

merlyn@iwarp.intel.com (Randal Schwartz) (12/04/89)

In article <1989Dec3.141336.27475@virtech.uucp>, cpcahil@virtech (Conor P. Cahill) writes:
| > Now, with Perl (of course, you knew it was coming :-), if you had a
| > whole directory full of these files, you could perform this
| > substitution, saving the original files as filename.bak, with:
| > 
| > % perl -p -i.bak -e 's,\\(12,1/2,g;' *
| 
| First, you forgot an extra \ for the (.
| Second, this silently deletes the original files if the filename is
| too long on a system V system.

I won't argue about System V.  No point.  It's brain-damaged.

However, shame on me for forgetting that Perl makes left-paren a magic
char!

Larry, can you ever forgive me?  Speak to me Larry!  We got another
200 pages to write for the book!  Larry!  Larry???!!

(I guess Larry is going to have to proofread my code pretty thoroughly
from now on...)

Just a (former) Perl hacker, :-)
-- 
/== Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ====\
| on contract to Intel's iWarp project, Hillsboro, Oregon, USA, Sol III  |
| merlyn@iwarp.intel.com ...!uunet!iwarp.intel.com!merlyn	         |
\== Cute Quote: "Welcome to Oregon... Home of the California Raisins!" ==/

guy@auspex.auspex.com (Guy Harris) (12/09/89)

>Try
>                sed 's/[\\](12/1\/2/g'

or just

                 sed 's/\\(12/1\/2/g'

since, as you note:

>Two backslashes are used since the backslash has special meaning to sed
>and must be escaped (with another backslash).

so escaping the backslash with another one is sufficient.  (Yes, I tried
it.)  In addition

>The slash (or virgule, if you will) that is used to separate the '1/2' must
>also be escaped to prevent sed from using it as the delimiter of the 
>substitution command.

Or you can do

                 sed 's;\\(12;1/2;g'

using ";" as the delimiter, or any other character.