簡體   English   中英

如何從perl中的兩個txt文件讀取變量行並將其寫入另一個txt文件

[英]how to read variable lines from two txt files in perl and write them in to another txt file

我想自動執行以下Perl代碼。 這樣它將從兩個txt文件讀取可變數量的行,並將這些行寫入另一個txt文件。

我有兩個txt文件( 1st_file.txt )和( 2nd_file.txt ),我想讀取變量號。 這些文件中的行數。 我怎樣才能做到這一點? 下面給出的Perl代碼執行相同的工作,但是如果我更改txt文件,我還需要更改我的Perl代碼,這不是很有效。

因此,有人可以指導我如何為下面給出的問題編寫有效的Perl代碼嗎? 因此,如果我更改txt文件中的數據,我將獲得所需的結果,但無需更改Perl代碼。

變化在這里意味着什么? 這意味着如果我從1st_file.txt中刪除第5到7行,即207- 207 --> A_207_P2_M2A --> T_207_P2_M2A並且我也從2nd_file.txt中刪除了第4行,即P2_M2A 因此,現在刪除這些行之后,我現在也想在Perl代碼中進行更改以獲得所需的結果,因為我刪除了這些行。 但是,我想要一個Perl代碼,如果我在兩個txt文件中都進行了相應的更改,則無需進行任何修改。

Perl代碼:

use warnings;
use strict;

open (FILE1, 'g:\perl_tests\1st_file.txt');
open (FILE2, 'g:\perl_tests\2nd_file.txt');
open (FILE3, '> g:\perl_tests\3rd_file.txt');

my @speech1 = <FILE1>;
my @speech2 = <FILE2>;

print FILE3 @speech2[0..1];
print FILE3 @speech1[1..2];
print FILE3 @speech1[5..6];
print FILE3 @speech2[4..6];
print FILE3 @speech1[9..10];
print FILE3 @speech2[8..10];
print FILE3 @speech1[13..14];
print FILE3 @speech2[12..14];
print FILE3 @speech1[17..18];
print FILE3 @speech2[16..18];
print FILE3 @speech1[21..22];
print FILE3 @speech1[25..26];
print FILE3 @speech1[29..30];

1st_file.txt

153
A_153_P1_M2A_Some text is written here
T_153_P1_M2A_Some text is written here

207
A_207_P2_M2A_Some text is written here
T_207_P2_M2A_Some text is written here

48
A_48_P1_T1B_Some text is written here
T_48_P1_T1B_Some text is written here

57
A_57_P1_T2A_Some text is written here
T_57_P1_T2A_Some text is written here

167
A_167_P1_W1C_Some text is written here
T_167_P1_W1C_Some text is written here

26
A_26_P1_W2B_Some text is written here
T_26_P1_W2B_Some text is written here

183
A_183_P2_W2B_Some text is written here
T_183_P2_W2B_Some text is written here

69
A_69_P3_W2B_Some text is written here
T_69_P3_W2B_Some text is written here

2nd_file.txt

M2A
Top_M2A
P1_M2A
P2_M2A

T1B
Top_T1B
P1_T1B

T2A
Top_T2A
P1_T2A

W1C
Top_W1C
P1_W1C

W2B
Top_W2B
P1_W2B
P2_W2B
P3_W2B

3rd_file.txt(輸出:由Perl代碼生成,應為以下給出的代碼)

M2A
Top_M2A
A_153_P1_M2A_Some text is written here
T_153_P1_M2A_Some text is written here
A_207_P2_M2A_Some text is written here
T_207_P2_M2A_Some text is written here

T1B
Top_T1B
A_48_P1_T1B_Some text is written here
T_48_P1_T1B_Some text is written here

T2A
Top_T2A
A_57_P1_T2A_Some text is written here
T_57_P1_T2A_Some text is written here

W1C
Top_W1C
A_167_P1_W1C_Some text is written here
T_167_P1_W1C_Some text is written here

W2B
Top_W2B
A_26_P1_W2B_Some text is written here
T_26_P1_W2B_Some text is written here
A_183_P2_W2B_Some text is written here
T_183_P2_W2B_Some text is written here
A_69_P3_W2B_Some text is written here
T_69_P3_W2B_Some text is written here

任何人都可以指導我解決這一問題。

看來您是按照下划線后的最后一部分來對行進行分組。 它是在什么樣的順序線應印有點不清楚(例如,如果P1_M2A后,來到P2_M2A在第2個文件),但下面的代碼提供准確你給數據的預期輸出。

它首先將1st_file讀取到一個哈希中,記住該段沒有每個ID的第一行(_之后的最后一部分)。 然后,它遍歷第二個文件並在打印“標題”之后打印記住的行。 它僅在每個段落的第三行上測試id,其余行將被忽略。 如上所述,您尚未指定如何獲取ID。 如果比最后一部分重要,那么您將不得不稍微容納一些代碼。

#!/usr/bin/perl
use warnings;
use strict;

open my $F1,  '<', '1st_file.txt' or die $!;
my %hash1;
my $num;
while (<$F1>) {
    if (my ($id) = /^[AT]_[0-9]+_.+?_(.*)/) {
        $hash1{$id} .= $_;
    }
}

open my $F2,  '<', '2nd_file.txt' or die $!;
open my $OUT, '>', '3rd_file.txt' or die $!;
while (<$F2>) {
    if (!/_/ or /^Top_/) {
        print $OUT $_;
    } else {
        if (my ($id) = /_(.*)/) {
            print $OUT $hash1{$id} if exists $hash1{$id};
            delete $hash1{$id};
        }
    }
}
close $OUT or die $!;

更新以反映您更詳細的規范:

#!/usr/bin/perl
use warnings;
use strict;

open my $F1,  '<', '1st_file.txt' or die $!;
my %hash1;
my $num;
while (<$F1>) {
    if (my ($id) = /^[AT]_[0-9]+_(.*)$/) {
        $hash1{$id} .= $_;
    }
}

open my $F2,  '<', '2nd_file.txt' or die $!;
open my $OUT, '>', '3rd_file.txt' or die $!;
while (<$F2>) {
    if (!/_/ or /^Top_/) {
        print $OUT $_;
    } else {
        chomp;
        print $OUT $hash1{$_} if exists $hash1{$_};
    }
}
close $OUT or die $!;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM