我有字符串,我想格式化。我希望在第二个之间删除所有内容;最后一个;。
输入
cellular organisms;Eukaryota;Opisthokonta;Metazoa;Eumetazoa;Bilateria;Protostomia;Ecdysozoa;Panarthropoda;Arthropoda;Mandibulata;Pancrustacea;Hexapoda;Insecta;Dicondylia;Pterygota;Neoptera;Endopterygota;Coleoptera;Polyphaga;Cucujiformia;Tenebrionoidea;Tenebrionidae;Tenebrionidae incertae sedis;Tribolium;Tribolium castaneum;
输出
cellular organisms;Eukaryota;Tribolium castaneum;
我试过了seind。
$ sed 's/;[^;]*//' <<<"cellular organisms;Eukaryota;Opisthokonta;Metazoa;Eumetazoa;Bilateria;Protostomia;Ecdysozoa;Panarthropoda;Arthropoda;Mandibulata;Pancrustacea;Hexapoda;Insecta;Dicondylia;Pterygota;Neoptera;Endopterygota;Coleoptera;Polyphaga;Cucujiformia;Tenebrionoidea;Tenebrionidae;Tenebrionidae incertae sedis;Tribolium;Tribolium castaneum;"
生产
cellular organisms;Opisthokonta;Metazoa;Eumetazoa;Bilateria;Protostomia;Ecdysozoa;Panarthropoda;Arthropoda;Mandibulata;Pancrustacea;Hexapoda;Insecta;Dicondylia;Pterygota;Neoptera;Endopterygota;Coleoptera;Polyphaga;Cucujiformia;Tenebrionoidea;Tenebrionidae;Tenebrionidae incertae sedis;Tribolium;Tribolium castaneum;
非常感谢。
答案 0 :(得分:2)
通过这么简单的字段选择,您可以使用split
将字符串分隔为字段,选择所需的字段,然后将其与join
一起重新组合。
请注意,您必须为split
的第三个参数提供一个负数,以防止它丢弃尾随的空字段。
看起来像这样
use strict;
use warnings;
my $s = 'cellular organisms;Eukaryota;Opisthokonta;Metazoa;Eumetazoa;Bilateria;Protostomia;Ecdysozoa;Panarthropoda;Arthropoda;Mandibulata;Pancrustacea;Hexapoda;Insecta;Dicondylia;Pterygota;Neoptera;Endopterygota;Coleoptera;Polyphaga;Cucujiformia;Tenebrionoidea;Tenebrionidae;Tenebrionidae incertae sedis;Tribolium;Tribolium castaneum;';
my $ss = join ';', (split /;/, $s, -1)[0,1,-2,-1];
print $ss, "\n";
<强>输出强>
cellular organisms;Eukaryota;Tribolium castaneum;
答案 1 :(得分:1)
您可以在Perl中使用此正则表达式
perl -pE's/;.*?\K;.*(?=;.*;)//' <<<'cellular organisms;Eukaryota;Opisthokonta;...;Tribolium;Tribolium castaneum;'