Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Make a perl script by taking specific lines from a txt file

by nimpoura (Initiate)
on Nov 22, 2014 at 18:40 UTC ( #1108114=perlquestion: print w/replies, xml ) Need Help??

nimpoura has asked for the wisdom of the Perl Monks concerning the following question:

Hello, i am a beginner and i have a problem that is really urgent! I would like to write a script! Actually i am working with lemur and i have this txt file as a parameter file :
<query> <type>indri</type> <number>351</number> <text>Falkland petroleum exploration</text> </query>
<query> <type>indri</type> <number>352</number> <text>British Chunnel impact</text> </query>
<query> <type>indri</type> <number>353</number> <text>Antarctica exploration</text> </query>
<query> <type>indri</type> <number>354</number> <text>journalist risks</text> </query>
(i want to use the IndriRunQuery application. The basic command line usage is: $ ./IndriRunQuery <parameter_file>.), so i want to run each query separately, namely for 351<i<354. I want for example for i=351 to run /home/nikol123/indri-5.6/runquery/IndriRunQuery <parameter_file> , where parameter file for i=351 is this:
<query> <type>indri</type> <number>351</number> <text>Falkland petroleum exploration</text> </query>
for i=352 the parameter file is
<query> <type>indri</type> <number>352</number> <text>British Chunnel impact</text> </query>
and for each different i to do it for <fbdocs>j</fbdocs> , for 2<j<20
Thank you in advance!
  • Comment on Make a perl script by taking specific lines from a txt file

Replies are listed 'Best First'.
Re: Make a perl script by taking specific lines from a txt file
by GrandFather (Saint) on Nov 22, 2014 at 19:23 UTC
    I would like to write a script!

    Ok. You have my permission to write a script, although really no one here is likely to stop you. In fact we encourage you to write a script, and if you have any trouble at all we are pleased to help you learn how to write scripts and correct any errors in scripts you have written.

    To help you get started take a look in the Tutorials section here and make use of perldoc.

    Perl is the programming world's equivalent of English
Re: Make a perl script by taking specific lines from a txt file
by Old_Gray_Bear (Bishop) on Nov 23, 2014 at 00:16 UTC
    Since you are having problems using the Lemur Toolkit, perhaps a stroll through the Lemur Help Forum would be more fruitfull.

    Please note that even if your application (IndriQuery) is written in Perl (which, by the way it is not), that does not make instructing you on the proper use of that tool a "Perl Question".

    I Go Back to Sleep, Now.


Re: Make a perl script by taking specific lines from a txt file
by Anonymous Monk on Nov 22, 2014 at 19:01 UTC
    I would like to write a script!
    So, what's the problem?
      I want to make a script in order to read from the parameter file(txt) that i wrote first, which contains some lines with this form
      <query> <type>indri</type> <number>i</number> <text>Falkland petroleum exploration</text> </query>
      . Based on the number of number field(<number>i</number>) i want to run for example /home/nikol123/indri-5.6/runquery/IndriRunQuery <parameter_file> (command written in terminal) for i=351 with parameter file which contain only
      <query> <type>indri</type> <number>351</number> <text>Falkland petroleum exploration</text> </query>
      ,namely ignore the other lines that are specified by different <number>i</number>
        I want to make a script
        Since it's 'really urgent' I guess you already wrote most of it... just show us what you've got so far.
RAM, "
by Laurent_R (Canon) on Nov 22, 2014 at 21:40 UTC
    I have no idea about what lemur is, and also don't know what IndriRunQuery is, but you could possibly do something like this;
    for my $i (351..354) { my $param = "<parameters> <index>/home/nikol123/indri-5.6/4collections</index> <query> <type>indri</type> <number>$i</number> <text>British Chunnel i +mpact</text> </query> <fbDocs>10</fbDocs> </parameters>"; my $paramfile = "param$i.txt"; open my $PARAM, ">", $paramfile or die "Can't open $paramfile $ +!"; print $PARAM, $param; close $PARAM; do_something_with($paramfile); }
      "I have no idea about what lemur is..."

      Please see Lemur as well as The Lemur Project ;-)

      Best regards, Karl

      «The Crux of the Biscuit is the Apostrophe»

        Thank you, Karl. I actually knew what lemurs were (the animals), but had not idea about what the Lemur Project was.
      I wrote this code :
      #!/usr/bin/perl use strict; use warnings; for(my $i=351; $i<400; $i++){ my $filename = '/home/nikol123/indri-5.6/runquery/trec7_4collections.t +xt'; open(my $file, '<', $filename) or die "open: $!"; while ( <$file> ) { my @lines = split(/\n/, $filename); foreach my $line (@lines) { if ($line = '<query> <type>indri</type> <number>$i</number> <text>"_" +</text> </query>') { my $param = "<parameters> <index>/home/nikol123/indri-5.6/4collections</index> print $line; <fbDocs>10</fbDocs> </parameters>"; print $param; }}}}
      but it's not functional as i get this
      <parameters> <index>/home/nikol123/indri-5.6/4collections</index> print <query> <type>indri</type> <number>$i</number> <text>"_"</text> +</query>; <fbDocs>10</fbDocs> </parameters>
      but i want due to for loop to read and write the line that contain the variable i from the my txt file ,in order to have the according text field, for example for i=351 to have
      <parameters> <index>/home/nikol123/indri-5.6/4collections</index> <query> <type>indri</type> <number>351</number> <text>Falkland petrole +um exploration</text> </query>; <fbDocs>10</fbDocs> </parameters>
        Consider using an XML parser. eg XML:Twig.
        #!perl use strict; use warnings; use XML::Twig; my $filename = 'trec7_4collections.txt'; our $num; my $fh; for $num (351..354){ open $fh,'>',$num.'.xml' or die "$!"; my $twig = XML::Twig->new( twig_handlers => { query => \&query }, pretty_print=>'indented' ); $twig->parsefile($filename); $twig->flush($fh); print "Created $num.xml\n"; } sub query { my( $twig, $e) = @_; my $n = $e->first_child_text('number'); $e->cut if ($n ne $num); }

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1108114]
Approved by GrandFather
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2021-10-24 02:55 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (88 votes). Check out past polls.