本文介绍了Perl来移除文件的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文件看起来像:

  ATOM 2517ØVAL 160 8.337 12.679 -2.487
ATOM 2518 OXT VAL 160 7.646 12.461 -0.386
TER
ATOM 2519ñVAL 161 -14.431 5.789 -25.371
ATOM 2520 H1 VAL 161 -15.336 5.698 -25.811
ATOM 2521 H2 VAL 161 -13.416 10.529 17.708
ATOM 2522 H3 VAL 161 -14.363 9.436 18.498
ATOM 2523 CA VAL 161 4.400 9.233 16.454
ATOM 2524 HA VAL 161 3.390 9.170 16.047

我不得不删除TER,TER和行之后3行只是TER后,使文件之前,该行连续如下:

  ATOM 2517ØVAL 160 8.337 12.679 -2.487
ATOM 2519ñVAL 161 -14.431 5.789 -25.371
ATOM 2523 CA VAL 161 4.400 9.233 16.454
ATOM 2524 HA VAL 161 3.390 9.170 16.047


解决方案

我意识到我应该把它写在Perl,但现在我已经写它用Python。我张贴也无妨,因为它可能被证明是有用的,看不出任何的伤害如此说。

 #!的/ usr / bin中/ python2.7
进口SYS
进口水珠
进口OS尝试:
    DIR = sys.argv中[1]
除非IndexError:
    打印用法:+ sys.argv中[0] +DIR
    打印例如:+ sys.argv中[0] +/ home / user中/ DI​​R /
    sys.exit(1)在glob.glob文件(os.path.join(DIR,'文件* _ * MINvac.pdb')):
    鳍=打开(文件,R)
    内容= fin.readlines()
    fin.close()    因为我在范围(0,LEN(内容)):
        尝试:
            如果内容TER[我]:
                德尔内容[I]
                德尔内容[I-1]
                德尔内容[I:我+ 3]
        除非IndexError:
            打破
    FOUT =打开(文件,W)
    fout.writelines(内容)
    fout.close()

编辑:增加了对多个文件的支持,如OP希望

I have file that looks like:

ATOM 2517 O   VAL 160 8.337  12.679  -2.487
ATOM 2518 OXT VAL 160 7.646  12.461  -0.386
TER 
ATOM 2519 N   VAL 161 -14.431  5.789 -25.371
ATOM 2520 H1  VAL 161 -15.336  5.698 -25.811
ATOM 2521 H2  VAL 161 -13.416 10.529  17.708
ATOM 2522 H3  VAL 161 -14.363  9.436  18.498
ATOM 2523 CA  VAL 161   4.400  9.233  16.454
ATOM 2524 HA  VAL 161   3.390  9.170  16.047

I have to remove "TER", the line before "TER" and 3 lines after the line just after TER and make file continuous like this:

ATOM 2517 O   VAL 160   8.337 12.679  -2.487
ATOM 2519 N   VAL 161 -14.431  5.789 -25.371
ATOM 2523 CA  VAL 161   4.400  9.233  16.454
ATOM 2524 HA  VAL 161   3.390  9.170  16.047
解决方案

I realized I was supposed to write it in Perl, but now I've already written it in Python. I'm posting it anyway as it may prove to be useful, don't see any harm in that.

#!/usr/bin/python2.7
import sys
import glob
import os

try:
    dir = sys.argv[1]
except IndexError:
    print "Usage: "+sys.argv[0]+" dir"
    print "Example: "+sys.argv[0]+" /home/user/dir/"
    sys.exit(1)

for file in glob.glob(os.path.join(dir, 'File*_*MINvac.pdb')):
    fin = open(file, "r")
    content = fin.readlines()
    fin.close()

    for i in range(0, len(content)):
        try:
            if "TER" in content[i]:
                del content[i]
                del content[i-1]
                del content[i:i+3]
        except IndexError:
            break
    fout = open(file, "w")
    fout.writelines(content)
    fout.close()

Edit: Added support for multiple files, like the OP wanted.

这篇关于Perl来移除文件的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 05:48