Operating System - HP-UX
1752843 Members
3475 Online
108789 Solutions
New Discussion юеВ

sed substitution question

 
Joe White
Frequent Advisor

sed substitution question

I'm trying to substitute a comma for a double quote found after two letters in each line of a text file. Sed doesn't complain but the substitution doesn't work. My substitution string is found below. Any suggestions would be appreciated.

s/"[a-zA-Z]""[a-zA-Z]""/"[a-zA-Z]""[a-zA-Z]",/
6 REPLIES 6
Matthew_50
Valued Contributor

Re: sed substitution question

for your reference, this should match your requirement, you need to use backslash to convert the special character.

root@SD /tmp/sed> sed s/\"[a-zA-Z][a-zA-Z]\"/\&\,/g input
"AA",bbccdd
AA"bB",ccdd
AABB"Cc",dd
AABBcc"dD",
AABB"cc",DD
"c"ABCDEFG
root@SD /tmp/sed> cat input
"AA"bbccdd
AA"bB"ccdd
AABB"Cc"dd
AABBcc"dD"
AABB"cc"DD
"c"ABCDEFG
root@SD /tmp/sed>
Thierry Poels_1
Honored Contributor

Re: sed substitution question

Hi,

to replace AA" by AA, :
sed 's/\([a-zA-Z][a-zA-Z]\)"/\1,/g' input

to replace "AA" by AA, :
sed 's/"\([a-zA-Z][a-zA-Z]\)"/\1,/g' input

regards,
Thierry.
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.
Ravi_8
Honored Contributor

Re: sed substitution question

Hi
use the back slashes as told by Matthew
never give up
Joe White
Frequent Advisor

Re: sed substitution question

Thanks for the responses everyone. Actually Thierry's response replacing AA" with AA, worked best for my particular situation.

Thierry, what about a condition wehere you want to replace [a-zA-Z] with ,[a-zA-Z]?
Thierry Poels_1
Honored Contributor

Re: sed substitution question

[a-zA-Z] --> ,[a-zA-Z]

sed 's/ \([a-zA-Z]\)/,\1/g' input
(= sed 's/\([a-zA-Z]\)/,\1/g')

regards,
Thierry.
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.
Joe White
Frequent Advisor

Re: sed substitution question

Thanks again Thierry. That did it.