473,408 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Searching a lot of files

Hi all:

I am new to PerlScripting .I got an assignment for parsing number of files at one time.The scenario is that I have 5 txt files in the same folder with different names (like SA1,XA2 etc.).I have to go through all these files and search for a pattern N008710LP in all the files and then stream the whole which contains this pattern to a new file which I have to create in the same script (in the format of Issuesmmddyyyy.txt).Can anyone give me a startup on that I will be very thankful.

Thanks
Jan 24 '08 #1
3 1212
KevinADC
4,059 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. my @files =qw(foo.txt bar.txt blah.txt);
  2. foreach my $file (@files) {
  3.    put Travis code here from Tek-Tips
  4. }
Jan 24 '08 #2
Hi KevinD:

Actually what I am trying to do here is to search a large number of files for a pattern NA001 in the folder /home/pp/.I
have many .txt files in the pp folder. I came up with the follwing code which will goto the pp folder and get the
name of the txt files in the pp folder and stores those @filenames array .This far is running good as I already tested it.
Now comes the searching part I am really stuck here I want my script here search each text file in my array @filenames in
the pp folder for the NA001 and the print filename and the whole line .Please see the code below.
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. opendir(CDIR,".");
  4.  
  5. @filenames = grep(/\.txt/,readdir(CDIR));
  6. @filenames=sort(@filenames);
  7. closedir(CDIR);
  8.  
  9.  
  10. foreach my $file(@filenames)) {
  11. open(FILE,"./pp/") or die "Can't open path ";
  12. while(<FILE>){
  13. if (/NA001/) {
  14. print "$_\n";
  15. }
  16. close FILE;
  17. }
  18.  
I will appreciate your help.

Thanks,

A Perl Beginner :)
Jan 25 '08 #3
KevinADC
4,059 Expert 2GB
The biggest problem is that you have not done anything with the $file variable in the "foreach" loop. I am not sure where $file goes in this path:

open(FILE,"./pp/")


but just plug it in where ever it goes and the script should work. I have fixed one syntax error and rewritten the script to compile with "strict" turned on:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. # use these two pragmas from now on!
  3. use strict;
  4. use warnings;
  5.  
  6. # this part is good
  7. opendir(CDIR,".");
  8. my @filenames = grep(/\.txt/,readdir(CDIR));
  9. @filenames = sort(@filenames);
  10. closedir(CDIR);
  11.  
  12.  
  13. # here you didn't do anything with $file
  14. # and you had at least one syntax error 
  15. # plug $file into the path and run the script
  16. foreach my $file (@filenames) {
  17.     open(FILE,"./pp/") or die "Can't open path ";
  18.     while(<FILE>){
  19.         if (/NA001/) {
  20.             print "$_\n";
  21.         }
  22.     }
  23.     close FILE;
  24. }
Jan 26 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: John | last post by:
Hi everyone ! This is a first time I post a message here. If I post my message in a wrong group. Please ignore it. I am trying to build a website which allows users (can be thousands of...
4
by: ADE | last post by:
Hi I was wondering if one of you could help me out I would like for this function to be able to find file names as well instead of printing out every file that ends with a .jpg or .gif extension...
3
by: Ma Xiaoming | last post by:
Dear ladies and gentlemen, As you know, by building a Smart Device Application in Microsoft Visual Studio .NET 2003, we could create a project for Pocket PC. My question is: How to search...
2
by: Paul Bromley | last post by:
Can someone give me a pointer of the best way to show that an application is busy and has not locked up? I am writing a utility to search a network drive for a number of tiff files. I will...
29
by: jaysherby | last post by:
I'm new at Python and I need a little advice. Part of the script I'm trying to write needs to be aware of all the files of a certain extension in the script's path and all sub-directories. Can...
1
by: Christian Münscher | last post by:
Hi Group! Here I have a big chaotic web-project where all 500 files are lying around in on directory without any structure. On the content-level there is a structure, the whole thing is divided...
5
by: bir | last post by:
I have a folder which contains the xml files with the word CICM in the filename (can also contain some other files) I need to search for all these xml files and create a single zip file for all...
11
by: jonathan184 | last post by:
script to search for keywords in files in a dir Basically the script consists of a file with part numbers I got a dir with files I want to search the files line by line and if a line has the...
1
by: SSJVEGETA | last post by:
Hello, everybody. I have read some examples and manuals for the egrep command for Linux and I don't know if this egrep command is right for the particular files I am searching for. Here is what the...
6
by: bushwacker | last post by:
Hello all, I'm a chemical engineering student. our teacher has given us a project to do some calculations based on some equations. those equations include constants, which are to be read from a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.