sed: replace part of text in previous line if match is found [duplicate]











up vote
0
down vote

favorite













This question already has an answer here:




  • Replace a string before a certain line

    5 answers




I want to replace create_test in the file with #create_test if the next line contains psfxxx_16_pi. How can I do it with sed for the whole file containing this?



Input file content:



create_test -type hard -outer { 1.0000 } { 
psfxxx_16_pi/psfop/deadline_north_re_0 }

create_test -type hard -outer { 0.0000 } {
psfxxx_16_pi/psfop/deadline_south_re_1 }


Output file:



#create_test -type hard -outer { 1.0000 } { 
psfxxx_16_pi/psfop/deadline_north_re_0 }

#create_test -type hard -outer { 0.0000 } {
psfxxx_16_pi/psfop/deadline_south_re_1 }









share|improve this question









New contributor




mandrake00 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











marked as duplicate by don_crissti, Jeff Schaller, Isaac, RalfFriedl, Romeo Ninov 6 hours ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • I can do it with perl, but I am looking for a compact one liner command using sed or awk. Thank you Mr. Shunz for the format.
    – mandrake00
    yesterday












  • May be helpful? unix.stackexchange.com/questions/466798/…
    – Sparhawk
    yesterday












  • I'm always curious when I see requests for one-liners; what's wrong with a perl solution?
    – Jeff Schaller
    18 hours ago















up vote
0
down vote

favorite













This question already has an answer here:




  • Replace a string before a certain line

    5 answers




I want to replace create_test in the file with #create_test if the next line contains psfxxx_16_pi. How can I do it with sed for the whole file containing this?



Input file content:



create_test -type hard -outer { 1.0000 } { 
psfxxx_16_pi/psfop/deadline_north_re_0 }

create_test -type hard -outer { 0.0000 } {
psfxxx_16_pi/psfop/deadline_south_re_1 }


Output file:



#create_test -type hard -outer { 1.0000 } { 
psfxxx_16_pi/psfop/deadline_north_re_0 }

#create_test -type hard -outer { 0.0000 } {
psfxxx_16_pi/psfop/deadline_south_re_1 }









share|improve this question









New contributor




mandrake00 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











marked as duplicate by don_crissti, Jeff Schaller, Isaac, RalfFriedl, Romeo Ninov 6 hours ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • I can do it with perl, but I am looking for a compact one liner command using sed or awk. Thank you Mr. Shunz for the format.
    – mandrake00
    yesterday












  • May be helpful? unix.stackexchange.com/questions/466798/…
    – Sparhawk
    yesterday












  • I'm always curious when I see requests for one-liners; what's wrong with a perl solution?
    – Jeff Schaller
    18 hours ago













up vote
0
down vote

favorite









up vote
0
down vote

favorite












This question already has an answer here:




  • Replace a string before a certain line

    5 answers




I want to replace create_test in the file with #create_test if the next line contains psfxxx_16_pi. How can I do it with sed for the whole file containing this?



Input file content:



create_test -type hard -outer { 1.0000 } { 
psfxxx_16_pi/psfop/deadline_north_re_0 }

create_test -type hard -outer { 0.0000 } {
psfxxx_16_pi/psfop/deadline_south_re_1 }


Output file:



#create_test -type hard -outer { 1.0000 } { 
psfxxx_16_pi/psfop/deadline_north_re_0 }

#create_test -type hard -outer { 0.0000 } {
psfxxx_16_pi/psfop/deadline_south_re_1 }









share|improve this question









New contributor




mandrake00 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












This question already has an answer here:




  • Replace a string before a certain line

    5 answers




I want to replace create_test in the file with #create_test if the next line contains psfxxx_16_pi. How can I do it with sed for the whole file containing this?



Input file content:



create_test -type hard -outer { 1.0000 } { 
psfxxx_16_pi/psfop/deadline_north_re_0 }

create_test -type hard -outer { 0.0000 } {
psfxxx_16_pi/psfop/deadline_south_re_1 }


Output file:



#create_test -type hard -outer { 1.0000 } { 
psfxxx_16_pi/psfop/deadline_north_re_0 }

#create_test -type hard -outer { 0.0000 } {
psfxxx_16_pi/psfop/deadline_south_re_1 }




This question already has an answer here:




  • Replace a string before a certain line

    5 answers








sed






share|improve this question









New contributor




mandrake00 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




mandrake00 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited yesterday









Mr Shunz

2,99311822




2,99311822






New contributor




mandrake00 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









mandrake00

243




243




New contributor




mandrake00 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





mandrake00 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






mandrake00 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




marked as duplicate by don_crissti, Jeff Schaller, Isaac, RalfFriedl, Romeo Ninov 6 hours ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by don_crissti, Jeff Schaller, Isaac, RalfFriedl, Romeo Ninov 6 hours ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • I can do it with perl, but I am looking for a compact one liner command using sed or awk. Thank you Mr. Shunz for the format.
    – mandrake00
    yesterday












  • May be helpful? unix.stackexchange.com/questions/466798/…
    – Sparhawk
    yesterday












  • I'm always curious when I see requests for one-liners; what's wrong with a perl solution?
    – Jeff Schaller
    18 hours ago


















  • I can do it with perl, but I am looking for a compact one liner command using sed or awk. Thank you Mr. Shunz for the format.
    – mandrake00
    yesterday












  • May be helpful? unix.stackexchange.com/questions/466798/…
    – Sparhawk
    yesterday












  • I'm always curious when I see requests for one-liners; what's wrong with a perl solution?
    – Jeff Schaller
    18 hours ago
















I can do it with perl, but I am looking for a compact one liner command using sed or awk. Thank you Mr. Shunz for the format.
– mandrake00
yesterday






I can do it with perl, but I am looking for a compact one liner command using sed or awk. Thank you Mr. Shunz for the format.
– mandrake00
yesterday














May be helpful? unix.stackexchange.com/questions/466798/…
– Sparhawk
yesterday






May be helpful? unix.stackexchange.com/questions/466798/…
– Sparhawk
yesterday














I'm always curious when I see requests for one-liners; what's wrong with a perl solution?
– Jeff Schaller
18 hours ago




I'm always curious when I see requests for one-liners; what's wrong with a perl solution?
– Jeff Schaller
18 hours ago










3 Answers
3






active

oldest

votes

















up vote
2
down vote













Try



sed '/^create_test/ {N; /psfxxx/ s/^/#/}' file
#create_test -type hard -outer { 1.0000 } {
psfxxx_16_pi/psfop/deadline_north_re_0 }

#create_test -type hard -outer { 0.0000 } {
psfxxx_16_pi/psfop/deadline_south_re_1 }

create_test -type hard -outer { 1.0000 } {
vsfxxx_16_pi/psfop/deadline_north_re_0 }

create_test -type hard -outer { 0.0000 } {
vsfxxx_16_pi/psfop/deadline_south_re_1 }


When encountering "create_test" it appends the next line, and if that contains "psfxxx", it prefixes the "#".






share|improve this answer





















  • Thank you RudiC. I tried. It replaces only the first occurance in the input text file. output: #create_test -type hard -outer { 1.0000 } { psfxxx_16_pi/psfop/deadline_north_re_0 } create_ test -type hard -outer { 0.0000 } { psfxxx_16_pi/psfop/deadline_south_re_1 }
    – mandrake00
    yesterday












  • Hi! RudiC. Your fix works. Great! my input file had an issue. It had create_ in one line and test... in the next. Fixed it and it's working fine now. You are a genius.
    – mandrake00
    yesterday










  • You can see in my sample output that it replaces on matching records, and doesn't if no match. What's different in your input file? Hoppla - cross post.
    – RudiC
    yesterday












  • Thank you RudiC. It works well.
    – mandrake00
    yesterday


















up vote
0
down vote













Another sed method:



$ sed -zE 's/create_test([^n]*n[^n]*psfxxx_16_pi)/#create_test1/g' file
#create_test -type hard -outer { 1.0000 } {
psfxxx_16_pi/psfop/deadline_north_re_0 }

#create_test -type hard -outer { 0.0000 } {
psfxxx_16_pi/psfop/deadline_south_re_1





share|improve this answer




























    up vote
    0
    down vote













    If awk will do:



    awk '/psfxxx_16_pi/{prev = "#" prev} {print prev} {prev = $0} END {print}'


    Here, I print the previous line (saved in prev) for each line, and save $0 for the next iteration in prev. If the line matches, I add # to prev. At the end, print the last line.






    share|improve this answer








    New contributor




    JohnDoea is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      2
      down vote













      Try



      sed '/^create_test/ {N; /psfxxx/ s/^/#/}' file
      #create_test -type hard -outer { 1.0000 } {
      psfxxx_16_pi/psfop/deadline_north_re_0 }

      #create_test -type hard -outer { 0.0000 } {
      psfxxx_16_pi/psfop/deadline_south_re_1 }

      create_test -type hard -outer { 1.0000 } {
      vsfxxx_16_pi/psfop/deadline_north_re_0 }

      create_test -type hard -outer { 0.0000 } {
      vsfxxx_16_pi/psfop/deadline_south_re_1 }


      When encountering "create_test" it appends the next line, and if that contains "psfxxx", it prefixes the "#".






      share|improve this answer





















      • Thank you RudiC. I tried. It replaces only the first occurance in the input text file. output: #create_test -type hard -outer { 1.0000 } { psfxxx_16_pi/psfop/deadline_north_re_0 } create_ test -type hard -outer { 0.0000 } { psfxxx_16_pi/psfop/deadline_south_re_1 }
        – mandrake00
        yesterday












      • Hi! RudiC. Your fix works. Great! my input file had an issue. It had create_ in one line and test... in the next. Fixed it and it's working fine now. You are a genius.
        – mandrake00
        yesterday










      • You can see in my sample output that it replaces on matching records, and doesn't if no match. What's different in your input file? Hoppla - cross post.
        – RudiC
        yesterday












      • Thank you RudiC. It works well.
        – mandrake00
        yesterday















      up vote
      2
      down vote













      Try



      sed '/^create_test/ {N; /psfxxx/ s/^/#/}' file
      #create_test -type hard -outer { 1.0000 } {
      psfxxx_16_pi/psfop/deadline_north_re_0 }

      #create_test -type hard -outer { 0.0000 } {
      psfxxx_16_pi/psfop/deadline_south_re_1 }

      create_test -type hard -outer { 1.0000 } {
      vsfxxx_16_pi/psfop/deadline_north_re_0 }

      create_test -type hard -outer { 0.0000 } {
      vsfxxx_16_pi/psfop/deadline_south_re_1 }


      When encountering "create_test" it appends the next line, and if that contains "psfxxx", it prefixes the "#".






      share|improve this answer





















      • Thank you RudiC. I tried. It replaces only the first occurance in the input text file. output: #create_test -type hard -outer { 1.0000 } { psfxxx_16_pi/psfop/deadline_north_re_0 } create_ test -type hard -outer { 0.0000 } { psfxxx_16_pi/psfop/deadline_south_re_1 }
        – mandrake00
        yesterday












      • Hi! RudiC. Your fix works. Great! my input file had an issue. It had create_ in one line and test... in the next. Fixed it and it's working fine now. You are a genius.
        – mandrake00
        yesterday










      • You can see in my sample output that it replaces on matching records, and doesn't if no match. What's different in your input file? Hoppla - cross post.
        – RudiC
        yesterday












      • Thank you RudiC. It works well.
        – mandrake00
        yesterday













      up vote
      2
      down vote










      up vote
      2
      down vote









      Try



      sed '/^create_test/ {N; /psfxxx/ s/^/#/}' file
      #create_test -type hard -outer { 1.0000 } {
      psfxxx_16_pi/psfop/deadline_north_re_0 }

      #create_test -type hard -outer { 0.0000 } {
      psfxxx_16_pi/psfop/deadline_south_re_1 }

      create_test -type hard -outer { 1.0000 } {
      vsfxxx_16_pi/psfop/deadline_north_re_0 }

      create_test -type hard -outer { 0.0000 } {
      vsfxxx_16_pi/psfop/deadline_south_re_1 }


      When encountering "create_test" it appends the next line, and if that contains "psfxxx", it prefixes the "#".






      share|improve this answer












      Try



      sed '/^create_test/ {N; /psfxxx/ s/^/#/}' file
      #create_test -type hard -outer { 1.0000 } {
      psfxxx_16_pi/psfop/deadline_north_re_0 }

      #create_test -type hard -outer { 0.0000 } {
      psfxxx_16_pi/psfop/deadline_south_re_1 }

      create_test -type hard -outer { 1.0000 } {
      vsfxxx_16_pi/psfop/deadline_north_re_0 }

      create_test -type hard -outer { 0.0000 } {
      vsfxxx_16_pi/psfop/deadline_south_re_1 }


      When encountering "create_test" it appends the next line, and if that contains "psfxxx", it prefixes the "#".







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered yesterday









      RudiC

      3,0411211




      3,0411211












      • Thank you RudiC. I tried. It replaces only the first occurance in the input text file. output: #create_test -type hard -outer { 1.0000 } { psfxxx_16_pi/psfop/deadline_north_re_0 } create_ test -type hard -outer { 0.0000 } { psfxxx_16_pi/psfop/deadline_south_re_1 }
        – mandrake00
        yesterday












      • Hi! RudiC. Your fix works. Great! my input file had an issue. It had create_ in one line and test... in the next. Fixed it and it's working fine now. You are a genius.
        – mandrake00
        yesterday










      • You can see in my sample output that it replaces on matching records, and doesn't if no match. What's different in your input file? Hoppla - cross post.
        – RudiC
        yesterday












      • Thank you RudiC. It works well.
        – mandrake00
        yesterday


















      • Thank you RudiC. I tried. It replaces only the first occurance in the input text file. output: #create_test -type hard -outer { 1.0000 } { psfxxx_16_pi/psfop/deadline_north_re_0 } create_ test -type hard -outer { 0.0000 } { psfxxx_16_pi/psfop/deadline_south_re_1 }
        – mandrake00
        yesterday












      • Hi! RudiC. Your fix works. Great! my input file had an issue. It had create_ in one line and test... in the next. Fixed it and it's working fine now. You are a genius.
        – mandrake00
        yesterday










      • You can see in my sample output that it replaces on matching records, and doesn't if no match. What's different in your input file? Hoppla - cross post.
        – RudiC
        yesterday












      • Thank you RudiC. It works well.
        – mandrake00
        yesterday
















      Thank you RudiC. I tried. It replaces only the first occurance in the input text file. output: #create_test -type hard -outer { 1.0000 } { psfxxx_16_pi/psfop/deadline_north_re_0 } create_ test -type hard -outer { 0.0000 } { psfxxx_16_pi/psfop/deadline_south_re_1 }
      – mandrake00
      yesterday






      Thank you RudiC. I tried. It replaces only the first occurance in the input text file. output: #create_test -type hard -outer { 1.0000 } { psfxxx_16_pi/psfop/deadline_north_re_0 } create_ test -type hard -outer { 0.0000 } { psfxxx_16_pi/psfop/deadline_south_re_1 }
      – mandrake00
      yesterday














      Hi! RudiC. Your fix works. Great! my input file had an issue. It had create_ in one line and test... in the next. Fixed it and it's working fine now. You are a genius.
      – mandrake00
      yesterday




      Hi! RudiC. Your fix works. Great! my input file had an issue. It had create_ in one line and test... in the next. Fixed it and it's working fine now. You are a genius.
      – mandrake00
      yesterday












      You can see in my sample output that it replaces on matching records, and doesn't if no match. What's different in your input file? Hoppla - cross post.
      – RudiC
      yesterday






      You can see in my sample output that it replaces on matching records, and doesn't if no match. What's different in your input file? Hoppla - cross post.
      – RudiC
      yesterday














      Thank you RudiC. It works well.
      – mandrake00
      yesterday




      Thank you RudiC. It works well.
      – mandrake00
      yesterday












      up vote
      0
      down vote













      Another sed method:



      $ sed -zE 's/create_test([^n]*n[^n]*psfxxx_16_pi)/#create_test1/g' file
      #create_test -type hard -outer { 1.0000 } {
      psfxxx_16_pi/psfop/deadline_north_re_0 }

      #create_test -type hard -outer { 0.0000 } {
      psfxxx_16_pi/psfop/deadline_south_re_1





      share|improve this answer

























        up vote
        0
        down vote













        Another sed method:



        $ sed -zE 's/create_test([^n]*n[^n]*psfxxx_16_pi)/#create_test1/g' file
        #create_test -type hard -outer { 1.0000 } {
        psfxxx_16_pi/psfop/deadline_north_re_0 }

        #create_test -type hard -outer { 0.0000 } {
        psfxxx_16_pi/psfop/deadline_south_re_1





        share|improve this answer























          up vote
          0
          down vote










          up vote
          0
          down vote









          Another sed method:



          $ sed -zE 's/create_test([^n]*n[^n]*psfxxx_16_pi)/#create_test1/g' file
          #create_test -type hard -outer { 1.0000 } {
          psfxxx_16_pi/psfop/deadline_north_re_0 }

          #create_test -type hard -outer { 0.0000 } {
          psfxxx_16_pi/psfop/deadline_south_re_1





          share|improve this answer












          Another sed method:



          $ sed -zE 's/create_test([^n]*n[^n]*psfxxx_16_pi)/#create_test1/g' file
          #create_test -type hard -outer { 1.0000 } {
          psfxxx_16_pi/psfop/deadline_north_re_0 }

          #create_test -type hard -outer { 0.0000 } {
          psfxxx_16_pi/psfop/deadline_south_re_1






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered yesterday









          fpmurphy1

          2,336915




          2,336915






















              up vote
              0
              down vote













              If awk will do:



              awk '/psfxxx_16_pi/{prev = "#" prev} {print prev} {prev = $0} END {print}'


              Here, I print the previous line (saved in prev) for each line, and save $0 for the next iteration in prev. If the line matches, I add # to prev. At the end, print the last line.






              share|improve this answer








              New contributor




              JohnDoea is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.






















                up vote
                0
                down vote













                If awk will do:



                awk '/psfxxx_16_pi/{prev = "#" prev} {print prev} {prev = $0} END {print}'


                Here, I print the previous line (saved in prev) for each line, and save $0 for the next iteration in prev. If the line matches, I add # to prev. At the end, print the last line.






                share|improve this answer








                New contributor




                JohnDoea is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.




















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  If awk will do:



                  awk '/psfxxx_16_pi/{prev = "#" prev} {print prev} {prev = $0} END {print}'


                  Here, I print the previous line (saved in prev) for each line, and save $0 for the next iteration in prev. If the line matches, I add # to prev. At the end, print the last line.






                  share|improve this answer








                  New contributor




                  JohnDoea is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  If awk will do:



                  awk '/psfxxx_16_pi/{prev = "#" prev} {print prev} {prev = $0} END {print}'


                  Here, I print the previous line (saved in prev) for each line, and save $0 for the next iteration in prev. If the line matches, I add # to prev. At the end, print the last line.







                  share|improve this answer








                  New contributor




                  JohnDoea is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  share|improve this answer



                  share|improve this answer






                  New contributor




                  JohnDoea is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  answered yesterday









                  JohnDoea

                  211




                  211




                  New contributor




                  JohnDoea is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.





                  New contributor





                  JohnDoea is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






                  JohnDoea is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.















                      Popular posts from this blog

                      Entries order in /etc/network/interfaces

                      新発田市

                      Grub takes very long (several minutes) to open Menu (in Multi-Boot-System)