初学perl语言,感觉提取文本矩阵的某些行或者列还是比较有用的。
下面是举一些具体的例子,供大家学习参考。
第一:提取含有某个关键字的行:
#!/usr/bin/perl
open FILE, "E:/SNP.txt ";
open OUT1,'+>E:/file3.txt';
foreach (<FILE>) {
@pairs=split(/ /, $_);
$count=@pairs;
if($pairs[$count-1] =~ m/^reference$/)
{
print OUT1 $_,"n";
}
}
close(FILE);
close(OUT1);
第二:提取指定的某些列(提取1 2 3 5列):
#!/usr/bin/perl
open(INFILE, "E:/file3.txt");
open(OUTFILE, ">E:/file4.txt")|| die "Cannot open the newfile: $!n";;
while (<INFILE>) {
@a = split(" ");
print OUTFILE "$a[0]t $a[1]t $a[2]t $a[4]n";
}
exit;
第三:去掉文本中有空行的命令:
#!/usr/bin/perl
open(INFILE, "E:/file3.txt");
open(OUTFILE, ">E:/file4.txt")|| die "Cannot open the newfile: $!n";;
while (<INFILE>) {
if (!/^$/){
print OUTFILE "$_";}
}
exit;
第四:自己编写的提取文本中含有某个关键字的命令(感觉比较实用):
#!/usr/bin/perl
open FILE, "E:/chr_2.txt ";
open OUT1,'+>E:/chr_2_refference.txt';
foreach (<FILE>) {
if(/reference$/)
{
print OUT1 $_,"n";
}
}
close(FILE);
close(OUT1);
第五:提取文本中非字符行的命令:
#!/usr/bin/perl
open(INFILE, "E:/最后得到的SNP/chr_X.txt");
open(OUTFILE, ">E:/去掉LOC以后得到的最后SNP/chr_X.txt")|| die "Cannot open the newfile: $!n";;
while (<INFILE>) {
if (!/[A-Za-z]/){
print OUTFILE "$_";}
}
exit;