How To Read Few Lines As A Single Line In A Text File,,
3
0
Entering edit mode
13.0 years ago
Kiran ▴ 80
    <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 • 3.0k views
ADD COMMENT
3
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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

ADD REPLY
4
Entering edit mode
13.0 years ago
Heikki ▴ 360

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.

ADD COMMENT
1
Entering edit mode

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.

ADD REPLY
0
Entering edit mode

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,,,?

ADD REPLY
0
Entering edit mode

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,,,?

ADD REPLY
0
Entering edit mode

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.

ADD REPLY
0
Entering edit mode
13.0 years ago

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'

ADD COMMENT
0
Entering edit mode

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,,?

ADD REPLY
0
Entering edit mode
13.0 years ago
Gjain 5.8k

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.

ADD COMMENT
0
Entering edit mode

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.

ADD REPLY

Login before adding your answer.

Traffic: 2678 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6