sed -e s/\'/\&apos\;/g doesn't seem to work

sed -e s/\'/\&apos\;/g doesn't seem to work

Post by William Hargro » Fri, 17 Aug 2001 18:29:01



Hi there,

I can't believe I can't get this to work - but:

I have a file that contains
William's
Test's

and I want to replace the apostrophe (') with '

So I thought

cat ./test |sed -e s/\'/\&apos\;/g would do the trick, but it doesn't. It just does

William'apos;s
Test'apos;s

So it seems to me that I have not escaped the replay character (&) properly.

If anyone can help with this I would appreciate it - it must be so simple.

Thanks in advance,
Will.

 
 
 

sed -e s/\'/\&apos\;/g doesn't seem to work

Post by Uwe Pros » Fri, 17 Aug 2001 19:53:21



> Hi there,

> I can't believe I can't get this to work - but:

> I have a file that contains
> William's
> Test's

> and I want to replace the apostrophe (') with '

> So I thought

> cat ./test |sed -e s/\'/\&apos\;/g would do the trick, but it doesn't. It just does

> William'apos;s
> Test'apos;s

> So it seems to me that I have not escaped the replay character (&) properly.

> If anyone can help with this I would appreciate it - it must be so simple.

You need to quote the expression given to sed to pass the \& properly to
sed. I your case you passed to sed the string "s/'/'".

Just try:
echo "William's" | sed "s/'/\'/g"
gives:
William's

Uwe

 
 
 

sed -e s/\'/\&apos\;/g doesn't seem to work

Post by t.. » Fri, 17 Aug 2001 19:48:29



> I have a file that contains
> William's
> Test's

> and I want to replace the apostrophe (') with '

> So I thought

> cat ./test |sed -e s/\'/\&apos\;/g would do the trick, but it doesn't. It just does

> William'apos;s
> Test'apos;s

> So it seems to me that I have not escaped the replay character (&) properly.

Right you are. You need to escape it twice, first from the shell,
then from sed. To make things more difficult, the \ you need to
protect it from sed must be protected from the shell, too.
So you need three backslashes:

sed s/\'/\\\&apos\;/g

It might be easier to use double quotes for the sed expression:

sed "s/'/\\&apos\;/g"

or single quotes for the end part of the expression:

sed -e s/\''/\&apos\;/g'

--
Tapani Tarvainen

 
 
 

sed -e s/\'/\&apos\;/g doesn't seem to work

Post by Walter Brisco » Fri, 17 Aug 2001 20:08:24



Aug 2001 02:29:01 in comp.unix.shell, William Hargrove

Quote:>Hi there,

>I can't believe I can't get this to work - but:

>I have a file that contains
>William's
>Test's

>and I want to replace the apostrophe (') with '

>So I thought

>cat ./test |sed -e s/\'/\&apos\;/g would do the trick, but it doesn't. It just

You seem to have forgotten to quote. You don't say what shell is being
used. You may want to look up UUOC. I suspect you have mis-transcribed
the line in putting it in your posting. For me, the unbalanced quote (')
does horrible things!

Quote:>does

>William'apos;s
>Test'apos;s

>So it seems to me that I have not escaped the replay character (&) properly.

>If anyone can help with this I would appreciate it - it must be so simple.

>Thanks in advance,
>Will.

The following was done with a Korn shell. I doubled a quote in the first
line to exercise the g flag in the sed substitute command.

? cat test                                                                      
Will'm's                                                                        
Test's                                                                          
? sed "s/'/\&apos/g" < test                                                    
Will&aposm&aposs                                                                
Test&aposs                                                                      
?            
--
Walter Briscoe

 
 
 

sed -e s/\'/\&apos\;/g doesn't seem to work

Post by Invalid&quo » Sat, 18 Aug 2001 01:50:35



Quote:> Hi there,

> I can't believe I can't get this to work - but:

> I have a file that contains
> William's
> Test's

> and I want to replace the apostrophe (') with &apos;

> So I thought

> cat ./test |sed -e s/\'/\&apos\;/g would do the trick, but it doesn't. It just does

> William'apos;s
> Test'apos;s

> So it seems to me that I have not escaped the replay character (&) properly.

> If anyone can help with this I would appreciate it - it must be so simple.

cat ./test  | sed -e "s/'/\&apos\;/g"

Lee.

 
 
 

sed -e s/\'/\&apos\;/g doesn't seem to work

Post by brian mille » Mon, 20 Aug 2001 11:10:14



Quote:> Hi there,

> I can't believe I can't get this to work - but:

> I have a file that contains
> William's
> Test's

> and I want to replace the apostrophe (') with &apos;

> So I thought

> cat ./test |sed -e s/\'/\&apos\;/g would do the trick, but it doesn't. It
just does

> William'apos;s
> Test'apos;s

> So it seems to me that I have not escaped the replay character (&)
properly.

> If anyone can help with this I would appreciate it - it must be so simple.

> Thanks in advance,
> Will.

Try double quotes.
cat ./test |sed -e "s/\'/\&apos\;/g"

Brian Miller

 
 
 

sed -e s/\'/\&apos\;/g doesn't seem to work

Post by those who know me have no need of my nam » Mon, 20 Aug 2001 12:21:29



Quote:>cat ./test |sed -e s/\'/\&apos\;/g would do the trick, but it doesn't.

ask yourself what your shell might do with the \'s.

--
okay, have a sig then

 
 
 

sed -e s/\'/\&apos\;/g doesn't seem to work

Post by brian mille » Tue, 21 Aug 2001 08:10:13



Quote:> Hi there,

> I can't believe I can't get this to work - but:

> I have a file that contains
> William's
> Test's

> and I want to replace the apostrophe (') with &apos;

> So I thought

> cat ./test |sed -e s/\'/\&apos\;/g would do the trick, but it doesn't. It
just does

> William'apos;s
> Test'apos;s

> So it seems to me that I have not escaped the replay character (&)
properly.

> If anyone can help with this I would appreciate it - it must be so simple.

> Thanks in advance,
> Will.

Try double quotes.

cat ./test |sed -e "s/\'/\&apos\;/g"

Brian Miller

 
 
 

sed -e s/\'/\&apos\;/g doesn't seem to work

Post by Chris F.A. Johnso » Tue, 21 Aug 2001 09:35:21





> > Hi there,

> > I can't believe I can't get this to work - but:

> > I have a file that contains
> > William's
> > Test's

> > and I want to replace the apostrophe (') with &apos;

> > So I thought

> > cat ./test |sed -e s/\'/\&apos\;/g would do the trick, but it doesn't. It
> just does

> > William'apos;s
> > Test'apos;s

> > So it seems to me that I have not escaped the replay character (&)
> properly.

> > If anyone can help with this I would appreciate it - it must be so simple.

> > Thanks in advance,
> > Will.

> Try double quotes.

> cat ./test |sed -e "s/\'/\&apos\;/g"

        sed "s/'/\&apos\;/g" test

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2001, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

1. sed -e 's/\'a\'/\';\'/' ?

Hi,

my question is, how can I mask the single
quotation mark in my file for sed

I have a lot of SQL-Loader files of this kind:

cat t55.ctl
load data
infile 't55'
truncate
into table A_220_T55
fields terminated by ';' TRAILING NULLCOLS
(AAA_000_KEY    char ,
B_220_BBB_LFD_NR char ,
AAA_220_BBB     char )

there is given a delimiter ';'

The delimiter should be changed to a.

#!/bin/ksh
#set -x
for file in t*.ctl
do
sed -e 's/;/a/' $file >$file.new
mv $file.new $file
done

That's ok.

Now the delimiter should be rechanged from a to ;.
I'd tried

sed -e 's/\'a\'/\';\'/' $file >$file.new

It doesn't work.

How to mask the single quotation mark inside the sed command?

TIA,

Nele

2. WEIRDNESS: name lookups only work for root!?!

3. Does sed '/'$var'/d' textfile work if $var has / in it???

4. BUG in ext2 file-system (2.0.33 kernel): tried to extend a file using lseek

5. sed and '/' to '\/' conversion.

6. RedHat 6.2 fresh install won't boot---help!

7. alpha sort: 'sed'/'awk'?

8. Does Linux supports Conner's QIC80 parallel tapebakup?

9. Why doesn't sed 's/$/^M/' work as expected?

10. slackware-elf-beta's emacs doesn't seem to be working..

11. qt2.2.1 doesn't seem to work with Nvidia's openGL library

12. POSIX threads & Linux, priorities doesn't seem to work.

13. sed '/$^/d'