Find line matching an expression, copy entire line, insert in new line and replace the expression











up vote
-2
down vote

favorite












I have several files (yml, tf, xml) for which I need to find a string i.e. var1, and then insert a new line with foo2, the rest of the line is unchanged.
Example



variable "my_vars" {
type = "map"

default = {
var1 = "10.48.225.160/28"
var2 = "10.48.225.160/28"
var3 = "10.48.225.160/28"
var4 = "10.48.225.160/28"
}
}


I tried the code below but I need the edit in place.



import sys
import string

def find(substr, replstr, infile):
f = open(infile,"rw")
lines = f.readlines()
for i in range(len(lines)):
if substr in lines[i]:
j = string.replace(lines[i], substr, replstr)
lines.insert(i + 1, j)
print "n".join(lines)

old_env = sys.argv[1]
new_env = sys.argv[2]
file = sys.argv[3]

find(old_env, new_env, file)









share|improve this question









New contributor




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
















  • 1




    And what have you tired till now ? What isnt working for you ? Can you show us the attempts you've done or are you expecting a straight up answer here ?
    – Jason Stanley
    Nov 7 at 22:49










  • @JasonStanley, I have tried sed, awk and python. I need the edit in place and can't find a way to do it.
    – kinihun
    Nov 8 at 10:07















up vote
-2
down vote

favorite












I have several files (yml, tf, xml) for which I need to find a string i.e. var1, and then insert a new line with foo2, the rest of the line is unchanged.
Example



variable "my_vars" {
type = "map"

default = {
var1 = "10.48.225.160/28"
var2 = "10.48.225.160/28"
var3 = "10.48.225.160/28"
var4 = "10.48.225.160/28"
}
}


I tried the code below but I need the edit in place.



import sys
import string

def find(substr, replstr, infile):
f = open(infile,"rw")
lines = f.readlines()
for i in range(len(lines)):
if substr in lines[i]:
j = string.replace(lines[i], substr, replstr)
lines.insert(i + 1, j)
print "n".join(lines)

old_env = sys.argv[1]
new_env = sys.argv[2]
file = sys.argv[3]

find(old_env, new_env, file)









share|improve this question









New contributor




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
















  • 1




    And what have you tired till now ? What isnt working for you ? Can you show us the attempts you've done or are you expecting a straight up answer here ?
    – Jason Stanley
    Nov 7 at 22:49










  • @JasonStanley, I have tried sed, awk and python. I need the edit in place and can't find a way to do it.
    – kinihun
    Nov 8 at 10:07













up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











I have several files (yml, tf, xml) for which I need to find a string i.e. var1, and then insert a new line with foo2, the rest of the line is unchanged.
Example



variable "my_vars" {
type = "map"

default = {
var1 = "10.48.225.160/28"
var2 = "10.48.225.160/28"
var3 = "10.48.225.160/28"
var4 = "10.48.225.160/28"
}
}


I tried the code below but I need the edit in place.



import sys
import string

def find(substr, replstr, infile):
f = open(infile,"rw")
lines = f.readlines()
for i in range(len(lines)):
if substr in lines[i]:
j = string.replace(lines[i], substr, replstr)
lines.insert(i + 1, j)
print "n".join(lines)

old_env = sys.argv[1]
new_env = sys.argv[2]
file = sys.argv[3]

find(old_env, new_env, file)









share|improve this question









New contributor




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











I have several files (yml, tf, xml) for which I need to find a string i.e. var1, and then insert a new line with foo2, the rest of the line is unchanged.
Example



variable "my_vars" {
type = "map"

default = {
var1 = "10.48.225.160/28"
var2 = "10.48.225.160/28"
var3 = "10.48.225.160/28"
var4 = "10.48.225.160/28"
}
}


I tried the code below but I need the edit in place.



import sys
import string

def find(substr, replstr, infile):
f = open(infile,"rw")
lines = f.readlines()
for i in range(len(lines)):
if substr in lines[i]:
j = string.replace(lines[i], substr, replstr)
lines.insert(i + 1, j)
print "n".join(lines)

old_env = sys.argv[1]
new_env = sys.argv[2]
file = sys.argv[3]

find(old_env, new_env, file)






awk sed






share|improve this question









New contributor




kinihun 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




kinihun 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 Nov 8 at 10:07





















New contributor




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









asked Nov 7 at 22:46









kinihun

12




12




New contributor




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





New contributor





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






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








  • 1




    And what have you tired till now ? What isnt working for you ? Can you show us the attempts you've done or are you expecting a straight up answer here ?
    – Jason Stanley
    Nov 7 at 22:49










  • @JasonStanley, I have tried sed, awk and python. I need the edit in place and can't find a way to do it.
    – kinihun
    Nov 8 at 10:07














  • 1




    And what have you tired till now ? What isnt working for you ? Can you show us the attempts you've done or are you expecting a straight up answer here ?
    – Jason Stanley
    Nov 7 at 22:49










  • @JasonStanley, I have tried sed, awk and python. I need the edit in place and can't find a way to do it.
    – kinihun
    Nov 8 at 10:07








1




1




And what have you tired till now ? What isnt working for you ? Can you show us the attempts you've done or are you expecting a straight up answer here ?
– Jason Stanley
Nov 7 at 22:49




And what have you tired till now ? What isnt working for you ? Can you show us the attempts you've done or are you expecting a straight up answer here ?
– Jason Stanley
Nov 7 at 22:49












@JasonStanley, I have tried sed, awk and python. I need the edit in place and can't find a way to do it.
– kinihun
Nov 8 at 10:07




@JasonStanley, I have tried sed, awk and python. I need the edit in place and can't find a way to do it.
– kinihun
Nov 8 at 10:07












1 Answer
1






active

oldest

votes

















up vote
0
down vote













import sys
import string

def find(substr, replstr, infile):
f = open(infile,"r")
lines = f.readlines()

for i in range(len(lines)):
if substr in lines[i]:
j = string.replace(lines[i], substr, replstr)
lines.insert(i + 1, j)
print "".join(lines)
f.close()

f = open(infile,"w")
k = "".join(lines)
f.writelines(k)
f.close()

old_env = sys.argv[1]
new_env = sys.argv[2]
file = sys.argv[3]

find(old_env, new_env, file)


The one caveat is there is a match on the last line of the file, the iterator will miss this.






share|improve this answer








New contributor




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


















    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',
    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
    });


    }
    });






    kinihun is a new contributor. Be nice, and check out our Code of Conduct.










     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53199063%2ffind-line-matching-an-expression-copy-entire-line-insert-in-new-line-and-repla%23new-answer', 'question_page');
    }
    );

    Post as a guest
































    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    import sys
    import string

    def find(substr, replstr, infile):
    f = open(infile,"r")
    lines = f.readlines()

    for i in range(len(lines)):
    if substr in lines[i]:
    j = string.replace(lines[i], substr, replstr)
    lines.insert(i + 1, j)
    print "".join(lines)
    f.close()

    f = open(infile,"w")
    k = "".join(lines)
    f.writelines(k)
    f.close()

    old_env = sys.argv[1]
    new_env = sys.argv[2]
    file = sys.argv[3]

    find(old_env, new_env, file)


    The one caveat is there is a match on the last line of the file, the iterator will miss this.






    share|improve this answer








    New contributor




    kinihun 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













      import sys
      import string

      def find(substr, replstr, infile):
      f = open(infile,"r")
      lines = f.readlines()

      for i in range(len(lines)):
      if substr in lines[i]:
      j = string.replace(lines[i], substr, replstr)
      lines.insert(i + 1, j)
      print "".join(lines)
      f.close()

      f = open(infile,"w")
      k = "".join(lines)
      f.writelines(k)
      f.close()

      old_env = sys.argv[1]
      new_env = sys.argv[2]
      file = sys.argv[3]

      find(old_env, new_env, file)


      The one caveat is there is a match on the last line of the file, the iterator will miss this.






      share|improve this answer








      New contributor




      kinihun 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









        import sys
        import string

        def find(substr, replstr, infile):
        f = open(infile,"r")
        lines = f.readlines()

        for i in range(len(lines)):
        if substr in lines[i]:
        j = string.replace(lines[i], substr, replstr)
        lines.insert(i + 1, j)
        print "".join(lines)
        f.close()

        f = open(infile,"w")
        k = "".join(lines)
        f.writelines(k)
        f.close()

        old_env = sys.argv[1]
        new_env = sys.argv[2]
        file = sys.argv[3]

        find(old_env, new_env, file)


        The one caveat is there is a match on the last line of the file, the iterator will miss this.






        share|improve this answer








        New contributor




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









        import sys
        import string

        def find(substr, replstr, infile):
        f = open(infile,"r")
        lines = f.readlines()

        for i in range(len(lines)):
        if substr in lines[i]:
        j = string.replace(lines[i], substr, replstr)
        lines.insert(i + 1, j)
        print "".join(lines)
        f.close()

        f = open(infile,"w")
        k = "".join(lines)
        f.writelines(k)
        f.close()

        old_env = sys.argv[1]
        new_env = sys.argv[2]
        file = sys.argv[3]

        find(old_env, new_env, file)


        The one caveat is there is a match on the last line of the file, the iterator will miss this.







        share|improve this answer








        New contributor




        kinihun 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




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









        answered Nov 8 at 21:18









        kinihun

        12




        12




        New contributor




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





        New contributor





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






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






















            kinihun is a new contributor. Be nice, and check out our Code of Conduct.










             

            draft saved


            draft discarded


















            kinihun is a new contributor. Be nice, and check out our Code of Conduct.













            kinihun is a new contributor. Be nice, and check out our Code of Conduct.












            kinihun is a new contributor. Be nice, and check out our Code of Conduct.















             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53199063%2ffind-line-matching-an-expression-copy-entire-line-insert-in-new-line-and-repla%23new-answer', 'question_page');
            }
            );

            Post as a guest




















































































            Popular posts from this blog

            Landwehr

            Reims

            Schenkenzell