11. Given a file, replace all occurrence of word “DEF” with “ABC” from 5th line till end in only...
Why is the T-1000 humanoid?
In what state are satellites left in when they are left in a graveyard orbit?
Which is the current decimal separator?
How do I get rid of distortion in pictures of distant objects photographed with a telephoto lens?
How to publish superseding results without creating enemies
Why some files are not movable in Windows 10
Bash, import output from command as command
Why is my fire extinguisher emptied after one use?
Is "you will become a subject matter expert" code for "you'll be working on your own 100% of the time"?
What is my breathable atmosphere composed of?
My research paper filed as a patent in China by my Chinese supervisor without me as inventor
Will the UK home office know about 5 previous visa rejections in other countries?
What officially disallows US presidents from driving?
Can I conceal an antihero's insanity - and should I?
5e Level 1 Druid cantrips
What is the mathematical notation for rounding a given number to the nearest integer?
Can I toggle Do Not Disturb on/off on my Mac as easily as I can on my iPhone?
Should you only use colons and periods in dialogues?
What was the ultimate objective of The Party in 1984?
Origin of the term "sinc" function
"Literally" Vs "In the true sense of the word"
What is the derivative of an exponential function with another function as its base?
Some Prime Peerage
Why is this weapon searching for a new owner?
11. Given a file, replace all occurrence of word “DEF” with “ABC” from 5th line till end in only those lines that contains word “MNO”
How to sed only that lines that contains given string?sed - change lines that start with one given word and end with another?Reading a string till a key word and replacing from there with another stringBash find and replace in a C++ fileRewrite a find command that uses sed -i for AIXBash - Integer expression expectedshell script to test condition on passed stringSubstitution of a line with another line in multiple filessed command to replace and write
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I was able to substitute all occurrences of "DEF" string with "ABC" string starting from line 5 in my file called "DEFABC.txt". However, when I try to add another condition with an if statement (only substitute if the line contains "MNO" string) the script still substitutes all occurrences after line 5. What am I doing wrong? Thank you!
shell-script sed
New contributor
add a comment
|
I was able to substitute all occurrences of "DEF" string with "ABC" string starting from line 5 in my file called "DEFABC.txt". However, when I try to add another condition with an if statement (only substitute if the line contains "MNO" string) the script still substitutes all occurrences after line 5. What am I doing wrong? Thank you!
shell-script sed
New contributor
1
Please don't post screenshots of text. Copy the text here and use code formatting instead
– muru
12 mins ago
add a comment
|
I was able to substitute all occurrences of "DEF" string with "ABC" string starting from line 5 in my file called "DEFABC.txt". However, when I try to add another condition with an if statement (only substitute if the line contains "MNO" string) the script still substitutes all occurrences after line 5. What am I doing wrong? Thank you!
shell-script sed
New contributor
I was able to substitute all occurrences of "DEF" string with "ABC" string starting from line 5 in my file called "DEFABC.txt". However, when I try to add another condition with an if statement (only substitute if the line contains "MNO" string) the script still substitutes all occurrences after line 5. What am I doing wrong? Thank you!
shell-script sed
shell-script sed
New contributor
New contributor
New contributor
asked 20 mins ago
KristinaKristina
1
1
New contributor
New contributor
1
Please don't post screenshots of text. Copy the text here and use code formatting instead
– muru
12 mins ago
add a comment
|
1
Please don't post screenshots of text. Copy the text here and use code formatting instead
– muru
12 mins ago
1
1
Please don't post screenshots of text. Copy the text here and use code formatting instead
– muru
12 mins ago
Please don't post screenshots of text. Copy the text here and use code formatting instead
– muru
12 mins ago
add a comment
|
1 Answer
1
active
oldest
votes
sed can do this on its own:
sed -e '5,${/MNO/{s/DEF/ABC/g;};}'
This:
- Selects only lines from 5 to the end to act on, with the code in the outer braces.
- Then further selects only those lines from that set containing
MNO
, to act on with the code in the inner braces. - Finally replaces DEF with ABC on those lines.
All other lines are printed unchanged.
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/4.0/"u003ecc by-sa 4.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
});
}
});
Kristina is a new contributor. Be nice, and check out our Code of Conduct.
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%2f541508%2f11-given-a-file-replace-all-occurrence-of-word-def-with-abc-from-5th-line%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
sed can do this on its own:
sed -e '5,${/MNO/{s/DEF/ABC/g;};}'
This:
- Selects only lines from 5 to the end to act on, with the code in the outer braces.
- Then further selects only those lines from that set containing
MNO
, to act on with the code in the inner braces. - Finally replaces DEF with ABC on those lines.
All other lines are printed unchanged.
add a comment
|
sed can do this on its own:
sed -e '5,${/MNO/{s/DEF/ABC/g;};}'
This:
- Selects only lines from 5 to the end to act on, with the code in the outer braces.
- Then further selects only those lines from that set containing
MNO
, to act on with the code in the inner braces. - Finally replaces DEF with ABC on those lines.
All other lines are printed unchanged.
add a comment
|
sed can do this on its own:
sed -e '5,${/MNO/{s/DEF/ABC/g;};}'
This:
- Selects only lines from 5 to the end to act on, with the code in the outer braces.
- Then further selects only those lines from that set containing
MNO
, to act on with the code in the inner braces. - Finally replaces DEF with ABC on those lines.
All other lines are printed unchanged.
sed can do this on its own:
sed -e '5,${/MNO/{s/DEF/ABC/g;};}'
This:
- Selects only lines from 5 to the end to act on, with the code in the outer braces.
- Then further selects only those lines from that set containing
MNO
, to act on with the code in the inner braces. - Finally replaces DEF with ABC on those lines.
All other lines are printed unchanged.
answered 10 mins ago
Michael HomerMichael Homer
55.3k9 gold badges155 silver badges189 bronze badges
55.3k9 gold badges155 silver badges189 bronze badges
add a comment
|
add a comment
|
Kristina is a new contributor. Be nice, and check out our Code of Conduct.
Kristina is a new contributor. Be nice, and check out our Code of Conduct.
Kristina is a new contributor. Be nice, and check out our Code of Conduct.
Kristina is a new contributor. Be nice, and check out our Code of Conduct.
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%2f541508%2f11-given-a-file-replace-all-occurrence-of-word-def-with-abc-from-5th-line%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
1
Please don't post screenshots of text. Copy the text here and use code formatting instead
– muru
12 mins ago