Opening Files and checking contents in Perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Opening Files and checking contents in Perl
# 1  
Old 08-31-2008
Opening Files and checking contents in Perl

Hi All,

I need some expert help in performing the following in Perl.
I have a code below but it doesn;t seem to work. Can any expert give me some advice?

Below are the requirements
1) Open numerous files assigned to an array @FILES. Note that the files are always named with the term "sorts". Examples of these file names are "false1_sorts" , "false_sorts" , "true1_sorts" , "true_sorts" etc

2) Search for contents "32N6524" in the opened file. If contents exists, add the file to another array which is @arr_x. Here, the files that this content exist is true1_sorts" , "true_sorts"

Code:
#!/usr/bin/perl

@FILES = ( *sorts );
print "@FILES ";
print "\n";

foreach $summary_x ( @FILES ) {
        open(FH, '< $summary_x') or die $!;
                while( <FH> ) {
                chomp;      
                if ( /32N6524/ ) {
                       push (@arr_x, $summary_x)
                };
                close FH;
};
};


print "@arr_x ";

Expected Output:

false1_sorts false_sorts true1_sorts true_sorts
true1_sorts true_sorts

Last edited by Raynon; 08-31-2008 at 07:39 AM..
# 2  
Old 08-31-2008
To glob the wildcard, use <*sorts> instead of *sorts which does something altogether different than you want (you should have seen that it got printed as *main::sorts which is a globbed variable name, not a globbed file name.
# 3  
Old 09-01-2008
Hi Era,

I have added on <*sorts> but still this (print "@arr_x "Smiliestatement doesn;t get printed out.
Can you help ?


Code:
#!/usr/bin/perl

@FILES = ( <*sorts> );
print "@FILES ";
print "\n";

foreach $summary_x ( @FILES ) {
        open(FH, '< $summary_x') or die $!;
                while( <FH> ) {
                chomp;      
                if ( /32N6524/ ) {
                       push (@arr_x, $summary_x)
                };
                close FH;
};
};


print "@arr_x ";

# 4  
Old 09-01-2008
Hi , i think i know where's the mistake.
I miss out the duoble quote in the open statement.

open(FH, "< $summary_x") or die $!;
# 5  
Old 09-01-2008
Hi Era,

What would be the syntax if i need to match a term consisting of several words for example the term "MY PERSONAL FIILE".
I tried to use the below code but it didn;t work.
Can you help ?

Code:
if ( /MY PERSONAL FILE/ ) {
                       push (@arr_x, $summary_x)
                };

Contents for the file "true_sorts" is below.
Looks like if the term i want to match is not at the first line of the contents , then there won;t be any match.

Code:
32N6524

MY PERSONAL FILE


Last edited by Raynon; 09-01-2008 at 10:56 PM..
# 6  
Old 09-02-2008
You have the closing brace in the wrong place, you are closing the file after reading one line from it.

It helps to see the logic if you consistently indent one level deeper after an opening brace, and decrease indentation at the closing brace.

Code:
#!/usr/bin/perl

@FILES = ( <*sorts> );
print "@FILES ";
print "\n";

foreach $summary_x ( @FILES ) {
        open(FH, "< $summary_x") or die $!;
        while( <FH> ) {
                chomp;      
                if ( /32N6524/ ) {
                       push (@arr_x, $summary_x);
		       last;
                }
	}
	close FH;
};


print "@arr_x ";

I added the last as a minor optimization, but otherwise, this just has the brace moved to the right place, and the single quotes changed to double quotes, and corrected indentation.

The chomp doesn't appear to be necessary, but maybe you want to expand the script to the point where you do want lines to be chomped.

The matching on stuff with spaces in it is not a problem.

Last edited by era; 09-02-2008 at 03:13 AM.. Reason: Remark on Useless Use of Chomp
# 7  
Old 09-02-2008
Hi Era,

Thks alot for the advice!!
Now i know where i am wrong.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl script to Merge contents of 2 different excel files in a single excel file

All, I have an excel sheet Excel1.xls that has some entries. I have one more excel sheet Excel2.xls that has entries only in those cells which are blank in Excel1.xls These may be in different workbooks. They are totally independent made by 2 different users. I have placed them in a... (1 Reply)
Discussion started by: Anamika08
1 Replies

2. Shell Programming and Scripting

Printing a message in file without opening it in perl

Hello friends, i have a perl script as below ... for (0 ..$#values) { ##want to print some message here in Report.txt file print `find /abc/xyz/pqr/$values" -type f -ls` >> Report.txt } I am able to get output of print `find /abc/xyz/pqr/$values" -type f -ls` >> Report.txt in... (2 Replies)
Discussion started by: harpal singh
2 Replies

3. UNIX for Dummies Questions & Answers

Opening a file in perl

Hi I need to open a file if a condition(for example a if a regular expression) is met. How do i do this ? open (file) if (some regex)..... (3 Replies)
Discussion started by: manutd
3 Replies

4. Shell Programming and Scripting

Opening File names with spaces inside it !- PERL

I developed a perl code..And the excerpt from it is given below... open(HANDLE,$cmp_path) ; #reading the xml file from the file path while($file_path = <HANDLE>) I have list of XML files to read from a folder. It has some spaces inside the name of the file...I used "\"... (2 Replies)
Discussion started by: gameboy87
2 Replies

5. Shell Programming and Scripting

Perl-opening a file then copying files

Good morning guys!! Im still practicing with Perl and now Im trying to open a file, and copy its contents to another file. Them I want to remeove the information out of the orginal file after it is copied over. The flow should be messages-->messages1-->messages2. Kind of like a log... (1 Reply)
Discussion started by: bigben1220
1 Replies

6. Shell Programming and Scripting

Opening Mulitple files using For loop in Perl

Hi All, I have a total of ten file to open in the Perl script and i am using a for loop to open each file and capture some strings inside each file. Unfortunately, i encounter the below syntax error. I think there should be something wrong with this term reports_${counting}_${_}.txt but i do... (4 Replies)
Discussion started by: Raynon
4 Replies

7. Shell Programming and Scripting

Perl: Opening a filehandle but not getting anything back from it

I have two perl functions defined, both run a set of shell commands on some somplied data and return hashs of the resulting parsed output from these shell commands. One works, one doesn't and I can't seem to see why. It's driving me insane :mad: The working one: sub getcellstatus { ... (8 Replies)
Discussion started by: Smiling Dragon
8 Replies

8. UNIX for Dummies Questions & Answers

Opening files

I am very new to unix. I want to open a file and read one line in at a time. Can anybody help? (3 Replies)
Discussion started by: saarshad001
3 Replies

9. UNIX for Dummies Questions & Answers

Opening Files

I'm a new to UNIX/LINUX. I just put cygwin on my laptop and I can navigate around the directories, but I can't open files (.doc, .ppt, .html or .exe). Is there an explicit command to do this? I know that in Solaris when it does not recognize the file, it brings up the list of available viewing... (4 Replies)
Discussion started by: AJA
4 Replies

10. Shell Programming and Scripting

Opening Perl

I have gone to /usr/bin/ and click on perl but notting happens.also notting happens when i click on c/c++ or any other program whats wrong ? (2 Replies)
Discussion started by: perleo
2 Replies
Login or Register to Ask a Question