Note that there are some explanatory texts on larger screens.

plurals
  1. POSearch in directory of files based on keywords from another file
    primarykey
    data
    text
    <p>Perl Newbie here and looking for some help.</p> <p>I have a directory of files and a "keywords" file which has the attributes to search for and the attribute type. </p> <p>For example:</p> <p>Keywords.txt</p> <pre><code>Attribute1 boolean Attribute2 boolean Attribute3 search_and_extract Attribute4 chunk </code></pre> <p>For each file in the directory, I have to:</p> <ul> <li>lookup the keywords.txt</li> <li>search based on <code>Attribute</code> type</li> </ul> <p>something like the below. </p> <pre><code>IF attribute_type = boolean THEN search for attribute; set found = Y if attribute found; ELSIF attribute_type = search_and_extract THEN extract string where attribute is Found ELSIF attribute_type = chunk THEN extract the complete chunk of paragraph where attribute is found. </code></pre> <p>This is what I have so far and I'm sure there is a more efficient way to do this.</p> <p>I'm hoping someone can guide me in the right direction to do the above. Thanks &amp; regards, SiMa</p> <pre><code># Reads attributes from config file # First set boolean attributes. IF keyword is found in text, # variable flag is set to Y else N # End Code: For each text file in directory loop. # Run the below for each document. use strict; use warnings; # open Doc open(DOC_FILE,'Final_CLP.txt'); while(&lt;DOC_FILE&gt;) { chomp; # open the file open(FILE,'attribute_config.txt'); while (&lt;FILE&gt;) { chomp; ($attribute,$attribute_type) = split("\t"); $is_boolean = ($attribute_type eq "boolean") ? "N" : "Y"; # For each boolean attribute, check if the keyword exists # in the file and return Y or N if ($is_boolean eq "Y") { print "Yes\n"; # search for keyword in doc and assign values } print "Attribute: $attribute\n"; print "Attribute_Type: $attribute_type\n"; print "is_boolean: $is_boolean\n"; print "-----------\n"; } close(FILE); } close(DOC_FILE); exit; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload