conditional replacement of rows with a number

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











up vote
0
down vote

favorite












I have a big file containing 27 columns and nearly 6 million rows. The following is a little example of my file



head data
0.65 0.722222 1.0 0.75 0
0.35 0.277778 0.0 0.25 0
0 0.666667 0.75 0.5 0.5625
0 0.333333 0.25 0.5 0.4375


Rows are samples, and I have "2 rows per sample" (one for observation "a", and the other observation "b"). In the example above I showed data for 2 samples (rows 1 and 2 correspond to sample1 and rows 3 and 4 correspond to sample 2). I want to check if for each sample, both observations are 0, replace them with 9.
this is my desired output:



head desired
0.65 0.722222 1.0 0.75 9
0.35 0.277778 0.0 0.25 9
9 0.666667 0.75 0.5 0.5625
9 0.333333 0.25 0.5 0.4375


Any perl or python or bash (if reliable for such a big file )solution how to do it?
In the past I was just splitting the file for each sample and running the following code for each file



awk 'NR==1 split($0,a);next; NR==2 split($0,b);for(i=1;i<= NF;i++) printf("%s%s",(i==1?"":"t"),a[i]==0 && b[i]==0?9:a[i]);
printf("n");;for(i=1;i<= NF;i++) printf("%s%s",(i==1?"":"t"),a[i]==0 && b[i]==0?9:b[i]);printf("n"); '


but now I want to do it for the entire file, do not want to split it.



Thanks.



Thanks.







share|improve this question






















  • what if one row within a pair of SNP contains 0 population and the other doesn't ? what should be the output?
    – RomanPerekhrest
    Dec 23 '17 at 20:25











  • @Jesse_b, thanks for the comment, agree. I just edited my question.
    – Anna1364
    Dec 23 '17 at 22:40














up vote
0
down vote

favorite












I have a big file containing 27 columns and nearly 6 million rows. The following is a little example of my file



head data
0.65 0.722222 1.0 0.75 0
0.35 0.277778 0.0 0.25 0
0 0.666667 0.75 0.5 0.5625
0 0.333333 0.25 0.5 0.4375


Rows are samples, and I have "2 rows per sample" (one for observation "a", and the other observation "b"). In the example above I showed data for 2 samples (rows 1 and 2 correspond to sample1 and rows 3 and 4 correspond to sample 2). I want to check if for each sample, both observations are 0, replace them with 9.
this is my desired output:



head desired
0.65 0.722222 1.0 0.75 9
0.35 0.277778 0.0 0.25 9
9 0.666667 0.75 0.5 0.5625
9 0.333333 0.25 0.5 0.4375


Any perl or python or bash (if reliable for such a big file )solution how to do it?
In the past I was just splitting the file for each sample and running the following code for each file



awk 'NR==1 split($0,a);next; NR==2 split($0,b);for(i=1;i<= NF;i++) printf("%s%s",(i==1?"":"t"),a[i]==0 && b[i]==0?9:a[i]);
printf("n");;for(i=1;i<= NF;i++) printf("%s%s",(i==1?"":"t"),a[i]==0 && b[i]==0?9:b[i]);printf("n"); '


but now I want to do it for the entire file, do not want to split it.



Thanks.



Thanks.







share|improve this question






















  • what if one row within a pair of SNP contains 0 population and the other doesn't ? what should be the output?
    – RomanPerekhrest
    Dec 23 '17 at 20:25











  • @Jesse_b, thanks for the comment, agree. I just edited my question.
    – Anna1364
    Dec 23 '17 at 22:40












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a big file containing 27 columns and nearly 6 million rows. The following is a little example of my file



head data
0.65 0.722222 1.0 0.75 0
0.35 0.277778 0.0 0.25 0
0 0.666667 0.75 0.5 0.5625
0 0.333333 0.25 0.5 0.4375


Rows are samples, and I have "2 rows per sample" (one for observation "a", and the other observation "b"). In the example above I showed data for 2 samples (rows 1 and 2 correspond to sample1 and rows 3 and 4 correspond to sample 2). I want to check if for each sample, both observations are 0, replace them with 9.
this is my desired output:



head desired
0.65 0.722222 1.0 0.75 9
0.35 0.277778 0.0 0.25 9
9 0.666667 0.75 0.5 0.5625
9 0.333333 0.25 0.5 0.4375


Any perl or python or bash (if reliable for such a big file )solution how to do it?
In the past I was just splitting the file for each sample and running the following code for each file



awk 'NR==1 split($0,a);next; NR==2 split($0,b);for(i=1;i<= NF;i++) printf("%s%s",(i==1?"":"t"),a[i]==0 && b[i]==0?9:a[i]);
printf("n");;for(i=1;i<= NF;i++) printf("%s%s",(i==1?"":"t"),a[i]==0 && b[i]==0?9:b[i]);printf("n"); '


but now I want to do it for the entire file, do not want to split it.



Thanks.



Thanks.







share|improve this question














I have a big file containing 27 columns and nearly 6 million rows. The following is a little example of my file



head data
0.65 0.722222 1.0 0.75 0
0.35 0.277778 0.0 0.25 0
0 0.666667 0.75 0.5 0.5625
0 0.333333 0.25 0.5 0.4375


Rows are samples, and I have "2 rows per sample" (one for observation "a", and the other observation "b"). In the example above I showed data for 2 samples (rows 1 and 2 correspond to sample1 and rows 3 and 4 correspond to sample 2). I want to check if for each sample, both observations are 0, replace them with 9.
this is my desired output:



head desired
0.65 0.722222 1.0 0.75 9
0.35 0.277778 0.0 0.25 9
9 0.666667 0.75 0.5 0.5625
9 0.333333 0.25 0.5 0.4375


Any perl or python or bash (if reliable for such a big file )solution how to do it?
In the past I was just splitting the file for each sample and running the following code for each file



awk 'NR==1 split($0,a);next; NR==2 split($0,b);for(i=1;i<= NF;i++) printf("%s%s",(i==1?"":"t"),a[i]==0 && b[i]==0?9:a[i]);
printf("n");;for(i=1;i<= NF;i++) printf("%s%s",(i==1?"":"t"),a[i]==0 && b[i]==0?9:b[i]);printf("n"); '


but now I want to do it for the entire file, do not want to split it.



Thanks.



Thanks.









share|improve this question













share|improve this question




share|improve this question








edited Dec 24 '17 at 20:03

























asked Dec 23 '17 at 19:59









Anna1364

421110




421110











  • what if one row within a pair of SNP contains 0 population and the other doesn't ? what should be the output?
    – RomanPerekhrest
    Dec 23 '17 at 20:25











  • @Jesse_b, thanks for the comment, agree. I just edited my question.
    – Anna1364
    Dec 23 '17 at 22:40
















  • what if one row within a pair of SNP contains 0 population and the other doesn't ? what should be the output?
    – RomanPerekhrest
    Dec 23 '17 at 20:25











  • @Jesse_b, thanks for the comment, agree. I just edited my question.
    – Anna1364
    Dec 23 '17 at 22:40















what if one row within a pair of SNP contains 0 population and the other doesn't ? what should be the output?
– RomanPerekhrest
Dec 23 '17 at 20:25





what if one row within a pair of SNP contains 0 population and the other doesn't ? what should be the output?
– RomanPerekhrest
Dec 23 '17 at 20:25













@Jesse_b, thanks for the comment, agree. I just edited my question.
– Anna1364
Dec 23 '17 at 22:40




@Jesse_b, thanks for the comment, agree. I just edited my question.
– Anna1364
Dec 23 '17 at 22:40










2 Answers
2






active

oldest

votes

















up vote
0
down vote













Here is how I would do this in Python:



#!/usr/bin/env python3

firstLineZero = False

# Open the file for reading
with open("biodata2", "r") as inFile:
for line in inFile:
# Check if last value in line is 0
if not firstLineZero and line.split()[-1] == "0":
# Save this line, and set a boolean
firstLineZero = True
prevLine = line
elif firstLineZero and line.split()[-1] == "0":
# Now we know that both lines end with 0.
# Change the final value to 9 in both lines...
prevLineSplit = prevLine.split()
thisLineSplit = line.split()
prevLineSplit[-1] = "9"
thisLineSplit[-1] = "9"
prevLine = "t".join(prevLineSplit)
thisLine = "t".join(thisLineSplit)
print(prevLine)
print(thisLine)
# Reset boolean
firstLineZero = False
# Reset prevLine
prevLine = ""
else:
print(line, end="")

# If we have a 'trailing' saved line, print that
if prevLine is not None:
print(prevLine, end="")


Execution example, with a couple of more lines to provide a POC.



Data:



cat biodata2 
0.65 0.722222 1.0 0.75 0
0.35 0.277778 0.0 0.25 0
0 0.666667 0.75 0.5 0.5625
0 0.333333 0.25 0.5 0.4375
0 0.333333 0.25 0.5 1
0 0.333333 0.25 0.5 0


Execution:



./readBioData.py
0.65 0.722222 1.0 0.75 9
0.35 0.277778 0.0 0.25 9
0 0.666667 0.75 0.5 0.5625
0 0.333333 0.25 0.5 0.4375
0 0.333333 0.25 0.5 1
0 0.333333 0.25 0.5 0


Obviously, if you want to save this to a file instead of printing to stdout, you will have to alter the print statements into write and set a file for writing.



Like so:



#!/usr/bin/env python3

firstLineZero = False
outFile = open("bioDataOut.txt", "w")

# Open the file for reading
with open("biodata2", "r") as inFile:
for line in inFile:
# Check if last value in line is 0
if not firstLineZero and line.split()[-1] == "0":
# Save this line, and set a boolean
firstLineZero = True
prevLine = line
elif firstLineZero and line.split()[-1] == "0":
# Now we know that both lines end with 0.
# Change the final value to 9 in both lines...
prevLineSplit = prevLine.split()
thisLineSplit = line.split()
prevLineSplit[-1] = "9"
thisLineSplit[-1] = "9"
prevLine = "t".join(prevLineSplit)
thisLine = "t".join(thisLineSplit)
outFile.write(prevLine + "n")
outFile.write(thisLine + "n")
# Reset boolean
firstLineZero = False
# Reset prevLine
prevLine = ""
else:
outFile.write(line)

# If we have a 'trailing' saved line, print that
if prevLine is not None:
outFile.write(prevLine)

outFile.close()


Then you can do:



./readBioDataSaveToFile.py
cat bioDataOut.txt
0.65 0.722222 1.0 0.75 9
0.35 0.277778 0.0 0.25 9
0 0.666667 0.75 0.5 0.5625
0 0.333333 0.25 0.5 0.4375
0 0.333333 0.25 0.5 1
0 0.333333 0.25 0.5 0





share|improve this answer





























    up vote
    -1
    down vote













    The trick to handle pairs of lines is to merge them:



    paste - - < paired_file


    Then you can test/manipulate the fields with awk ($1==0 && $6==0, etc.)






    share|improve this answer




















    • ,this is not an answer, it could be better just to suggest your view as a comment here!
      – Anna1364
      Dec 24 '17 at 2:25










    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    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: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f412713%2fconditional-replacement-of-rows-with-a-number%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    Here is how I would do this in Python:



    #!/usr/bin/env python3

    firstLineZero = False

    # Open the file for reading
    with open("biodata2", "r") as inFile:
    for line in inFile:
    # Check if last value in line is 0
    if not firstLineZero and line.split()[-1] == "0":
    # Save this line, and set a boolean
    firstLineZero = True
    prevLine = line
    elif firstLineZero and line.split()[-1] == "0":
    # Now we know that both lines end with 0.
    # Change the final value to 9 in both lines...
    prevLineSplit = prevLine.split()
    thisLineSplit = line.split()
    prevLineSplit[-1] = "9"
    thisLineSplit[-1] = "9"
    prevLine = "t".join(prevLineSplit)
    thisLine = "t".join(thisLineSplit)
    print(prevLine)
    print(thisLine)
    # Reset boolean
    firstLineZero = False
    # Reset prevLine
    prevLine = ""
    else:
    print(line, end="")

    # If we have a 'trailing' saved line, print that
    if prevLine is not None:
    print(prevLine, end="")


    Execution example, with a couple of more lines to provide a POC.



    Data:



    cat biodata2 
    0.65 0.722222 1.0 0.75 0
    0.35 0.277778 0.0 0.25 0
    0 0.666667 0.75 0.5 0.5625
    0 0.333333 0.25 0.5 0.4375
    0 0.333333 0.25 0.5 1
    0 0.333333 0.25 0.5 0


    Execution:



    ./readBioData.py
    0.65 0.722222 1.0 0.75 9
    0.35 0.277778 0.0 0.25 9
    0 0.666667 0.75 0.5 0.5625
    0 0.333333 0.25 0.5 0.4375
    0 0.333333 0.25 0.5 1
    0 0.333333 0.25 0.5 0


    Obviously, if you want to save this to a file instead of printing to stdout, you will have to alter the print statements into write and set a file for writing.



    Like so:



    #!/usr/bin/env python3

    firstLineZero = False
    outFile = open("bioDataOut.txt", "w")

    # Open the file for reading
    with open("biodata2", "r") as inFile:
    for line in inFile:
    # Check if last value in line is 0
    if not firstLineZero and line.split()[-1] == "0":
    # Save this line, and set a boolean
    firstLineZero = True
    prevLine = line
    elif firstLineZero and line.split()[-1] == "0":
    # Now we know that both lines end with 0.
    # Change the final value to 9 in both lines...
    prevLineSplit = prevLine.split()
    thisLineSplit = line.split()
    prevLineSplit[-1] = "9"
    thisLineSplit[-1] = "9"
    prevLine = "t".join(prevLineSplit)
    thisLine = "t".join(thisLineSplit)
    outFile.write(prevLine + "n")
    outFile.write(thisLine + "n")
    # Reset boolean
    firstLineZero = False
    # Reset prevLine
    prevLine = ""
    else:
    outFile.write(line)

    # If we have a 'trailing' saved line, print that
    if prevLine is not None:
    outFile.write(prevLine)

    outFile.close()


    Then you can do:



    ./readBioDataSaveToFile.py
    cat bioDataOut.txt
    0.65 0.722222 1.0 0.75 9
    0.35 0.277778 0.0 0.25 9
    0 0.666667 0.75 0.5 0.5625
    0 0.333333 0.25 0.5 0.4375
    0 0.333333 0.25 0.5 1
    0 0.333333 0.25 0.5 0





    share|improve this answer


























      up vote
      0
      down vote













      Here is how I would do this in Python:



      #!/usr/bin/env python3

      firstLineZero = False

      # Open the file for reading
      with open("biodata2", "r") as inFile:
      for line in inFile:
      # Check if last value in line is 0
      if not firstLineZero and line.split()[-1] == "0":
      # Save this line, and set a boolean
      firstLineZero = True
      prevLine = line
      elif firstLineZero and line.split()[-1] == "0":
      # Now we know that both lines end with 0.
      # Change the final value to 9 in both lines...
      prevLineSplit = prevLine.split()
      thisLineSplit = line.split()
      prevLineSplit[-1] = "9"
      thisLineSplit[-1] = "9"
      prevLine = "t".join(prevLineSplit)
      thisLine = "t".join(thisLineSplit)
      print(prevLine)
      print(thisLine)
      # Reset boolean
      firstLineZero = False
      # Reset prevLine
      prevLine = ""
      else:
      print(line, end="")

      # If we have a 'trailing' saved line, print that
      if prevLine is not None:
      print(prevLine, end="")


      Execution example, with a couple of more lines to provide a POC.



      Data:



      cat biodata2 
      0.65 0.722222 1.0 0.75 0
      0.35 0.277778 0.0 0.25 0
      0 0.666667 0.75 0.5 0.5625
      0 0.333333 0.25 0.5 0.4375
      0 0.333333 0.25 0.5 1
      0 0.333333 0.25 0.5 0


      Execution:



      ./readBioData.py
      0.65 0.722222 1.0 0.75 9
      0.35 0.277778 0.0 0.25 9
      0 0.666667 0.75 0.5 0.5625
      0 0.333333 0.25 0.5 0.4375
      0 0.333333 0.25 0.5 1
      0 0.333333 0.25 0.5 0


      Obviously, if you want to save this to a file instead of printing to stdout, you will have to alter the print statements into write and set a file for writing.



      Like so:



      #!/usr/bin/env python3

      firstLineZero = False
      outFile = open("bioDataOut.txt", "w")

      # Open the file for reading
      with open("biodata2", "r") as inFile:
      for line in inFile:
      # Check if last value in line is 0
      if not firstLineZero and line.split()[-1] == "0":
      # Save this line, and set a boolean
      firstLineZero = True
      prevLine = line
      elif firstLineZero and line.split()[-1] == "0":
      # Now we know that both lines end with 0.
      # Change the final value to 9 in both lines...
      prevLineSplit = prevLine.split()
      thisLineSplit = line.split()
      prevLineSplit[-1] = "9"
      thisLineSplit[-1] = "9"
      prevLine = "t".join(prevLineSplit)
      thisLine = "t".join(thisLineSplit)
      outFile.write(prevLine + "n")
      outFile.write(thisLine + "n")
      # Reset boolean
      firstLineZero = False
      # Reset prevLine
      prevLine = ""
      else:
      outFile.write(line)

      # If we have a 'trailing' saved line, print that
      if prevLine is not None:
      outFile.write(prevLine)

      outFile.close()


      Then you can do:



      ./readBioDataSaveToFile.py
      cat bioDataOut.txt
      0.65 0.722222 1.0 0.75 9
      0.35 0.277778 0.0 0.25 9
      0 0.666667 0.75 0.5 0.5625
      0 0.333333 0.25 0.5 0.4375
      0 0.333333 0.25 0.5 1
      0 0.333333 0.25 0.5 0





      share|improve this answer
























        up vote
        0
        down vote










        up vote
        0
        down vote









        Here is how I would do this in Python:



        #!/usr/bin/env python3

        firstLineZero = False

        # Open the file for reading
        with open("biodata2", "r") as inFile:
        for line in inFile:
        # Check if last value in line is 0
        if not firstLineZero and line.split()[-1] == "0":
        # Save this line, and set a boolean
        firstLineZero = True
        prevLine = line
        elif firstLineZero and line.split()[-1] == "0":
        # Now we know that both lines end with 0.
        # Change the final value to 9 in both lines...
        prevLineSplit = prevLine.split()
        thisLineSplit = line.split()
        prevLineSplit[-1] = "9"
        thisLineSplit[-1] = "9"
        prevLine = "t".join(prevLineSplit)
        thisLine = "t".join(thisLineSplit)
        print(prevLine)
        print(thisLine)
        # Reset boolean
        firstLineZero = False
        # Reset prevLine
        prevLine = ""
        else:
        print(line, end="")

        # If we have a 'trailing' saved line, print that
        if prevLine is not None:
        print(prevLine, end="")


        Execution example, with a couple of more lines to provide a POC.



        Data:



        cat biodata2 
        0.65 0.722222 1.0 0.75 0
        0.35 0.277778 0.0 0.25 0
        0 0.666667 0.75 0.5 0.5625
        0 0.333333 0.25 0.5 0.4375
        0 0.333333 0.25 0.5 1
        0 0.333333 0.25 0.5 0


        Execution:



        ./readBioData.py
        0.65 0.722222 1.0 0.75 9
        0.35 0.277778 0.0 0.25 9
        0 0.666667 0.75 0.5 0.5625
        0 0.333333 0.25 0.5 0.4375
        0 0.333333 0.25 0.5 1
        0 0.333333 0.25 0.5 0


        Obviously, if you want to save this to a file instead of printing to stdout, you will have to alter the print statements into write and set a file for writing.



        Like so:



        #!/usr/bin/env python3

        firstLineZero = False
        outFile = open("bioDataOut.txt", "w")

        # Open the file for reading
        with open("biodata2", "r") as inFile:
        for line in inFile:
        # Check if last value in line is 0
        if not firstLineZero and line.split()[-1] == "0":
        # Save this line, and set a boolean
        firstLineZero = True
        prevLine = line
        elif firstLineZero and line.split()[-1] == "0":
        # Now we know that both lines end with 0.
        # Change the final value to 9 in both lines...
        prevLineSplit = prevLine.split()
        thisLineSplit = line.split()
        prevLineSplit[-1] = "9"
        thisLineSplit[-1] = "9"
        prevLine = "t".join(prevLineSplit)
        thisLine = "t".join(thisLineSplit)
        outFile.write(prevLine + "n")
        outFile.write(thisLine + "n")
        # Reset boolean
        firstLineZero = False
        # Reset prevLine
        prevLine = ""
        else:
        outFile.write(line)

        # If we have a 'trailing' saved line, print that
        if prevLine is not None:
        outFile.write(prevLine)

        outFile.close()


        Then you can do:



        ./readBioDataSaveToFile.py
        cat bioDataOut.txt
        0.65 0.722222 1.0 0.75 9
        0.35 0.277778 0.0 0.25 9
        0 0.666667 0.75 0.5 0.5625
        0 0.333333 0.25 0.5 0.4375
        0 0.333333 0.25 0.5 1
        0 0.333333 0.25 0.5 0





        share|improve this answer














        Here is how I would do this in Python:



        #!/usr/bin/env python3

        firstLineZero = False

        # Open the file for reading
        with open("biodata2", "r") as inFile:
        for line in inFile:
        # Check if last value in line is 0
        if not firstLineZero and line.split()[-1] == "0":
        # Save this line, and set a boolean
        firstLineZero = True
        prevLine = line
        elif firstLineZero and line.split()[-1] == "0":
        # Now we know that both lines end with 0.
        # Change the final value to 9 in both lines...
        prevLineSplit = prevLine.split()
        thisLineSplit = line.split()
        prevLineSplit[-1] = "9"
        thisLineSplit[-1] = "9"
        prevLine = "t".join(prevLineSplit)
        thisLine = "t".join(thisLineSplit)
        print(prevLine)
        print(thisLine)
        # Reset boolean
        firstLineZero = False
        # Reset prevLine
        prevLine = ""
        else:
        print(line, end="")

        # If we have a 'trailing' saved line, print that
        if prevLine is not None:
        print(prevLine, end="")


        Execution example, with a couple of more lines to provide a POC.



        Data:



        cat biodata2 
        0.65 0.722222 1.0 0.75 0
        0.35 0.277778 0.0 0.25 0
        0 0.666667 0.75 0.5 0.5625
        0 0.333333 0.25 0.5 0.4375
        0 0.333333 0.25 0.5 1
        0 0.333333 0.25 0.5 0


        Execution:



        ./readBioData.py
        0.65 0.722222 1.0 0.75 9
        0.35 0.277778 0.0 0.25 9
        0 0.666667 0.75 0.5 0.5625
        0 0.333333 0.25 0.5 0.4375
        0 0.333333 0.25 0.5 1
        0 0.333333 0.25 0.5 0


        Obviously, if you want to save this to a file instead of printing to stdout, you will have to alter the print statements into write and set a file for writing.



        Like so:



        #!/usr/bin/env python3

        firstLineZero = False
        outFile = open("bioDataOut.txt", "w")

        # Open the file for reading
        with open("biodata2", "r") as inFile:
        for line in inFile:
        # Check if last value in line is 0
        if not firstLineZero and line.split()[-1] == "0":
        # Save this line, and set a boolean
        firstLineZero = True
        prevLine = line
        elif firstLineZero and line.split()[-1] == "0":
        # Now we know that both lines end with 0.
        # Change the final value to 9 in both lines...
        prevLineSplit = prevLine.split()
        thisLineSplit = line.split()
        prevLineSplit[-1] = "9"
        thisLineSplit[-1] = "9"
        prevLine = "t".join(prevLineSplit)
        thisLine = "t".join(thisLineSplit)
        outFile.write(prevLine + "n")
        outFile.write(thisLine + "n")
        # Reset boolean
        firstLineZero = False
        # Reset prevLine
        prevLine = ""
        else:
        outFile.write(line)

        # If we have a 'trailing' saved line, print that
        if prevLine is not None:
        outFile.write(prevLine)

        outFile.close()


        Then you can do:



        ./readBioDataSaveToFile.py
        cat bioDataOut.txt
        0.65 0.722222 1.0 0.75 9
        0.35 0.277778 0.0 0.25 9
        0 0.666667 0.75 0.5 0.5625
        0 0.333333 0.25 0.5 0.4375
        0 0.333333 0.25 0.5 1
        0 0.333333 0.25 0.5 0






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 25 '17 at 14:25

























        answered Dec 25 '17 at 14:10









        maulinglawns

        5,4932822




        5,4932822






















            up vote
            -1
            down vote













            The trick to handle pairs of lines is to merge them:



            paste - - < paired_file


            Then you can test/manipulate the fields with awk ($1==0 && $6==0, etc.)






            share|improve this answer




















            • ,this is not an answer, it could be better just to suggest your view as a comment here!
              – Anna1364
              Dec 24 '17 at 2:25














            up vote
            -1
            down vote













            The trick to handle pairs of lines is to merge them:



            paste - - < paired_file


            Then you can test/manipulate the fields with awk ($1==0 && $6==0, etc.)






            share|improve this answer




















            • ,this is not an answer, it could be better just to suggest your view as a comment here!
              – Anna1364
              Dec 24 '17 at 2:25












            up vote
            -1
            down vote










            up vote
            -1
            down vote









            The trick to handle pairs of lines is to merge them:



            paste - - < paired_file


            Then you can test/manipulate the fields with awk ($1==0 && $6==0, etc.)






            share|improve this answer












            The trick to handle pairs of lines is to merge them:



            paste - - < paired_file


            Then you can test/manipulate the fields with awk ($1==0 && $6==0, etc.)







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Dec 23 '17 at 22:54









            n.caillou

            29216




            29216











            • ,this is not an answer, it could be better just to suggest your view as a comment here!
              – Anna1364
              Dec 24 '17 at 2:25
















            • ,this is not an answer, it could be better just to suggest your view as a comment here!
              – Anna1364
              Dec 24 '17 at 2:25















            ,this is not an answer, it could be better just to suggest your view as a comment here!
            – Anna1364
            Dec 24 '17 at 2:25




            ,this is not an answer, it could be better just to suggest your view as a comment here!
            – Anna1364
            Dec 24 '17 at 2:25












             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f412713%2fconditional-replacement-of-rows-with-a-number%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay