[英]How do I use correctly chomp command to get rid of \n character in perl?
我的目標只是消除每個序列行末尾的換行符\\ n,而不是標題,我嘗試了以下代碼
#!/usr/bin/perl
use strict;
my $db = shift;
my $outfile= "Silva_chomped_for_R_fin.fasta";
my $header;
my $seq;
my $kick = ">";
open(FASTAFILE, $db);
open(OUTFILE,">". $outfile);
while(<FASTAFILE>) {
my $currentline = $_;
chomp $currentline;
if ($currentline =~ m/^$kick/) {
$header = $currentline;
} else {
chomp $currentline;
$seq = $currentline;
}
my $path = $header.$seq."\n";
print(OUTFILE $path);
}
close OUTFILE;
close FASTAFILE;
exit;
像如果chomp根本不起作用..任何關於我做錯事的想法嗎? 非常感謝Alfredo
while()
循環存在三個問題。
chomp()
。 chomp()
了先前的chomp()
的用途)。 這是一個簡化的版本。
use strict;
use warnings;
my $db = shift;
my $outfile = "out.fasta";
open(my $fh, "<", $db) or die "Could not open input file";
open(my $out, ">", $outfile) or die "Could not open output file";
my $header;
while (<$fh>) {
$header = /^>/;
chomp unless $header;
print $out $. > 1 && $header && "\n", $_;
}
close $out;
close $fh;
線
print $out $. > 1 && $header && "\n", $_;
將有條件前面加上一個新行的輸出,如果這符合一個開始“>” - ,除非它是在該文件中的第一行。 ( $.
變量是當前行號。)
圖片來源: ikegami在我的原始代碼中發現了該錯誤,以允許輸入數據庫中包含多個序列。
my $add_lf = 0;
while (<>) {
chomp;
if (/^>/) {
print("\n") if $add_lf;
print("$_\n");
$add_lf = 0;
} else {
print;
$add_lf = 1;
}
}
print("\n") if $add_lf;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.