This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How To Read Few Lines As A Single Line In A Text File,,
    <reporter name="GT_Hg_chr17_69994042-69994101_138264" systematic_name="chr17:69994042-69994101" active_sequence="AGCCACCCAACAGAAGCAAAAGACAACTAAGGCAGCAAATACAAGCCTACAATATATCCA" start_coord="0">
          <feature number="12921">
            <position x="1.3931461995545655" y="0.0" units="mm"/></feature>
          <gene systematic_name="chr17:69994042-69994101" primary_name="chr17:69994042-69994101" description="Unknown"></gene>
        </reporter>

       Hi all i have file as above, of size 20mb with the same data.



  if($_=~/\<reporter name\=\"(.*)\"\ssystematic_name\=\"(.*)\"\sactive_sequence\=\"(.*)\"\s.*/g){
        $ID=$1;
        $Loc=$2;
        $Seq=$3;
            print OUT"$ID\t $Loc\t $Seq\n";
          }

the problem is i could match the line only till "active_sequence" coz till there it is in a single line, but all i want to do is read the file from <reporter> to </reporter> as a single line match and so on,,,,,

so how do i this using PERL,,,,??

perl

Because i want to match and print values of "x","y" and description with "t"

Rule of thumb. Never parse HTML or XML with REGEX. Use appropriate modules.

3 answers

For XML, use an XML parser.

For example, XML::Twig can stream through tags without loading everything into memory. The code will look something this:

use XML::Twig;
use Modern::Perl;

my $t= XML::Twig->new(
    TwigHandlers => { 'reporter' => \&reporter },
);
$t->parsefile( 'report.xml');

sub reporter {
    my( $t, $reporter)= @_;
    my $name = $reporter->{'att'}->{'name'};
    say $name;
    my $feature_id = $reporter->first_child('feature')->{'att'}->{'number'};
    say $feature_id;
}

If you have more than one child tag, there are methods like 'children' that return an array.

Hi Heikki, i went through code spippet which is as below

use XML::Simple qw(:strict); my $filename = 'trial.xml'; my $library = XMLin($filename, ForceArray => 1, KeyAttr => {}, );

foreach my $reporter (@{$project->{name}}) { print $reporter->{title}->[0], "\n" }

so the above code prints all the "names" from the xml file , but how do i modify this to print details including systematicname, Activeseq,x,y, number and description which tab(\t) separated as i printed in the query, so do u have any idea,,,?

Hi Heikki, i went through code spippet which is as below use XML::Simple qw(:strict); my $filename = 'trial.xml'; my $library = XMLin($filename, ForceArray => 1, KeyAttr => {}, ); foreach my $reporter (@{$project->{name}}) { print $reporter->{name}->[0], "\n" } so the above code prints all the "names" from the xml file , but how do i modify this to print details including systematicname, Activeseq,x,y, number and description which tab(\t) separated as i printed in the query, so do u have any idea,,,?

Kiran, use a little effort in here. Don't abuse people to do the complete coding for you. Given this answer, it is obvious what you have to do.

Kiran, you need to first figure out how to access each of the elements you are interested. Use Data::Dumper or Data::Printer modules for that. Then use function join() to print the line out.

Check out the perl special variable "$/" 'coz' it allows you to change the line delimiter. This is if I am interrupting your question correctly?

$/ = 'whatever line break'

yea it works as input record separator, Perl spl char seeems to very useful, but still i have practice em,, n dono how to use em,,?

A simple HTML or XML parser in perl should help you.

you can define your own tag and use the parsing script to parse your data.

  1. resource 1
  2. resource 2
  3. resource 3

hope this helps.

Thank you all as Gjain, maasha and Heikki said i've to learn and work using HTML and XML perl modules to get the details frm XML file.

Log in to answer this question.