Add a value to a column and print results
<p>I have a file like this</p>

<pre><code>Daniel 400 411 f
Mark 976 315 g
</code></pre>

<p>I would like to add 20 to line[2] and subtract 20 from line[1] and print new results overwriting this lines or to a new file.This is my try.</p>

<pre><code>f=open('w', 'r')
r = open('w2','a')
lines=f.readlines()
for line in lines:
  new_list = line.rstrip('\r\n').split('\t')
  q_start=int(new_list[1]) - 20
  q_end=int(new_list[2]) + 20
  # I think something is missing here, but I don't know what
  r.writelines(lines)
f.close()
r.close()
</code></pre>

<p>Expected outcome</p>

<pre><code>Daniel 380 431 f
Mark 956 335 g
</code></pre>

<pre><code>f=open('w', 'r')
r=open('w2','a')
lines=f.readlines()

for line in lines:
    new_list = line.rstrip('\r\n').split('\t')
    new_list[1] = str(int(new_list[1]) - 20)
    new_list[2] = str(int(new_list[2]) + 20)
    r.write("\t".join(new_list)+"\n")

f.close()
r.close()
</code></pre>

<p>This should works. Basically, you need split your line into segments first, then modify the parts you want, and stitch them together.</p>

python