Search and replace a substring only if another substring is not presentSearch and replace with sedHow can I replace a string in a file(s)?Find & replace image URLs in one file from another fileHow to replace string of the file, which is present in tar file through UNIXsed append a text with many lines after matching of multiple strings while the text remains many lines in sed commandBash while loop search and replace using sedExtracting values from a text file having | pipe as a delimiter in text file using awk command and Replacing new lines with <br> tag using sed?How to run different python scripts from command line by passing the script name as argumentchange only part of the substring using sedSed to replace lowercase and capital strings
How can I find an old paper when the usual methods fail?
What is the hottest thing in the universe?
Telephone number in spoken words
Why does this Jet Provost strikemaster have a textured leading edge?
How do I call a 6-digit Australian phone number with a US-based mobile phone?
Why did IBM make the PC BIOS source code public?
How to programatically get all linked items for a given Sitecore item?
Setting up a Mathematical Institute of Refereeing?
How to gracefully leave a company you helped start?
What evidence points to a long ō in the first syllable of nōscō's present-tense form?
Source that you can't tell your wife not to lend to others
Bringing Power Supplies on Plane?
How do figure out how powerful I am, when my abilities far exceed my knowledge?
What is a "soap"?
Is there a word for returning to unpreparedness?
What's the point of writing that I know will never be used or read?
Output the list of musical notes
The more + the + comparative degree
Will some rockets really collapse under their own weight?
Locked room poison mystery!
Why do my bicycle brakes get worse and feel more 'squishy" over time?
How can I shoot a bow using Strength instead of Dexterity?
Perpendicular symbol with litle square
Unconventional examples of mathematical modelling
Search and replace a substring only if another substring is not present
Search and replace with sedHow can I replace a string in a file(s)?Find & replace image URLs in one file from another fileHow to replace string of the file, which is present in tar file through UNIXsed append a text with many lines after matching of multiple strings while the text remains many lines in sed commandBash while loop search and replace using sedExtracting values from a text file having | pipe as a delimiter in text file using awk command and Replacing new lines with <br> tag using sed?How to run different python scripts from command line by passing the script name as argumentchange only part of the substring using sedSed to replace lowercase and capital strings
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have the following strings in a very large document:
1.test.html#
2.test.md#
3.http://test.html#
4.https://test.md#
5.http://test.md#
6.test2.md#
Now I want to replace every .md#
with .html#
but ONLY if there is no http
in the string. So only 2 and 6 should have a replacement. How can I do this in a shell script?
shell-script sed
add a comment |
I have the following strings in a very large document:
1.test.html#
2.test.md#
3.http://test.html#
4.https://test.md#
5.http://test.md#
6.test2.md#
Now I want to replace every .md#
with .html#
but ONLY if there is no http
in the string. So only 2 and 6 should have a replacement. How can I do this in a shell script?
shell-script sed
add a comment |
I have the following strings in a very large document:
1.test.html#
2.test.md#
3.http://test.html#
4.https://test.md#
5.http://test.md#
6.test2.md#
Now I want to replace every .md#
with .html#
but ONLY if there is no http
in the string. So only 2 and 6 should have a replacement. How can I do this in a shell script?
shell-script sed
I have the following strings in a very large document:
1.test.html#
2.test.md#
3.http://test.html#
4.https://test.md#
5.http://test.md#
6.test2.md#
Now I want to replace every .md#
with .html#
but ONLY if there is no http
in the string. So only 2 and 6 should have a replacement. How can I do this in a shell script?
shell-script sed
shell-script sed
asked Aug 4 at 5:26
JeroenJeroen
1134 bronze badges
1134 bronze badges
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
With GNU sed. If current line (pattern space) contains http
jump to end of script (b
). Otherwise do search and replace.
sed '/http/b; s/.md#/.html#/' file
Output:
1.test.html#
2.test.html#
3.http://test.html#
4.https://test.md#
5.http://test.md#
6.test2.html#
If you want to edit your file "in place" use sed's option -i
.
See: man sed
4
Very good solution. Just as an alternative:sed '/http/! s/.md#/.html#/' file
– John1024
Aug 4 at 5:49
add a comment |
perl -pe'/http/ or s/.md#/.html#/' input.txt > output.txt
perl -pe'/http/||s/.md#/.html#/' input.txt > output.txt #same
perl -i -pe'/http/||s/.md#/.html#/' file.txt #edit inplace, changes file.txt
perl -i.bk -pe'/http/||s/.md#/.html#/' files*.txt #same with backups to .bk files
sed
and awk
are great, but perl
has everything they have and very much more.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f533784%2fsearch-and-replace-a-substring-only-if-another-substring-is-not-present%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
With GNU sed. If current line (pattern space) contains http
jump to end of script (b
). Otherwise do search and replace.
sed '/http/b; s/.md#/.html#/' file
Output:
1.test.html#
2.test.html#
3.http://test.html#
4.https://test.md#
5.http://test.md#
6.test2.html#
If you want to edit your file "in place" use sed's option -i
.
See: man sed
4
Very good solution. Just as an alternative:sed '/http/! s/.md#/.html#/' file
– John1024
Aug 4 at 5:49
add a comment |
With GNU sed. If current line (pattern space) contains http
jump to end of script (b
). Otherwise do search and replace.
sed '/http/b; s/.md#/.html#/' file
Output:
1.test.html#
2.test.html#
3.http://test.html#
4.https://test.md#
5.http://test.md#
6.test2.html#
If you want to edit your file "in place" use sed's option -i
.
See: man sed
4
Very good solution. Just as an alternative:sed '/http/! s/.md#/.html#/' file
– John1024
Aug 4 at 5:49
add a comment |
With GNU sed. If current line (pattern space) contains http
jump to end of script (b
). Otherwise do search and replace.
sed '/http/b; s/.md#/.html#/' file
Output:
1.test.html#
2.test.html#
3.http://test.html#
4.https://test.md#
5.http://test.md#
6.test2.html#
If you want to edit your file "in place" use sed's option -i
.
See: man sed
With GNU sed. If current line (pattern space) contains http
jump to end of script (b
). Otherwise do search and replace.
sed '/http/b; s/.md#/.html#/' file
Output:
1.test.html#
2.test.html#
3.http://test.html#
4.https://test.md#
5.http://test.md#
6.test2.html#
If you want to edit your file "in place" use sed's option -i
.
See: man sed
answered Aug 4 at 5:29
CyrusCyrus
7,8762 gold badges12 silver badges41 bronze badges
7,8762 gold badges12 silver badges41 bronze badges
4
Very good solution. Just as an alternative:sed '/http/! s/.md#/.html#/' file
– John1024
Aug 4 at 5:49
add a comment |
4
Very good solution. Just as an alternative:sed '/http/! s/.md#/.html#/' file
– John1024
Aug 4 at 5:49
4
4
Very good solution. Just as an alternative:
sed '/http/! s/.md#/.html#/' file
– John1024
Aug 4 at 5:49
Very good solution. Just as an alternative:
sed '/http/! s/.md#/.html#/' file
– John1024
Aug 4 at 5:49
add a comment |
perl -pe'/http/ or s/.md#/.html#/' input.txt > output.txt
perl -pe'/http/||s/.md#/.html#/' input.txt > output.txt #same
perl -i -pe'/http/||s/.md#/.html#/' file.txt #edit inplace, changes file.txt
perl -i.bk -pe'/http/||s/.md#/.html#/' files*.txt #same with backups to .bk files
sed
and awk
are great, but perl
has everything they have and very much more.
add a comment |
perl -pe'/http/ or s/.md#/.html#/' input.txt > output.txt
perl -pe'/http/||s/.md#/.html#/' input.txt > output.txt #same
perl -i -pe'/http/||s/.md#/.html#/' file.txt #edit inplace, changes file.txt
perl -i.bk -pe'/http/||s/.md#/.html#/' files*.txt #same with backups to .bk files
sed
and awk
are great, but perl
has everything they have and very much more.
add a comment |
perl -pe'/http/ or s/.md#/.html#/' input.txt > output.txt
perl -pe'/http/||s/.md#/.html#/' input.txt > output.txt #same
perl -i -pe'/http/||s/.md#/.html#/' file.txt #edit inplace, changes file.txt
perl -i.bk -pe'/http/||s/.md#/.html#/' files*.txt #same with backups to .bk files
sed
and awk
are great, but perl
has everything they have and very much more.
perl -pe'/http/ or s/.md#/.html#/' input.txt > output.txt
perl -pe'/http/||s/.md#/.html#/' input.txt > output.txt #same
perl -i -pe'/http/||s/.md#/.html#/' file.txt #edit inplace, changes file.txt
perl -i.bk -pe'/http/||s/.md#/.html#/' files*.txt #same with backups to .bk files
sed
and awk
are great, but perl
has everything they have and very much more.
edited Aug 5 at 10:55
answered Aug 4 at 8:59
Kjetil S.Kjetil S.
1755 bronze badges
1755 bronze badges
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f533784%2fsearch-and-replace-a-substring-only-if-another-substring-is-not-present%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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