Solved

I want to search through an asci file

Posted on 2000-03-14
4
201 Views
Last Modified: 2010-03-05
I'm a new programmer in Perl. I have a ascii file with 20000 records. How i can make searches inside this file with one or two fields. Which are the steps?
May i have a simple example of this type of program.
Thank you
0
Comment
Question by:vage78
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 5

Expert Comment

by:PC_User321
ID: 2615128
Is each line of the file divided into fields?
If so, what is used to separate the fields?

Here is an example where the fields are separated with a '|' character.
Assume you want the first field to be "Person" and the third field to be "Every month":

$Record0Search = "Person";
$Record2Search = "Every month";

open(FILE, "data.txt");
while ($Line = <FILE>) {
   @Fields = split '\|', $Line;
   print $Line if (($Fields[0] eq $Record0Search) && ($Fields[2] eq $Record2Search));
}

0
 
LVL 5

Accepted Solution

by:
PC_User321 earned 30 total points
ID: 2615182
The 'split' command divides the lines into fields, separated by the character within the single quotes.

The 'eq' in the comparison means that the fields must be exactly equal the the values you are looking for.  Other comparisons are possible, such a case-insentive comparisons, or checking to see if the field _contains_ the string you are searching for.
http://www.perl.com/pub/doc/manual/html/pod/perlre.html gives you details.
For example, if you just wanted to find lines containing words "seven" or "eight" (case insensitive) in any field you would replace the print line with
   print $Line if ($Line =~ /seven|eight/i/);
(and you would not need to do the 'split')

I hope this helps.
0
 

Author Comment

by:vage78
ID: 2615208
Thank you very much.I will try.My file is very big. Is this a problem? (20000 records) or everything is ok.
May i use also indexes.
Thanks a lot
0
 
LVL 5

Expert Comment

by:PC_User321
ID: 2615210
>> My file is very big. Is this a problem?  No

>> May i use also indexes?
Please explain what you mean.

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question