David Loh
David Loh

Reputation: 11

How to remove single quote in a single quoted string?

I need to remove a single quote in a single quoted email address with Sed.

I have tried matching with

    ,'.*(?<=@)

it is always including ,'def',' in the regex.

For example

    aabc,123,'def','me.o'[email protected]',123,abc

to

    aabc,123,'def','[email protected]',123,abc

How can I do the subsitution without touching the rest of the single quotes ?

Thanks

Upvotes: 1

Views: 103

Answers (1)

Dario
Dario

Reputation: 2713

You have not specified enough information about the constraints on the format of the input string. The following sed command, however, works with the example string you provided and many similar ones. (To be precise: it will work with any lines where there is only one character to remove, which can appear at any position in the name part of the email address, and the single-quote-delimited email address is itself neither the first nor the last comma-separated field. If there are more then two email addresses to correct, it will operate on the first.)

It should give you an idea. The key is anchoring your match between ,' and ',.

sed -e "s/\(,'[^']*\)'\([^']*@[^']*',\)/\1\2/" 

Upvotes: 2

Related Questions