在Perl中解析文件 [英] Parsing a file in Perl

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

问题描述



我仅学习Perl不到2周.我是C ++程序员.
我已附上以下数据的一部分.数据在file1.txt中.我想将数据从file1.txt移到file2.txt.但是,我只想保留数字.

例如:我希望第1行看起来像这样:

Hi,

I have only been learning Perl for less than 2 weeks. I am a C++ programmer.
I have attached a portion of the data below. The data is in file1.txt. I would like to move the data from file1.txt to file2.txt. But, I only want to keep the numbers.

Eg: I want row 1 to look like this:

1       1549367 11     8       3      11      0       -12.00  6.00    -0.25   -3.00   0.00    -1.67   -12.00  6.00    -0.64



代替这个:



Instead of this:

1       Chr26   1549367 11      GGGGGGGAAGA     8       3       Transition      11      0       -12.00  6.00    -0.25   -3.00   0.00    -1.67   -12.00  6.00    -0.64



这是我到目前为止所做的(file1.txt将位于@ARGV中):



This is what I have done so far (file1.txt will be in @ARGV):

open FILE2, "+>file2.txt" or die "Cant not open file2.txt!";
my $line;
while($line = readline(ARGV))
{
        print FILE2 $line;
}



上面的代码仅将file1.txt(ARGV)的内容复制到file2.txt.
我曾尝试使用"seek"和"tell()",但为了解决上述问题,但我感到困惑:(

我也尝试过:



The code above only copies content of file1.txt (ARGV) into file2.txt.
I tried to use ‘seek’ and ‘tell()’ but, to solve my problem above but, I got confused :(

I also tried this:

Open(FILE, "file1.txt")
@theFile = ;



这会将每一行放入@file数组中.但是,我现在可以修改一行的元素吗? (我仍然是Perl的新手)

谢谢您的帮助

/../
文件部分



This puts every row in the array @the File. But, I can I now modify the elements of one row? (I’m still a novice Perl programmer)

Thank you for your help

/………………………………………………………………………………………../
The file portion

1       Chr26   1549367 11      GGGGGGGAAGA     8       3       Transition      11      0       -12.00  6.00    -0.25   -3.00   0.00    -1.67   -12.00  6.00    -0.64
1       Chr26   1549501 15      ccCctctccccctCC 12      3       Transition      3       12      -17.00  6.00    0.50    1.00    6.00    2.67    -17.00  6.00    0.93
1       Chr26   1549552 14      AagAAaaAAAagga  11      3       Transition      6       8       -31.00  6.00    -2.09   -12.00  3.00    -5.67   -31.00  6.00    -2.86
1       Chr26   1549563 14      tAAaaAAAattat^Ft        9       5       Transversion    5       9       -7.00   6.00    0.22    -64.00  4.00    -18.40  -64.00  6.00    -6.43
1       Chr26   1549726 14      TtTtctTtTtTTTT  13      1       Transition      8       6       -3.00   6.00    1.92    6.00    6.00    6.00    -3.00   6.00    2.21
2       Chr26   1549737 16      T+1Atttt+1aT+1At+1aTt+1aT+1AT+1AT+1AT+1AtT+1A^FA        15      11      Transversion    16      10      -64.00  6.00    -35.67  -64.00  6.00    -46.18  -64.00  6.00    -40.12
2       Chr26   1549815 9       CtCTTTTTT       7       2       Transition      8       1       -3.00   6.00    -0.14   -9.00   0.00    -4.50   -9.00   6.00    -1.11
1       Chr26   1549914 12      gGGGGGGGAGgg    11      1       Transition      9       3       -9.00   6.00    1.18    -4.00   -4.00   -4.00   -9.00   6.00    0.75
1       Chr26   1550018

推荐答案

行; while(


line = readline(ARGV)) { 打印FILE2
line = readline(ARGV)) { print FILE2


行; }



上面的代码仅将file1.txt(ARGV)的内容复制到file2.txt.
我曾尝试使用"seek"和"tell()",但为了解决上述问题,但我感到困惑:(

我也尝试过:



The code above only copies content of file1.txt (ARGV) into file2.txt.
I tried to use ‘seek’ and ‘tell()’ but, to solve my problem above but, I got confused :(

I also tried this:

Open(FILE, "file1.txt")
@theFile = ;



这会将每一行放入@file数组中.但是,我现在可以修改一行的元素吗? (我仍然是Perl的新手)

谢谢您的帮助

/../
文件部分



This puts every row in the array @the File. But, I can I now modify the elements of one row? (I’m still a novice Perl programmer)

Thank you for your help

/………………………………………………………………………………………../
The file portion

1       Chr26   1549367 11      GGGGGGGAAGA     8       3       Transition      11      0       -12.00  6.00    -0.25   -3.00   0.00    -1.67   -12.00  6.00    -0.64
1       Chr26   1549501 15      ccCctctccccctCC 12      3       Transition      3       12      -17.00  6.00    0.50    1.00    6.00    2.67    -17.00  6.00    0.93
1       Chr26   1549552 14      AagAAaaAAAagga  11      3       Transition      6       8       -31.00  6.00    -2.09   -12.00  3.00    -5.67   -31.00  6.00    -2.86
1       Chr26   1549563 14      tAAaaAAAattat^Ft        9       5       Transversion    5       9       -7.00   6.00    0.22    -64.00  4.00    -18.40  -64.00  6.00    -6.43
1       Chr26   1549726 14      TtTtctTtTtTTTT  13      1       Transition      8       6       -3.00   6.00    1.92    6.00    6.00    6.00    -3.00   6.00    2.21
2       Chr26   1549737 16      T+1Atttt+1aT+1At+1aTt+1aT+1AT+1AT+1AT+1AtT+1A^FA        15      11      Transversion    16      10      -64.00  6.00    -35.67  -64.00  6.00    -46.18  -64.00  6.00    -40.12
2       Chr26   1549815 9       CtCTTTTTT       7       2       Transition      8       1       -3.00   6.00    -0.14   -9.00   0.00    -4.50   -9.00   6.00    -1.11
1       Chr26   1549914 12      gGGGGGGGAGgg    11      1       Transition      9       3       -9.00   6.00    1.18    -4.00   -4.00   -4.00   -9.00   6.00    0.75
1       Chr26   1550018


这篇关于在Perl中解析文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆