#!/usr/bin/env python3
import sys
#my_file= sys.argv[1]
line_number = 0
with open(sys.argv[1]) as f:
for line in f:
line_number += 1
if (line_number % 4 == 2):
print("The length of protein %d is: %d" % (line_number,int(line.strip())))
#!/usr/bin/env python3
import sys
#my_file= sys.argv[1]
line_number = 0
with open(sys.argv[1]) as f:
for line in f:
line_number += 1
if (line_number % 4 == 2):
print("The length of protein %d is: %d" % (line_number,int(line.strip())))
$ python3 foo.py pdb.fasta_qual.dataset
The length of protein 2 is: 247
Out of curiosity, I created a test file by repeating your example 10000000 times, which left me with a 2.4G file:
$ perl -e 'open(A,"pdb.fasta_qual.dataset"); @lines=<A>; print "@lines" x 10000000' > testFile
$ ls -lh testFile
-rw-r--r-- 1 terdon terdon 2.4G Feb 7 17:06 testFile
I then tested both approaches for speed. Unsurprisingly, awk
was much faster:
$ time awk 'NR%4==2' testFile > /dev/null
real 0m6.447s
user 0m5.917s
sys 0m0.492s
$ time foo.py testFile > /dev/null
real 0m10.856s
user 0m10.111s
sys 0m0.523s