Issue
I have a list of usernames like the following:
call StoredProcedure('O'[email protected]');
call StoredProcedure('[email protected]');
call StoredProcedure('[email protected]');
As you can see some names have apostrophes in them and some do not.
In Shell, I want to be able to find and escape the second occurrence of an apostrophe on a line IF there are more than 2 occurrences.
Ive managed to remove all occurrences using SED but im drawing a blank here.
Any help would be greatly appreciated.
Solution
I want to be able to find and escape the second occurrence of an apostrophe on a line IF there are more than 2 occurrences.
That's surprisingly simple:
sed "/'[^']*'[^']*'/s/'/\\\\'/2"
/'[^']*'[^']*'/
- if the line matches this regex with three'
s
- replace'
- apostrophe\\\\'
- for\'
. Note that we are inside double quotes. So first double quotes change\\\\
into\\
then sed changes\\
into\
.2
second occurrence only.
Answered By - KamilCuk Answer Checked By - Katrina (WPSolving Volunteer)