How to search for files with a pattern of 2 consecutive new lines & modify the succeeding line?












1















I have a few directories with a bunch of files that I need to change. That change involves me finding the files that includes the selector and service labels:



selector:
service: XXXXX


and replacing the service label with app:



selector:
app: XXXXX


Btw, none of the solutions in this similar stack overflow thread work.










share|improve this question

























  • Start with reading about sed, and here's a question that may help you get some ideas: stackoverflow.com/questions/16123102/…

    – dcp
    Nov 19 '18 at 16:52






  • 1





    Since this is a YAML file, I suggest you read in the YAML into whatever language you're using, and then replace the element of the structure that you want to change. Then, write out the new YAML based on your modifications. In general, if you have structured data in a specific format, it's far safer and easier to read it, modify it, and rewrite it, rather than playing around with it as plain text.

    – Andy Lester
    Nov 19 '18 at 17:04











  • @dcp I tried applying all of their answers and each of them threw an invalid command code.

    – seemcat
    Nov 19 '18 at 17:26
















1















I have a few directories with a bunch of files that I need to change. That change involves me finding the files that includes the selector and service labels:



selector:
service: XXXXX


and replacing the service label with app:



selector:
app: XXXXX


Btw, none of the solutions in this similar stack overflow thread work.










share|improve this question

























  • Start with reading about sed, and here's a question that may help you get some ideas: stackoverflow.com/questions/16123102/…

    – dcp
    Nov 19 '18 at 16:52






  • 1





    Since this is a YAML file, I suggest you read in the YAML into whatever language you're using, and then replace the element of the structure that you want to change. Then, write out the new YAML based on your modifications. In general, if you have structured data in a specific format, it's far safer and easier to read it, modify it, and rewrite it, rather than playing around with it as plain text.

    – Andy Lester
    Nov 19 '18 at 17:04











  • @dcp I tried applying all of their answers and each of them threw an invalid command code.

    – seemcat
    Nov 19 '18 at 17:26














1












1








1








I have a few directories with a bunch of files that I need to change. That change involves me finding the files that includes the selector and service labels:



selector:
service: XXXXX


and replacing the service label with app:



selector:
app: XXXXX


Btw, none of the solutions in this similar stack overflow thread work.










share|improve this question
















I have a few directories with a bunch of files that I need to change. That change involves me finding the files that includes the selector and service labels:



selector:
service: XXXXX


and replacing the service label with app:



selector:
app: XXXXX


Btw, none of the solutions in this similar stack overflow thread work.







grep yaml






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 19:31







seemcat

















asked Nov 19 '18 at 16:48









seemcatseemcat

429




429













  • Start with reading about sed, and here's a question that may help you get some ideas: stackoverflow.com/questions/16123102/…

    – dcp
    Nov 19 '18 at 16:52






  • 1





    Since this is a YAML file, I suggest you read in the YAML into whatever language you're using, and then replace the element of the structure that you want to change. Then, write out the new YAML based on your modifications. In general, if you have structured data in a specific format, it's far safer and easier to read it, modify it, and rewrite it, rather than playing around with it as plain text.

    – Andy Lester
    Nov 19 '18 at 17:04











  • @dcp I tried applying all of their answers and each of them threw an invalid command code.

    – seemcat
    Nov 19 '18 at 17:26



















  • Start with reading about sed, and here's a question that may help you get some ideas: stackoverflow.com/questions/16123102/…

    – dcp
    Nov 19 '18 at 16:52






  • 1





    Since this is a YAML file, I suggest you read in the YAML into whatever language you're using, and then replace the element of the structure that you want to change. Then, write out the new YAML based on your modifications. In general, if you have structured data in a specific format, it's far safer and easier to read it, modify it, and rewrite it, rather than playing around with it as plain text.

    – Andy Lester
    Nov 19 '18 at 17:04











  • @dcp I tried applying all of their answers and each of them threw an invalid command code.

    – seemcat
    Nov 19 '18 at 17:26

















Start with reading about sed, and here's a question that may help you get some ideas: stackoverflow.com/questions/16123102/…

– dcp
Nov 19 '18 at 16:52





Start with reading about sed, and here's a question that may help you get some ideas: stackoverflow.com/questions/16123102/…

– dcp
Nov 19 '18 at 16:52




1




1





Since this is a YAML file, I suggest you read in the YAML into whatever language you're using, and then replace the element of the structure that you want to change. Then, write out the new YAML based on your modifications. In general, if you have structured data in a specific format, it's far safer and easier to read it, modify it, and rewrite it, rather than playing around with it as plain text.

– Andy Lester
Nov 19 '18 at 17:04





Since this is a YAML file, I suggest you read in the YAML into whatever language you're using, and then replace the element of the structure that you want to change. Then, write out the new YAML based on your modifications. In general, if you have structured data in a specific format, it's far safer and easier to read it, modify it, and rewrite it, rather than playing around with it as plain text.

– Andy Lester
Nov 19 '18 at 17:04













@dcp I tried applying all of their answers and each of them threw an invalid command code.

– seemcat
Nov 19 '18 at 17:26





@dcp I tried applying all of their answers and each of them threw an invalid command code.

– seemcat
Nov 19 '18 at 17:26












1 Answer
1






active

oldest

votes


















1














In case You wanna AWK (gawk) solution. Here it is.
Considering input per Your question:



$ cat v1mg1rl.txt
application:
id: 0
service: WWWWW
selector:
id: 1
service: XXXXX
cartridge:
id: 2
service: ZZZZZ


AWK:



$ awk '/^[^ ]+/ { isselector=0; } /^selector:/ { isselector=1; } /^ +service: / { if (isselector) sub(/service: /,"app: "); } { print($0); }' v1mg1rl.txt
application:
id: 0
service: WWWWW
selector:
id: 1
app: XXXXX
cartridge:
id: 2
service: ZZZZZ





share|improve this answer
























  • Thanks, I appreciate ya' Kubator! (Lol, nice name btw.)

    – seemcat
    Nov 20 '18 at 20:06











  • Sure thing. Glad if helped.

    – Kubator
    Nov 21 '18 at 16:49











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f53379229%2fhow-to-search-for-files-with-a-pattern-of-2-consecutive-new-lines-modify-the-s%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









1














In case You wanna AWK (gawk) solution. Here it is.
Considering input per Your question:



$ cat v1mg1rl.txt
application:
id: 0
service: WWWWW
selector:
id: 1
service: XXXXX
cartridge:
id: 2
service: ZZZZZ


AWK:



$ awk '/^[^ ]+/ { isselector=0; } /^selector:/ { isselector=1; } /^ +service: / { if (isselector) sub(/service: /,"app: "); } { print($0); }' v1mg1rl.txt
application:
id: 0
service: WWWWW
selector:
id: 1
app: XXXXX
cartridge:
id: 2
service: ZZZZZ





share|improve this answer
























  • Thanks, I appreciate ya' Kubator! (Lol, nice name btw.)

    – seemcat
    Nov 20 '18 at 20:06











  • Sure thing. Glad if helped.

    – Kubator
    Nov 21 '18 at 16:49
















1














In case You wanna AWK (gawk) solution. Here it is.
Considering input per Your question:



$ cat v1mg1rl.txt
application:
id: 0
service: WWWWW
selector:
id: 1
service: XXXXX
cartridge:
id: 2
service: ZZZZZ


AWK:



$ awk '/^[^ ]+/ { isselector=0; } /^selector:/ { isselector=1; } /^ +service: / { if (isselector) sub(/service: /,"app: "); } { print($0); }' v1mg1rl.txt
application:
id: 0
service: WWWWW
selector:
id: 1
app: XXXXX
cartridge:
id: 2
service: ZZZZZ





share|improve this answer
























  • Thanks, I appreciate ya' Kubator! (Lol, nice name btw.)

    – seemcat
    Nov 20 '18 at 20:06











  • Sure thing. Glad if helped.

    – Kubator
    Nov 21 '18 at 16:49














1












1








1







In case You wanna AWK (gawk) solution. Here it is.
Considering input per Your question:



$ cat v1mg1rl.txt
application:
id: 0
service: WWWWW
selector:
id: 1
service: XXXXX
cartridge:
id: 2
service: ZZZZZ


AWK:



$ awk '/^[^ ]+/ { isselector=0; } /^selector:/ { isselector=1; } /^ +service: / { if (isselector) sub(/service: /,"app: "); } { print($0); }' v1mg1rl.txt
application:
id: 0
service: WWWWW
selector:
id: 1
app: XXXXX
cartridge:
id: 2
service: ZZZZZ





share|improve this answer













In case You wanna AWK (gawk) solution. Here it is.
Considering input per Your question:



$ cat v1mg1rl.txt
application:
id: 0
service: WWWWW
selector:
id: 1
service: XXXXX
cartridge:
id: 2
service: ZZZZZ


AWK:



$ awk '/^[^ ]+/ { isselector=0; } /^selector:/ { isselector=1; } /^ +service: / { if (isselector) sub(/service: /,"app: "); } { print($0); }' v1mg1rl.txt
application:
id: 0
service: WWWWW
selector:
id: 1
app: XXXXX
cartridge:
id: 2
service: ZZZZZ






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 '18 at 9:50









KubatorKubator

74911




74911













  • Thanks, I appreciate ya' Kubator! (Lol, nice name btw.)

    – seemcat
    Nov 20 '18 at 20:06











  • Sure thing. Glad if helped.

    – Kubator
    Nov 21 '18 at 16:49



















  • Thanks, I appreciate ya' Kubator! (Lol, nice name btw.)

    – seemcat
    Nov 20 '18 at 20:06











  • Sure thing. Glad if helped.

    – Kubator
    Nov 21 '18 at 16:49

















Thanks, I appreciate ya' Kubator! (Lol, nice name btw.)

– seemcat
Nov 20 '18 at 20:06





Thanks, I appreciate ya' Kubator! (Lol, nice name btw.)

– seemcat
Nov 20 '18 at 20:06













Sure thing. Glad if helped.

– Kubator
Nov 21 '18 at 16:49





Sure thing. Glad if helped.

– Kubator
Nov 21 '18 at 16:49


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • 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%2fstackoverflow.com%2fquestions%2f53379229%2fhow-to-search-for-files-with-a-pattern-of-2-consecutive-new-lines-modify-the-s%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

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?