如果两行以相同的表达式开头,则合并文本文件中的两行

Merging two lines in a text file if they start with the same expression

我有一个这样的文件

>Unc14086 
AGAGUUUGAU 
>Unc35443
GCACGAGAAA

因此,每 n(n 可能不同)行下一行以“>”开头,即新信息块的开始。

我有另一个制表符分隔的文件:

Unc14806 InformationalTextExample
Unc35433 InformationalTextExampleII

我的目标是使用在第一个文件中以“>”开头的行中找到的信息来解析第二个文件。每当出现匹配对时,我想在该行中写 "InformationalTextExample",可能用“_”分隔:

>Unc14086_InformationalTextExample
AGAGUUUGAU 
>Unc35443_InformationalTextExampleII
GCACGAGAAA 

这怎么可能?

谢谢!

我会使用 Perl 来完成这项任务。我假设文件名为 1.fasta1.tsv:

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

my %name_of_id;
open my $TSV, '<', '1.tsv' or die $!;
while (<$TSV>) {
    my ($id, $name) = split /\t/;
    $name_of_id{$id} = $name;
}
close $TSV;

open my $FASTA, '<', '1.fasta' or die $!;
while (<$FASTA>) {
    if (my ($id) = /^>(\S*)/) {
        if (exists $name_of_id{$id}) {
            chomp;
            print $id, '_', $name_of_id{$id};
        } else {
            warn "WARNING: $id not found!\n";
            print;
        }
    } else {
        print;
    }
}

最好提供一个 input/output 示例来帮助人们理解您的问题。但请尽量避免打错字!!!

看看你的例子:

>Unc14086  then Unc14806 Inf...
      ^              ^
>Unc35443  then Unf35433 Inf...
   ^              ^

总之,这条线将帮助你:

 awk 'NR==FNR{a[]="_";next}sub(/^>/,"",){[=11=]=">"a[]}7' FS="\t" f2 f1 

f2是没有>

的文件

固定错字的示例:

kent$  head f2 f
==> f2 <==
Unc14086        InformationalTextExample
Unc35443        InformationalTextExampleII

==> f <==
>Unc14086
AGAGUUUGAU 
>Unc35443
GCACGAGAAA

kent$  awk 'NR==FNR{a[]="_";next}sub(/^>/,"",){[=12=]=">"a[]}7' FS="\t" f2 f
>Unc14086_InformationalTextExample
AGAGUUUGAU 
>Unc35443_InformationalTextExampleII
GCACGAGAAA