The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Perl: Opening a filehandle but not getting anything back from it Smiling Dragon Shell Programming and Scripting 8 08-16-2008 03:40 AM
ls command to print fifo of contents (or perl) jerardfjay Shell Programming and Scripting 1 04-06-2006 03:01 PM
Opening files saarshad001 UNIX for Dummies Questions & Answers 3 11-28-2003 12:42 PM
Opening Files AJA UNIX for Dummies Questions & Answers 4 11-10-2003 11:25 AM
Opening Perl perleo Shell Programming and Scripting 2 08-26-2002 10:41 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-31-2008
Raynon Raynon is offline
Registered User
  
 

Join Date: Sep 2006
Location: Sg
Posts: 350
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 (permalink)  
Old 08-31-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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 (permalink)  
Old 09-01-2008
Raynon Raynon is offline
Registered User
  
 

Join Date: Sep 2006
Location: Sg
Posts: 350
Hi Era,

I have added on <*sorts> but still this (print "@arr_x "statement 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 (permalink)  
Old 09-01-2008
Raynon Raynon is offline
Registered User
  
 

Join Date: Sep 2006
Location: Sg
Posts: 350
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 (permalink)  
Old 09-01-2008
Raynon Raynon is offline
Registered User
  
 

Join Date: Sep 2006
Location: Sg
Posts: 350
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 (permalink)  
Old 09-02-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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 (permalink)  
Old 09-02-2008
Raynon Raynon is offline
Registered User
  
 

Join Date: Sep 2006
Location: Sg
Posts: 350
Hi Era,

Thks alot for the advice!!
Now i know where i am wrong.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:02 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0