perl按行内容来分类:按照一行中元素个数的不同写入不同的文件夹,供大家学习参考。
假如,手里有如下所示的文件内容:
1000033 A A A A A A A A A A A A A A C A A A A
1000114 G G C G G G G G G G G G G G G G G G G
100013 C C T C T T T C C C C C C T C C C C C
1000238 C C G C C C C C C C C C C C C C C C C
100027 T T A T A A A T T T T T T A T T T T T
1000383 G G G G G A G G A G A A G G A G G G G
以第一个碱基为参考标准:
第一行:A有18个:其他1个
第二行:G有18个:其他1个
第三行:C有14个:T有5个
第四行:C有18个:G有1个
第五行:T有14个:A有5个
我们需要通过程序,把1,2,4行这样的分到一个文件中,3,5行的内容放到一个文件夹中。
程序如下:
#!/usr/bin/perl
#class different line to differet file
use strict;
use warnings;
open (CLASS,"OnlyATGC.txt")||die("can not open!");
open (NUM1,">1-18.txt")||die("can not open!");
open (NUM2,">2-17.txt")||die("can not open!");
open (NUM3,">3-16.txt")||die("can not open!");
open (NUM4,">4-15.txt")||die("can not open!");
open (NUM5,">5-14.txt")||die("can not open!");
open (NUM6,">6-13.txt")||die("can not open!");
open (NUM7,">7-12.txt")||die("can not open!");
open (NUM8,">8-11.txt")||die("can not open!");
open (NUM9,">9-10.txt")||die("can not open!");
open (NUM10,">10-9.txt")||die("can not open!");
open (NUM11,">11-8.txt")||die("can not open!");
open (NUM12,">12-7.txt")||die("can not open!");
open (NUM13,">13-6.txt")||die("can not open!");
open (NUM14,">14-5.txt")||die("can not open!");
open (NUM15,">15-4.txt")||die("can not open!");
open (NUM16,">16-3.txt")||die("can not open!");
open (NUM17,">17-2.txt")||die("can not open!");
open (NUM18,">18-1.txt")||die("can not open!");
my @informations;
my $information;
my $cout=1;
my $firstpase;
while(<CLASS>)
{
chomp;
@informations = split;
$firstpase = $informations[1];
foreach $information(@informations)
{
if($information=~$firstpase)
{
$cout++;
}
else
{
next;
}
}
if($cout==1)
{
print NUM1 "$_n";
$cout=0;
}
elsif($cout==2)
{
print NUM2 "$_n";
$cout=0;
}
elsif($cout==3)
{
print NUM3 "$_n";
$cout=0;
}
elsif($cout==4)
{
print NUM4 "$_n";
$cout=0;
}
elsif($cout==5)
{
print NUM5 "$_n";
$cout=0;
}
elsif($cout==6)
{
print NUM6 "$_n";
$cout=0;
}
elsif($cout==7)
{
print NUM7 "$_n";
$cout=0;
}
elsif($cout==8)
{
print NUM8 "$_n";
$cout=0;
}
elsif($cout==9)
{
print NUM9 "$_n";
$cout=0;
}
elsif($cout==10)
{
print NUM10 "$_n";
$cout=0;
}
elsif($cout==11)
{
print NUM11 "$_n";
$cout=0;
}
elsif($cout==12)
{
print NUM12 "$_n";
$cout=0;
}
elsif($cout==13)
{
print NUM13 "$_n";
$cout=0;
}
elsif($cout==14)
{
print NUM14 "$_n";
$cout=0;
}
elsif($cout==15)
{
print NUM15 "$_n";
$cout=0;
}
elsif($cout==16)
{
print NUM16 "$_n";
$cout=0;
}
elsif($cout==17)
{
print NUM17 "$_n";
$cout=0;
}
elsif($cout==18)
{
print NUM18 "$_n";
$cout=0;
}
else
{
$cout=0;
next;
}
}