How to Insert a line after a pattern and insert another line before the first occurence of a word from...












0














sub tc1 {
a
b
c
d
endcase
}
sub tc2
{
a
b
c
endcase
}


I want to call a subroutine say start() after sub tc1 and call stop() before that endcase of sub tc1



I used this command



perl -i.bak -pe 'BEGIN{undef $/;} s/(subs+TC1[sn]+{)(.*)endcase/$1n&start();$2&stop();nendcase/smg' file.txt


Output



sub tc1 {
&start();
a
b
c
d
endcase
}
sub tc2
{
a
b
c
stop();
endcase
}


but it is modifying at lastly matched endcase pattern



Expected output



sub tc1 {
&start();
a
b
c
d
&stop();
endcase
}









share|improve this question
























  • Not clear, please do explain the logic behind the expected output.
    – RavinderSingh13
    Nov 18 at 11:46










  • I want to append a line start(); after the pattern /sub tc1/ and prepend a line stop(); before the first occurence of the pattern /endcase/ after /sub tc1/
    – Sasi
    Nov 18 at 11:51
















0














sub tc1 {
a
b
c
d
endcase
}
sub tc2
{
a
b
c
endcase
}


I want to call a subroutine say start() after sub tc1 and call stop() before that endcase of sub tc1



I used this command



perl -i.bak -pe 'BEGIN{undef $/;} s/(subs+TC1[sn]+{)(.*)endcase/$1n&start();$2&stop();nendcase/smg' file.txt


Output



sub tc1 {
&start();
a
b
c
d
endcase
}
sub tc2
{
a
b
c
stop();
endcase
}


but it is modifying at lastly matched endcase pattern



Expected output



sub tc1 {
&start();
a
b
c
d
&stop();
endcase
}









share|improve this question
























  • Not clear, please do explain the logic behind the expected output.
    – RavinderSingh13
    Nov 18 at 11:46










  • I want to append a line start(); after the pattern /sub tc1/ and prepend a line stop(); before the first occurence of the pattern /endcase/ after /sub tc1/
    – Sasi
    Nov 18 at 11:51














0












0








0







sub tc1 {
a
b
c
d
endcase
}
sub tc2
{
a
b
c
endcase
}


I want to call a subroutine say start() after sub tc1 and call stop() before that endcase of sub tc1



I used this command



perl -i.bak -pe 'BEGIN{undef $/;} s/(subs+TC1[sn]+{)(.*)endcase/$1n&start();$2&stop();nendcase/smg' file.txt


Output



sub tc1 {
&start();
a
b
c
d
endcase
}
sub tc2
{
a
b
c
stop();
endcase
}


but it is modifying at lastly matched endcase pattern



Expected output



sub tc1 {
&start();
a
b
c
d
&stop();
endcase
}









share|improve this question















sub tc1 {
a
b
c
d
endcase
}
sub tc2
{
a
b
c
endcase
}


I want to call a subroutine say start() after sub tc1 and call stop() before that endcase of sub tc1



I used this command



perl -i.bak -pe 'BEGIN{undef $/;} s/(subs+TC1[sn]+{)(.*)endcase/$1n&start();$2&stop();nendcase/smg' file.txt


Output



sub tc1 {
&start();
a
b
c
d
endcase
}
sub tc2
{
a
b
c
stop();
endcase
}


but it is modifying at lastly matched endcase pattern



Expected output



sub tc1 {
&start();
a
b
c
d
&stop();
endcase
}






perl sed






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 18 at 11:49

























asked Nov 18 at 11:32









Sasi

53




53












  • Not clear, please do explain the logic behind the expected output.
    – RavinderSingh13
    Nov 18 at 11:46










  • I want to append a line start(); after the pattern /sub tc1/ and prepend a line stop(); before the first occurence of the pattern /endcase/ after /sub tc1/
    – Sasi
    Nov 18 at 11:51


















  • Not clear, please do explain the logic behind the expected output.
    – RavinderSingh13
    Nov 18 at 11:46










  • I want to append a line start(); after the pattern /sub tc1/ and prepend a line stop(); before the first occurence of the pattern /endcase/ after /sub tc1/
    – Sasi
    Nov 18 at 11:51
















Not clear, please do explain the logic behind the expected output.
– RavinderSingh13
Nov 18 at 11:46




Not clear, please do explain the logic behind the expected output.
– RavinderSingh13
Nov 18 at 11:46












I want to append a line start(); after the pattern /sub tc1/ and prepend a line stop(); before the first occurence of the pattern /endcase/ after /sub tc1/
– Sasi
Nov 18 at 11:51




I want to append a line start(); after the pattern /sub tc1/ and prepend a line stop(); before the first occurence of the pattern /endcase/ after /sub tc1/
– Sasi
Nov 18 at 11:51












2 Answers
2






active

oldest

votes


















0














This should be easy task of awk, could you please try following if you are ok with awk.



awk '
/sub tc1/{
sub_tcl_val=$0 ORS "&start();"
flag=val=""
next
}
/endcase/ && ++count==1{
flag=1
print sub_tcl_val ORS val ORS "&stop()" ORS $0 ORS "}"
val=sub_tcl_val=""
next
}
!flag{
val=val?val ORS $0:$0
}' Input_file


Output will be as follows.



sub tc1 {
&start();
a
b
c
d
&stop()
endcase
}





share|improve this answer























  • But if I have lines before sub tc1 it is appending those lines after start();
    – Sasi
    Nov 18 at 13:33










  • @Sasi, I have edited it now, could you please check now and let me know?
    – RavinderSingh13
    Nov 18 at 13:40



















0














This might work for you (GNU sed):



sed -e '/sub tc1/,/endcase/!b;/sub tc1/a&start();' -e '/endcase/i&stop();' file


Use a range to focus on the start/end delimiters in this case sub tc1/endcase. Then append the string &start(); after the start delimiter and insert the string &stop(); before the end delimiter.






share|improve this answer





















  • $ sed -e '/sub tc1/,/endcase/!b;/sub tc1/a&start();' -e '/endcase/i&stop();' myfil sed -e '/sub tc1/,/endcase/bvi;/sub tc1/a&start();' -e '/endcase/i&stop();' myfil sed: can't find label for jump to `vi'
    – Sasi
    Nov 23 at 8:50










  • @sasi your input seems some what garbled. Try removing everything following the first occurrence of myfil.
    – potong
    Nov 23 at 8:58











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%2f53360376%2fhow-to-insert-a-line-after-a-pattern-and-insert-another-line-before-the-first-oc%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









0














This should be easy task of awk, could you please try following if you are ok with awk.



awk '
/sub tc1/{
sub_tcl_val=$0 ORS "&start();"
flag=val=""
next
}
/endcase/ && ++count==1{
flag=1
print sub_tcl_val ORS val ORS "&stop()" ORS $0 ORS "}"
val=sub_tcl_val=""
next
}
!flag{
val=val?val ORS $0:$0
}' Input_file


Output will be as follows.



sub tc1 {
&start();
a
b
c
d
&stop()
endcase
}





share|improve this answer























  • But if I have lines before sub tc1 it is appending those lines after start();
    – Sasi
    Nov 18 at 13:33










  • @Sasi, I have edited it now, could you please check now and let me know?
    – RavinderSingh13
    Nov 18 at 13:40
















0














This should be easy task of awk, could you please try following if you are ok with awk.



awk '
/sub tc1/{
sub_tcl_val=$0 ORS "&start();"
flag=val=""
next
}
/endcase/ && ++count==1{
flag=1
print sub_tcl_val ORS val ORS "&stop()" ORS $0 ORS "}"
val=sub_tcl_val=""
next
}
!flag{
val=val?val ORS $0:$0
}' Input_file


Output will be as follows.



sub tc1 {
&start();
a
b
c
d
&stop()
endcase
}





share|improve this answer























  • But if I have lines before sub tc1 it is appending those lines after start();
    – Sasi
    Nov 18 at 13:33










  • @Sasi, I have edited it now, could you please check now and let me know?
    – RavinderSingh13
    Nov 18 at 13:40














0












0








0






This should be easy task of awk, could you please try following if you are ok with awk.



awk '
/sub tc1/{
sub_tcl_val=$0 ORS "&start();"
flag=val=""
next
}
/endcase/ && ++count==1{
flag=1
print sub_tcl_val ORS val ORS "&stop()" ORS $0 ORS "}"
val=sub_tcl_val=""
next
}
!flag{
val=val?val ORS $0:$0
}' Input_file


Output will be as follows.



sub tc1 {
&start();
a
b
c
d
&stop()
endcase
}





share|improve this answer














This should be easy task of awk, could you please try following if you are ok with awk.



awk '
/sub tc1/{
sub_tcl_val=$0 ORS "&start();"
flag=val=""
next
}
/endcase/ && ++count==1{
flag=1
print sub_tcl_val ORS val ORS "&stop()" ORS $0 ORS "}"
val=sub_tcl_val=""
next
}
!flag{
val=val?val ORS $0:$0
}' Input_file


Output will be as follows.



sub tc1 {
&start();
a
b
c
d
&stop()
endcase
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 18 at 13:40

























answered Nov 18 at 12:04









RavinderSingh13

25.5k41438




25.5k41438












  • But if I have lines before sub tc1 it is appending those lines after start();
    – Sasi
    Nov 18 at 13:33










  • @Sasi, I have edited it now, could you please check now and let me know?
    – RavinderSingh13
    Nov 18 at 13:40


















  • But if I have lines before sub tc1 it is appending those lines after start();
    – Sasi
    Nov 18 at 13:33










  • @Sasi, I have edited it now, could you please check now and let me know?
    – RavinderSingh13
    Nov 18 at 13:40
















But if I have lines before sub tc1 it is appending those lines after start();
– Sasi
Nov 18 at 13:33




But if I have lines before sub tc1 it is appending those lines after start();
– Sasi
Nov 18 at 13:33












@Sasi, I have edited it now, could you please check now and let me know?
– RavinderSingh13
Nov 18 at 13:40




@Sasi, I have edited it now, could you please check now and let me know?
– RavinderSingh13
Nov 18 at 13:40













0














This might work for you (GNU sed):



sed -e '/sub tc1/,/endcase/!b;/sub tc1/a&start();' -e '/endcase/i&stop();' file


Use a range to focus on the start/end delimiters in this case sub tc1/endcase. Then append the string &start(); after the start delimiter and insert the string &stop(); before the end delimiter.






share|improve this answer





















  • $ sed -e '/sub tc1/,/endcase/!b;/sub tc1/a&start();' -e '/endcase/i&stop();' myfil sed -e '/sub tc1/,/endcase/bvi;/sub tc1/a&start();' -e '/endcase/i&stop();' myfil sed: can't find label for jump to `vi'
    – Sasi
    Nov 23 at 8:50










  • @sasi your input seems some what garbled. Try removing everything following the first occurrence of myfil.
    – potong
    Nov 23 at 8:58
















0














This might work for you (GNU sed):



sed -e '/sub tc1/,/endcase/!b;/sub tc1/a&start();' -e '/endcase/i&stop();' file


Use a range to focus on the start/end delimiters in this case sub tc1/endcase. Then append the string &start(); after the start delimiter and insert the string &stop(); before the end delimiter.






share|improve this answer





















  • $ sed -e '/sub tc1/,/endcase/!b;/sub tc1/a&start();' -e '/endcase/i&stop();' myfil sed -e '/sub tc1/,/endcase/bvi;/sub tc1/a&start();' -e '/endcase/i&stop();' myfil sed: can't find label for jump to `vi'
    – Sasi
    Nov 23 at 8:50










  • @sasi your input seems some what garbled. Try removing everything following the first occurrence of myfil.
    – potong
    Nov 23 at 8:58














0












0








0






This might work for you (GNU sed):



sed -e '/sub tc1/,/endcase/!b;/sub tc1/a&start();' -e '/endcase/i&stop();' file


Use a range to focus on the start/end delimiters in this case sub tc1/endcase. Then append the string &start(); after the start delimiter and insert the string &stop(); before the end delimiter.






share|improve this answer












This might work for you (GNU sed):



sed -e '/sub tc1/,/endcase/!b;/sub tc1/a&start();' -e '/endcase/i&stop();' file


Use a range to focus on the start/end delimiters in this case sub tc1/endcase. Then append the string &start(); after the start delimiter and insert the string &stop(); before the end delimiter.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 at 11:39









potong

35k42960




35k42960












  • $ sed -e '/sub tc1/,/endcase/!b;/sub tc1/a&start();' -e '/endcase/i&stop();' myfil sed -e '/sub tc1/,/endcase/bvi;/sub tc1/a&start();' -e '/endcase/i&stop();' myfil sed: can't find label for jump to `vi'
    – Sasi
    Nov 23 at 8:50










  • @sasi your input seems some what garbled. Try removing everything following the first occurrence of myfil.
    – potong
    Nov 23 at 8:58


















  • $ sed -e '/sub tc1/,/endcase/!b;/sub tc1/a&start();' -e '/endcase/i&stop();' myfil sed -e '/sub tc1/,/endcase/bvi;/sub tc1/a&start();' -e '/endcase/i&stop();' myfil sed: can't find label for jump to `vi'
    – Sasi
    Nov 23 at 8:50










  • @sasi your input seems some what garbled. Try removing everything following the first occurrence of myfil.
    – potong
    Nov 23 at 8:58
















$ sed -e '/sub tc1/,/endcase/!b;/sub tc1/a&start();' -e '/endcase/i&stop();' myfil sed -e '/sub tc1/,/endcase/bvi;/sub tc1/a&start();' -e '/endcase/i&stop();' myfil sed: can't find label for jump to `vi'
– Sasi
Nov 23 at 8:50




$ sed -e '/sub tc1/,/endcase/!b;/sub tc1/a&start();' -e '/endcase/i&stop();' myfil sed -e '/sub tc1/,/endcase/bvi;/sub tc1/a&start();' -e '/endcase/i&stop();' myfil sed: can't find label for jump to `vi'
– Sasi
Nov 23 at 8:50












@sasi your input seems some what garbled. Try removing everything following the first occurrence of myfil.
– potong
Nov 23 at 8:58




@sasi your input seems some what garbled. Try removing everything following the first occurrence of myfil.
– potong
Nov 23 at 8:58


















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.





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%2fstackoverflow.com%2fquestions%2f53360376%2fhow-to-insert-a-line-after-a-pattern-and-insert-another-line-before-the-first-oc%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

Volksrepublik China

How to test boost logger output in unit testing?

Write to the output between two pipeline