perl - Split a file in several output -
i trying divide big file different files containing single information each variable inside file.
my input file this:
#chrom pos id ref alt qual filter info format pid008sm ...info here 1..... #chrom pos id ref alt qual filter info format cl001-sc ....info here 2.... #chrom pos id ref alt qual filter info format cl001-sc ....info here 3.... #chrom pos id ref alt qual filter info format pid008sm ....info here 4.... in case create 2 output file (one pid008sm , cl001-sc) information related each of them.
output cl001-sc:
#chrom pos id ref alt qual filter info format cl001-sc ....info here 2... ....info here 3... output pid008sm
#chrom pos id ref alt qual filter info format pid008sm ....info here 1.... ....info here 4.... the script have used in perl suggestion more welcome. thank in advance.
code:
#!/usr/bin/perl; use strict; use warnings; $file1 = $argv[0] ; $file2 = $argv[1]; open (f1, $file1); #opens first .vcf file comparison open (f2, $file2); #2nd comparison %file; ## create hash key each line of file2 while (<f2> ) { #chomp; $file{$_}=''; } ## print line , if key exist in hash ; foreach $string (<f1>) { if ( exists $file{$_}) , ($string =~ /(#)(.+?)(#)/s) { print $string; } }
something perhaps?
use strict; use warnings; open $fh, '<', 'chrom.txt' or die $!; %fh; while (<$fh>) { if ( /^#chrom/ ) { $name = (split)[-1]; if ($fh{$name}) { select $fh{$name}; next; } $file = "$name.txt"; open $fh{$name}, '>', $file or die qq{unable open "$file" output: $!}; print stdout qq{created file "$file"\n}; select $fh{$name}; } print; }
Comments
Post a Comment