Perl - Grep open file more then once.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl - Grep open file more then once.
# 1  
Old 07-16-2011
Perl - Grep open file more then once.

Hi,

I am using File::Find to go through a very large tree.
I am looking for all xml files and open only those that contain a tag <Updated>. I then want to capture the contents of two tags <Old> and <New>.

My problem is, after I open the file and do the first grep for <Updated> (which does work), I am unable to grep again unless I close the file and open it.

I did something like this:

Code:
find(\&check, $dir);

sub check {
          if ($_ =~ /.xml/){
          open(FILE,"$_");
                  if (grep{/Updated/} <FILE>){    # <-- works
                       my $old = grep /OLD/ <FILE>  #<-- does not - syntax may not be right, working from mem
                       my $new = grep /New/ <FILE>  #<--does not
                    } 
         }    
}

Print "File $_ has Old value of $old and new value of $new";

I gave up on using grep more then once and tried using while:

Code:
open(FILE,"$_");
if (grep{/Updated/} <FILE>){ 
    my $path = cwd;
    print "\n$path\n";
    print "$_ \n";
    while ($line = <FILE>) {
   if ($line =~ m/Old|New/) { print $line }
   } 
  } 
} 
close FILE;

This also did not work.
If I close the file and open it again before the while statement, it works.

So....
1) Is there a way to grep the open file more then once and create $vars?
2) If not, is it better (faster) to write the file to a @list and then grep that rather then keep opening and closing the file?

I would rather grep then use "while" as that causes other headaches with the rest of the script.

Most of the files are fairly small.... but there will be many thousand of them.

Any thoughts / slaps to the head?

-OG-

Last edited by pludi; 07-18-2011 at 05:53 PM..
# 2  
Old 07-17-2011
No need to open a file for grepping, when grep it does that automatically for you

File::Grep - search.cpan.org
# 3  
Old 07-17-2011
Try this, this should work. I am not sure about your goal here.

If it works try to find answers to why changing approach worked!

Code:
find(\&check, $dir);

sub check {
    if ($_ =~ /.xml/)
    { 
         open(FILE,"$_") or die "Failed to open $_ file , Error [$!]\n" ; 
             chomp ( my @Contents =  <FILE> ) ; 
         close(FILE) ; 

         my ($old,$new) ; 

         if ( grep(/Updated/, @Contents ) ) 
         {
              $old  = grep(/^OLD$/,@Contents ) ; # $old will be array 
              $new = grep(/^New$/,@Contents ) ; 
          }
      }
}

# 4  
Old 07-18-2011
Hi.
Quote:
Originally Posted by matrixmadhan
No need to open a file for grepping, when grep it does that automatically for you

File::Grep - search.cpan.org
Indeed File::Grep will open files for you. Here is a driver that lists data files, the perl code, and the results of counting lines in the files twice, then searching for a pattern in those same files:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate File::Grep, test driver for.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C perl

p=./p1

pl " Input data files data[12]:"
for file in data[12]
do
  pe
  pe " File $file:"
  cat $file
done

pl " perl script:"
cat $p

pl " Results:"
$p hi data*

exit 0

producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
GNU bash 3.2.39
perl 5.10.0

-----
 Input data files data[12]:

 File data1:
hi
hi 2

 File data2:
hi 3
lo
hi 4
by

-----
 perl script:
#!/usr/bin/env perl

# @(#) p1	Demonstrate File::Grep, perl grep on a file.

use File::Grep qw( fgrep fmap fdo );
use warnings;
use strict;

$File::Grep::SILENT = 0;
my ($keyword) = shift || die " Must have at least a keyword.\n";
print " keyword is :$keyword:\n";
my (@f) = @ARGV;
print " files are :@f:\n";

# fdo
print "\n";
my ($count) = 0;
fdo { $count++ } @f;
print "Pass 1 (no open), total lines in :@f: -- $count\n";

print "\n";
$count = 0;
fdo { $count++ } @f;
print "Pass 2 (no open), total lines in :@f: -- $count\n";

# fmap (fgrep is complex).
my (@matches);
print "\n";
print " Matches for :$keyword: in :@f: (no open)\n";
fmap { push( @matches, $_ ) if /$keyword/; } @f;
print @matches;

exit;

-----
 Results:
 keyword is :hi:
 files are :data1 data2:

Pass 1 (no open), total lines in :data1 data2: -- 6

Pass 2 (no open), total lines in :data1 data2: -- 6

 Matches for :hi: in :data1 data2: (no open)
hi
hi 2
hi 3
hi 4

Best wishes ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Capture output of open pipe to a file in perl

Hi, I am trying to capture the output of the an open pipe in perl. but I am not sure how to do this. can some one please help me do that? Below is the script I am using (2 Replies)
Discussion started by: ahmedwaseem2000
2 Replies

2. Shell Programming and Scripting

Using perl to grep a list of patterns from an input file

I have been struggling to grep a file of NGrams (basically clusters of consonants or Consonant and Vowel) acting as a pattern file from an Input file which contains a long list of words, one word per line. The script would do two things: Firstly read a text pattern from a large file of such... (5 Replies)
Discussion started by: gimley
5 Replies

3. Shell Programming and Scripting

[Solved] perl and grep: get a script to look at more then one file

hi guys i have this very messy script, that looks in /var/log/messages.all for an error and reports if it finds the key works how can i get it to look at more then one file, i.e /var/log/message.all * so it looks in old logs as well thanks exit 0 if (isRenderNode(hostname)); my... (4 Replies)
Discussion started by: ab52
4 Replies

4. Shell Programming and Scripting

Unable to open a file in perl

Not able to open a file using this code why not? use strict; use warnings; my $file = "verInfo.txt"; unless(open FILE, $file) { # Die with error message # if we can't open it. die "\nUnable to open $file\n"; } my $line = <FILE>; print $line; close FILE; (7 Replies)
Discussion started by: srijith
7 Replies

5. Red Hat

cannot set user id: Resource temporarily unavailable (not open file/open process related)

First post, sorry to be a bother but this one has been dogging me. I have a process user (java application server) that trips a resource limit every couple weeks and need help finding what limit we're hitting. First, this is what's running: This is the error when jobs are run or the... (0 Replies)
Discussion started by: Katahdin
0 Replies

6. Shell Programming and Scripting

pattern grep using Perl in .TSV file

Hi All, I have a .TSV extension file having ~1 Gig data and I need to grep a pattern in that file using perl. I am not able to read the file using perl any suggestions on this/ If I Change the format my data gets mismangled so I am bothered about using specific format as well. #!... (3 Replies)
Discussion started by: vmsenthil
3 Replies

7. Shell Programming and Scripting

doing grep inside perl file

Hi have one Perl file inside that i am defining at an array file. @temp_vmdk_files = `grep vmdk '$guest_vmx'` where my $guest_vmx=/vmfs/volumes/47e40fec-9c8bb7f7-d076-001422159f8a/BES Exchange/BES-Exchange.vmx and i am just want to do grep of "vmdk" files from the above path but when... (5 Replies)
Discussion started by: bp_vardhaman
5 Replies

8. Shell Programming and Scripting

Split a file based on pattern in awk, grep, sed or perl

Hi All, Can someone please help me write a script for the following requirement in awk, grep, sed or perl. Buuuu xxx bbb Kmmmm rrr ssss uuuu Kwwww zzzz ccc Roooowwww eeee Bxxxx jjjj dddd Kuuuu eeeee nnnn Rpppp cccc vvvv cccc Rhhhhhhyyyy tttt Lhhhh rrrrrssssss Bffff mmmm iiiii Ktttt... (5 Replies)
Discussion started by: kumarn
5 Replies

9. Shell Programming and Scripting

grep all records in a file and get a word count -perl

Hi, I have a file .. file.txt .. i need to get a total record count in the files into a $variable.. im using perl script thanks (4 Replies)
Discussion started by: meghana
4 Replies

10. Shell Programming and Scripting

grep ^M in file using perl script....

hi i am using perl on windows ( active state perl 5.8 ) and i want to check for Control-M (^M) in file. files to be checked are in unix format so new line character is (\n). This perl script is called from Batch file ( windows .BAT file ) my script is while (<PROGRAM>) { ... (12 Replies)
Discussion started by: zedex
12 Replies
Login or Register to Ask a Question