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

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











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 yesterday


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
    2 days ago











  • May be helpful? unix.stackexchange.com/questions/466798/…
    – Sparhawk
    2 days ago











  • I'm always curious when I see requests for one-liners; what's wrong with a perl solution?
    – Jeff Schaller
    2 days 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 yesterday


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
    2 days ago











  • May be helpful? unix.stackexchange.com/questions/466798/…
    – Sparhawk
    2 days ago











  • I'm always curious when I see requests for one-liners; what's wrong with a perl solution?
    – Jeff Schaller
    2 days 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 2 days ago









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 2 days ago









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 yesterday


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 yesterday


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
    2 days ago











  • May be helpful? unix.stackexchange.com/questions/466798/…
    – Sparhawk
    2 days ago











  • I'm always curious when I see requests for one-liners; what's wrong with a perl solution?
    – Jeff Schaller
    2 days 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
    2 days ago











  • May be helpful? unix.stackexchange.com/questions/466798/…
    – Sparhawk
    2 days ago











  • I'm always curious when I see requests for one-liners; what's wrong with a perl solution?
    – Jeff Schaller
    2 days 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
2 days 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
2 days ago













May be helpful? unix.stackexchange.com/questions/466798/…
– Sparhawk
2 days ago





May be helpful? unix.stackexchange.com/questions/466798/…
– Sparhawk
2 days ago













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




I'm always curious when I see requests for one-liners; what's wrong with a perl solution?
– Jeff Schaller
2 days 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
    2 days ago











  • 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
    2 days ago










  • 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
    2 days ago











  • Thank you RudiC. It works well.
    – mandrake00
    2 days ago

















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
        2 days ago











      • 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
        2 days ago










      • 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
        2 days ago











      • Thank you RudiC. It works well.
        – mandrake00
        2 days ago














      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
        2 days ago











      • 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
        2 days ago










      • 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
        2 days ago











      • Thank you RudiC. It works well.
        – mandrake00
        2 days ago












      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 2 days ago









      RudiC

      3,0611211




      3,0611211











      • 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
        2 days ago











      • 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
        2 days ago










      • 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
        2 days ago











      • Thank you RudiC. It works well.
        – mandrake00
        2 days ago
















      • 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
        2 days ago











      • 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
        2 days ago










      • 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
        2 days ago











      • Thank you RudiC. It works well.
        – mandrake00
        2 days ago















      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
      2 days ago





      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
      2 days ago













      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
      2 days ago




      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
      2 days ago












      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
      2 days ago





      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
      2 days ago













      Thank you RudiC. It works well.
      – mandrake00
      2 days ago




      Thank you RudiC. It works well.
      – mandrake00
      2 days ago












      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 2 days ago









          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 2 days ago









                  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

                      Peggy Mitchell

                      Palaiologos

                      The Forum (Inglewood, California)