Sed replace word except when word is preceded by a specific string












3














I want to replace all instance of abc with 123 provided it isn't preceded by https://.



Code:



file=Data.txt
Initial="# Start";
orig="abc";
new="123";

Final="$" # Line Number
sed -r -e "/${Initial}/,${Final}s/${orig}/${new}/g" ${file} # Final doesn't accept $


Data:



# Start

abc.md
https://abc.md
The path is https://abc.md for abc.md
The path for abc.md is https://abc.md


Expected Output:



# Start

123.md
https://abc.md
The path is https://abc.md for 123.md
The path for 123.md is https://abc.md


How can I achieve this?





Note: I also need to use ${Initial} and ${Final} to denote between which the pattern might exist.



This answer doesn't work for the case The path is: https://abc.md abc.md










share|improve this question
























  • Would you be happy with a solution that uses line numbers (not regular expressions for addressing the relevant bit of the input)?
    – Kusalananda
    Dec 13 at 12:13










  • @Kusalananda No, because the line numbers are nof fixed.
    – Nikhil
    Dec 13 at 12:15












  • Is the substitution always happening from the matching of some pattern and to the end of the file?
    – Kusalananda
    Dec 13 at 12:18












  • @Kusalananda Yes the substitution happens between ${Iniital} and to end of the file.
    – Nikhil
    Dec 13 at 12:19
















3














I want to replace all instance of abc with 123 provided it isn't preceded by https://.



Code:



file=Data.txt
Initial="# Start";
orig="abc";
new="123";

Final="$" # Line Number
sed -r -e "/${Initial}/,${Final}s/${orig}/${new}/g" ${file} # Final doesn't accept $


Data:



# Start

abc.md
https://abc.md
The path is https://abc.md for abc.md
The path for abc.md is https://abc.md


Expected Output:



# Start

123.md
https://abc.md
The path is https://abc.md for 123.md
The path for 123.md is https://abc.md


How can I achieve this?





Note: I also need to use ${Initial} and ${Final} to denote between which the pattern might exist.



This answer doesn't work for the case The path is: https://abc.md abc.md










share|improve this question
























  • Would you be happy with a solution that uses line numbers (not regular expressions for addressing the relevant bit of the input)?
    – Kusalananda
    Dec 13 at 12:13










  • @Kusalananda No, because the line numbers are nof fixed.
    – Nikhil
    Dec 13 at 12:15












  • Is the substitution always happening from the matching of some pattern and to the end of the file?
    – Kusalananda
    Dec 13 at 12:18












  • @Kusalananda Yes the substitution happens between ${Iniital} and to end of the file.
    – Nikhil
    Dec 13 at 12:19














3












3








3







I want to replace all instance of abc with 123 provided it isn't preceded by https://.



Code:



file=Data.txt
Initial="# Start";
orig="abc";
new="123";

Final="$" # Line Number
sed -r -e "/${Initial}/,${Final}s/${orig}/${new}/g" ${file} # Final doesn't accept $


Data:



# Start

abc.md
https://abc.md
The path is https://abc.md for abc.md
The path for abc.md is https://abc.md


Expected Output:



# Start

123.md
https://abc.md
The path is https://abc.md for 123.md
The path for 123.md is https://abc.md


How can I achieve this?





Note: I also need to use ${Initial} and ${Final} to denote between which the pattern might exist.



This answer doesn't work for the case The path is: https://abc.md abc.md










share|improve this question















I want to replace all instance of abc with 123 provided it isn't preceded by https://.



Code:



file=Data.txt
Initial="# Start";
orig="abc";
new="123";

Final="$" # Line Number
sed -r -e "/${Initial}/,${Final}s/${orig}/${new}/g" ${file} # Final doesn't accept $


Data:



# Start

abc.md
https://abc.md
The path is https://abc.md for abc.md
The path for abc.md is https://abc.md


Expected Output:



# Start

123.md
https://abc.md
The path is https://abc.md for 123.md
The path for 123.md is https://abc.md


How can I achieve this?





Note: I also need to use ${Initial} and ${Final} to denote between which the pattern might exist.



This answer doesn't work for the case The path is: https://abc.md abc.md







text-processing sed






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 13 at 11:34

























asked Dec 13 at 9:55









Nikhil

24319




24319












  • Would you be happy with a solution that uses line numbers (not regular expressions for addressing the relevant bit of the input)?
    – Kusalananda
    Dec 13 at 12:13










  • @Kusalananda No, because the line numbers are nof fixed.
    – Nikhil
    Dec 13 at 12:15












  • Is the substitution always happening from the matching of some pattern and to the end of the file?
    – Kusalananda
    Dec 13 at 12:18












  • @Kusalananda Yes the substitution happens between ${Iniital} and to end of the file.
    – Nikhil
    Dec 13 at 12:19


















  • Would you be happy with a solution that uses line numbers (not regular expressions for addressing the relevant bit of the input)?
    – Kusalananda
    Dec 13 at 12:13










  • @Kusalananda No, because the line numbers are nof fixed.
    – Nikhil
    Dec 13 at 12:15












  • Is the substitution always happening from the matching of some pattern and to the end of the file?
    – Kusalananda
    Dec 13 at 12:18












  • @Kusalananda Yes the substitution happens between ${Iniital} and to end of the file.
    – Nikhil
    Dec 13 at 12:19
















Would you be happy with a solution that uses line numbers (not regular expressions for addressing the relevant bit of the input)?
– Kusalananda
Dec 13 at 12:13




Would you be happy with a solution that uses line numbers (not regular expressions for addressing the relevant bit of the input)?
– Kusalananda
Dec 13 at 12:13












@Kusalananda No, because the line numbers are nof fixed.
– Nikhil
Dec 13 at 12:15






@Kusalananda No, because the line numbers are nof fixed.
– Nikhil
Dec 13 at 12:15














Is the substitution always happening from the matching of some pattern and to the end of the file?
– Kusalananda
Dec 13 at 12:18






Is the substitution always happening from the matching of some pattern and to the end of the file?
– Kusalananda
Dec 13 at 12:18














@Kusalananda Yes the substitution happens between ${Iniital} and to end of the file.
– Nikhil
Dec 13 at 12:19




@Kusalananda Yes the substitution happens between ${Iniital} and to end of the file.
– Nikhil
Dec 13 at 12:19










2 Answers
2






active

oldest

votes


















4














What you are looking for is negative-look-behind, which neither sed or awk supports. I recommend going with perl, e.g.:



file=Data.txt
export Initial="# Start"
export orig="abc"
export new="123"
export Final="5"

perl -pe '
$flag=1 if /$ENV{Initial}/;
s,(?<!https://)$ENV{orig},$ENV{new},g if $flag;
$flag=0 if $. == $ENV{Final};
' $file





share|improve this answer





















  • Does not work with The path is https://abc.md for abc.md as per the OP's updated requirement. I updated my answer, and I think you can update yours ;-)
    – sudodus
    Dec 13 at 18:41












  • @sudodus: It does if it occurs before the Final line, line number 5 in the above example
    – Thor
    Dec 13 at 18:43










  • You are right about that. (A bit difficult to see for a perl illiterate, but it works) :-)
    – sudodus
    Dec 13 at 18:47










  • @Thor How to make Final as the last line? In sed we use $.
    – Nikhil
    Dec 13 at 23:11






  • 1




    @Nikhil: You can also set Final to 0, which will never be true
    – Thor
    Dec 14 at 6:34



















2














You could temporarily change all instances that start with https://to something else, not containing abc, and change them back when you've finished.



Lazy method



sed -e 's_https://abc_protected_g;/# Start/,$s_abc_123_g;s_protected_https://abc_'


You'll have to pick some value for the temporary string, that you can be certain won't appear in your input (and doesn't contain any special characters that would interfere with sed or shell quoting). I used protected but something more distinctive is advisable.



Thorough method



If you can't be certain that your temporary string won't occur in the input, a longer but safer alternative would be to use two replacement strings:



sed -e 's/X/Xv/g;s_https://abc_Xu_g;/^# Start/,$s/abc/123/g;s_Xu_https://abc_g;s/Xv/X/g'


The requirements of these two temporary strings are:




  • They have a common prefix (I used X, but it can be longer/more readable)

  • They don't occur within the other variables: orig, new, Initial or Final

  • As before, they don't contain characters that would break the sed expression






share|improve this answer























  • It is interesting to see PCRE features implemented in a pure BRE way, in a single sed invocation. I am wondering whether other PCRE features have their equivalents as well.
    – Weijun Zhou
    Dec 14 at 2:10












  • @WeijunZhou As noted elsewhere this does not reimplement negative lookaheads, only the subproblem of negative lookaheads with a static string. Some Perl / PCRE extensions are purely convenience / syntactic sugar which is straightforward (but often cumbersome or boring) to replace with traditional regex, but e.g. lookarounds genuinely extend the formalism to something which is no longer theoretically equivalent (reducible) to regular expressions.
    – tripleee
    Dec 14 at 6:13












  • There is no contradiction. A PCRE not being reducible to regular expression is one thing, and I agree on that. Using sed to modify the text to assist in pattern matching is another thing, which is no longer simple pattern patching and hence does not contradict. To put it more clearly, there is still no way to use grep to match lookaround patterns with BRE or ERE.
    – Weijun Zhou
    Dec 14 at 7:40













Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f487725%2fsed-replace-word-except-when-word-is-preceded-by-a-specific-string%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









4














What you are looking for is negative-look-behind, which neither sed or awk supports. I recommend going with perl, e.g.:



file=Data.txt
export Initial="# Start"
export orig="abc"
export new="123"
export Final="5"

perl -pe '
$flag=1 if /$ENV{Initial}/;
s,(?<!https://)$ENV{orig},$ENV{new},g if $flag;
$flag=0 if $. == $ENV{Final};
' $file





share|improve this answer





















  • Does not work with The path is https://abc.md for abc.md as per the OP's updated requirement. I updated my answer, and I think you can update yours ;-)
    – sudodus
    Dec 13 at 18:41












  • @sudodus: It does if it occurs before the Final line, line number 5 in the above example
    – Thor
    Dec 13 at 18:43










  • You are right about that. (A bit difficult to see for a perl illiterate, but it works) :-)
    – sudodus
    Dec 13 at 18:47










  • @Thor How to make Final as the last line? In sed we use $.
    – Nikhil
    Dec 13 at 23:11






  • 1




    @Nikhil: You can also set Final to 0, which will never be true
    – Thor
    Dec 14 at 6:34
















4














What you are looking for is negative-look-behind, which neither sed or awk supports. I recommend going with perl, e.g.:



file=Data.txt
export Initial="# Start"
export orig="abc"
export new="123"
export Final="5"

perl -pe '
$flag=1 if /$ENV{Initial}/;
s,(?<!https://)$ENV{orig},$ENV{new},g if $flag;
$flag=0 if $. == $ENV{Final};
' $file





share|improve this answer





















  • Does not work with The path is https://abc.md for abc.md as per the OP's updated requirement. I updated my answer, and I think you can update yours ;-)
    – sudodus
    Dec 13 at 18:41












  • @sudodus: It does if it occurs before the Final line, line number 5 in the above example
    – Thor
    Dec 13 at 18:43










  • You are right about that. (A bit difficult to see for a perl illiterate, but it works) :-)
    – sudodus
    Dec 13 at 18:47










  • @Thor How to make Final as the last line? In sed we use $.
    – Nikhil
    Dec 13 at 23:11






  • 1




    @Nikhil: You can also set Final to 0, which will never be true
    – Thor
    Dec 14 at 6:34














4












4








4






What you are looking for is negative-look-behind, which neither sed or awk supports. I recommend going with perl, e.g.:



file=Data.txt
export Initial="# Start"
export orig="abc"
export new="123"
export Final="5"

perl -pe '
$flag=1 if /$ENV{Initial}/;
s,(?<!https://)$ENV{orig},$ENV{new},g if $flag;
$flag=0 if $. == $ENV{Final};
' $file





share|improve this answer












What you are looking for is negative-look-behind, which neither sed or awk supports. I recommend going with perl, e.g.:



file=Data.txt
export Initial="# Start"
export orig="abc"
export new="123"
export Final="5"

perl -pe '
$flag=1 if /$ENV{Initial}/;
s,(?<!https://)$ENV{orig},$ENV{new},g if $flag;
$flag=0 if $. == $ENV{Final};
' $file






share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 13 at 14:03









Thor

11.6k13358




11.6k13358












  • Does not work with The path is https://abc.md for abc.md as per the OP's updated requirement. I updated my answer, and I think you can update yours ;-)
    – sudodus
    Dec 13 at 18:41












  • @sudodus: It does if it occurs before the Final line, line number 5 in the above example
    – Thor
    Dec 13 at 18:43










  • You are right about that. (A bit difficult to see for a perl illiterate, but it works) :-)
    – sudodus
    Dec 13 at 18:47










  • @Thor How to make Final as the last line? In sed we use $.
    – Nikhil
    Dec 13 at 23:11






  • 1




    @Nikhil: You can also set Final to 0, which will never be true
    – Thor
    Dec 14 at 6:34


















  • Does not work with The path is https://abc.md for abc.md as per the OP's updated requirement. I updated my answer, and I think you can update yours ;-)
    – sudodus
    Dec 13 at 18:41












  • @sudodus: It does if it occurs before the Final line, line number 5 in the above example
    – Thor
    Dec 13 at 18:43










  • You are right about that. (A bit difficult to see for a perl illiterate, but it works) :-)
    – sudodus
    Dec 13 at 18:47










  • @Thor How to make Final as the last line? In sed we use $.
    – Nikhil
    Dec 13 at 23:11






  • 1




    @Nikhil: You can also set Final to 0, which will never be true
    – Thor
    Dec 14 at 6:34
















Does not work with The path is https://abc.md for abc.md as per the OP's updated requirement. I updated my answer, and I think you can update yours ;-)
– sudodus
Dec 13 at 18:41






Does not work with The path is https://abc.md for abc.md as per the OP's updated requirement. I updated my answer, and I think you can update yours ;-)
– sudodus
Dec 13 at 18:41














@sudodus: It does if it occurs before the Final line, line number 5 in the above example
– Thor
Dec 13 at 18:43




@sudodus: It does if it occurs before the Final line, line number 5 in the above example
– Thor
Dec 13 at 18:43












You are right about that. (A bit difficult to see for a perl illiterate, but it works) :-)
– sudodus
Dec 13 at 18:47




You are right about that. (A bit difficult to see for a perl illiterate, but it works) :-)
– sudodus
Dec 13 at 18:47












@Thor How to make Final as the last line? In sed we use $.
– Nikhil
Dec 13 at 23:11




@Thor How to make Final as the last line? In sed we use $.
– Nikhil
Dec 13 at 23:11




1




1




@Nikhil: You can also set Final to 0, which will never be true
– Thor
Dec 14 at 6:34




@Nikhil: You can also set Final to 0, which will never be true
– Thor
Dec 14 at 6:34













2














You could temporarily change all instances that start with https://to something else, not containing abc, and change them back when you've finished.



Lazy method



sed -e 's_https://abc_protected_g;/# Start/,$s_abc_123_g;s_protected_https://abc_'


You'll have to pick some value for the temporary string, that you can be certain won't appear in your input (and doesn't contain any special characters that would interfere with sed or shell quoting). I used protected but something more distinctive is advisable.



Thorough method



If you can't be certain that your temporary string won't occur in the input, a longer but safer alternative would be to use two replacement strings:



sed -e 's/X/Xv/g;s_https://abc_Xu_g;/^# Start/,$s/abc/123/g;s_Xu_https://abc_g;s/Xv/X/g'


The requirements of these two temporary strings are:




  • They have a common prefix (I used X, but it can be longer/more readable)

  • They don't occur within the other variables: orig, new, Initial or Final

  • As before, they don't contain characters that would break the sed expression






share|improve this answer























  • It is interesting to see PCRE features implemented in a pure BRE way, in a single sed invocation. I am wondering whether other PCRE features have their equivalents as well.
    – Weijun Zhou
    Dec 14 at 2:10












  • @WeijunZhou As noted elsewhere this does not reimplement negative lookaheads, only the subproblem of negative lookaheads with a static string. Some Perl / PCRE extensions are purely convenience / syntactic sugar which is straightforward (but often cumbersome or boring) to replace with traditional regex, but e.g. lookarounds genuinely extend the formalism to something which is no longer theoretically equivalent (reducible) to regular expressions.
    – tripleee
    Dec 14 at 6:13












  • There is no contradiction. A PCRE not being reducible to regular expression is one thing, and I agree on that. Using sed to modify the text to assist in pattern matching is another thing, which is no longer simple pattern patching and hence does not contradict. To put it more clearly, there is still no way to use grep to match lookaround patterns with BRE or ERE.
    – Weijun Zhou
    Dec 14 at 7:40


















2














You could temporarily change all instances that start with https://to something else, not containing abc, and change them back when you've finished.



Lazy method



sed -e 's_https://abc_protected_g;/# Start/,$s_abc_123_g;s_protected_https://abc_'


You'll have to pick some value for the temporary string, that you can be certain won't appear in your input (and doesn't contain any special characters that would interfere with sed or shell quoting). I used protected but something more distinctive is advisable.



Thorough method



If you can't be certain that your temporary string won't occur in the input, a longer but safer alternative would be to use two replacement strings:



sed -e 's/X/Xv/g;s_https://abc_Xu_g;/^# Start/,$s/abc/123/g;s_Xu_https://abc_g;s/Xv/X/g'


The requirements of these two temporary strings are:




  • They have a common prefix (I used X, but it can be longer/more readable)

  • They don't occur within the other variables: orig, new, Initial or Final

  • As before, they don't contain characters that would break the sed expression






share|improve this answer























  • It is interesting to see PCRE features implemented in a pure BRE way, in a single sed invocation. I am wondering whether other PCRE features have their equivalents as well.
    – Weijun Zhou
    Dec 14 at 2:10












  • @WeijunZhou As noted elsewhere this does not reimplement negative lookaheads, only the subproblem of negative lookaheads with a static string. Some Perl / PCRE extensions are purely convenience / syntactic sugar which is straightforward (but often cumbersome or boring) to replace with traditional regex, but e.g. lookarounds genuinely extend the formalism to something which is no longer theoretically equivalent (reducible) to regular expressions.
    – tripleee
    Dec 14 at 6:13












  • There is no contradiction. A PCRE not being reducible to regular expression is one thing, and I agree on that. Using sed to modify the text to assist in pattern matching is another thing, which is no longer simple pattern patching and hence does not contradict. To put it more clearly, there is still no way to use grep to match lookaround patterns with BRE or ERE.
    – Weijun Zhou
    Dec 14 at 7:40
















2












2








2






You could temporarily change all instances that start with https://to something else, not containing abc, and change them back when you've finished.



Lazy method



sed -e 's_https://abc_protected_g;/# Start/,$s_abc_123_g;s_protected_https://abc_'


You'll have to pick some value for the temporary string, that you can be certain won't appear in your input (and doesn't contain any special characters that would interfere with sed or shell quoting). I used protected but something more distinctive is advisable.



Thorough method



If you can't be certain that your temporary string won't occur in the input, a longer but safer alternative would be to use two replacement strings:



sed -e 's/X/Xv/g;s_https://abc_Xu_g;/^# Start/,$s/abc/123/g;s_Xu_https://abc_g;s/Xv/X/g'


The requirements of these two temporary strings are:




  • They have a common prefix (I used X, but it can be longer/more readable)

  • They don't occur within the other variables: orig, new, Initial or Final

  • As before, they don't contain characters that would break the sed expression






share|improve this answer














You could temporarily change all instances that start with https://to something else, not containing abc, and change them back when you've finished.



Lazy method



sed -e 's_https://abc_protected_g;/# Start/,$s_abc_123_g;s_protected_https://abc_'


You'll have to pick some value for the temporary string, that you can be certain won't appear in your input (and doesn't contain any special characters that would interfere with sed or shell quoting). I used protected but something more distinctive is advisable.



Thorough method



If you can't be certain that your temporary string won't occur in the input, a longer but safer alternative would be to use two replacement strings:



sed -e 's/X/Xv/g;s_https://abc_Xu_g;/^# Start/,$s/abc/123/g;s_Xu_https://abc_g;s/Xv/X/g'


The requirements of these two temporary strings are:




  • They have a common prefix (I used X, but it can be longer/more readable)

  • They don't occur within the other variables: orig, new, Initial or Final

  • As before, they don't contain characters that would break the sed expression







share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 14 at 11:39

























answered Dec 13 at 13:12









JigglyNaga

3,678930




3,678930












  • It is interesting to see PCRE features implemented in a pure BRE way, in a single sed invocation. I am wondering whether other PCRE features have their equivalents as well.
    – Weijun Zhou
    Dec 14 at 2:10












  • @WeijunZhou As noted elsewhere this does not reimplement negative lookaheads, only the subproblem of negative lookaheads with a static string. Some Perl / PCRE extensions are purely convenience / syntactic sugar which is straightforward (but often cumbersome or boring) to replace with traditional regex, but e.g. lookarounds genuinely extend the formalism to something which is no longer theoretically equivalent (reducible) to regular expressions.
    – tripleee
    Dec 14 at 6:13












  • There is no contradiction. A PCRE not being reducible to regular expression is one thing, and I agree on that. Using sed to modify the text to assist in pattern matching is another thing, which is no longer simple pattern patching and hence does not contradict. To put it more clearly, there is still no way to use grep to match lookaround patterns with BRE or ERE.
    – Weijun Zhou
    Dec 14 at 7:40




















  • It is interesting to see PCRE features implemented in a pure BRE way, in a single sed invocation. I am wondering whether other PCRE features have their equivalents as well.
    – Weijun Zhou
    Dec 14 at 2:10












  • @WeijunZhou As noted elsewhere this does not reimplement negative lookaheads, only the subproblem of negative lookaheads with a static string. Some Perl / PCRE extensions are purely convenience / syntactic sugar which is straightforward (but often cumbersome or boring) to replace with traditional regex, but e.g. lookarounds genuinely extend the formalism to something which is no longer theoretically equivalent (reducible) to regular expressions.
    – tripleee
    Dec 14 at 6:13












  • There is no contradiction. A PCRE not being reducible to regular expression is one thing, and I agree on that. Using sed to modify the text to assist in pattern matching is another thing, which is no longer simple pattern patching and hence does not contradict. To put it more clearly, there is still no way to use grep to match lookaround patterns with BRE or ERE.
    – Weijun Zhou
    Dec 14 at 7:40


















It is interesting to see PCRE features implemented in a pure BRE way, in a single sed invocation. I am wondering whether other PCRE features have their equivalents as well.
– Weijun Zhou
Dec 14 at 2:10






It is interesting to see PCRE features implemented in a pure BRE way, in a single sed invocation. I am wondering whether other PCRE features have their equivalents as well.
– Weijun Zhou
Dec 14 at 2:10














@WeijunZhou As noted elsewhere this does not reimplement negative lookaheads, only the subproblem of negative lookaheads with a static string. Some Perl / PCRE extensions are purely convenience / syntactic sugar which is straightforward (but often cumbersome or boring) to replace with traditional regex, but e.g. lookarounds genuinely extend the formalism to something which is no longer theoretically equivalent (reducible) to regular expressions.
– tripleee
Dec 14 at 6:13






@WeijunZhou As noted elsewhere this does not reimplement negative lookaheads, only the subproblem of negative lookaheads with a static string. Some Perl / PCRE extensions are purely convenience / syntactic sugar which is straightforward (but often cumbersome or boring) to replace with traditional regex, but e.g. lookarounds genuinely extend the formalism to something which is no longer theoretically equivalent (reducible) to regular expressions.
– tripleee
Dec 14 at 6:13














There is no contradiction. A PCRE not being reducible to regular expression is one thing, and I agree on that. Using sed to modify the text to assist in pattern matching is another thing, which is no longer simple pattern patching and hence does not contradict. To put it more clearly, there is still no way to use grep to match lookaround patterns with BRE or ERE.
– Weijun Zhou
Dec 14 at 7:40






There is no contradiction. A PCRE not being reducible to regular expression is one thing, and I agree on that. Using sed to modify the text to assist in pattern matching is another thing, which is no longer simple pattern patching and hence does not contradict. To put it more clearly, there is still no way to use grep to match lookaround patterns with BRE or ERE.
– Weijun Zhou
Dec 14 at 7:40




















draft saved

draft discarded




















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f487725%2fsed-replace-word-except-when-word-is-preceded-by-a-specific-string%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Morgemoulin

Scott Moir

Souastre