Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: How do i search and extract commented lines from a file using perl?

by sravan937 (Initiate)
on Jun 26, 2015 at 14:50 UTC ( [id://1132163]=note: print w/replies, xml ) Need Help??


in reply to Re: How do i search and extract commented lines from a file using perl?
in thread How do i search and extract commented lines from a file using perl?

Below is my progress so far. Below script is going through each file in a directory and printing all lines that has dbms output string in them. But how to eliminate dbms output string which is in between commented lines block. Ex:

/*Line 1 Line 2 Line 3 dbms out put string Line 4 */
I want to ignore above 5 lines
#!/usr/bin/perl use File::Find; use File::Basename; use strict; use English; # to get program name use Cwd; #use warnings; # Variables definitions my $match_param = "DBMS_output.put_line"; my $ignore_parameter = "/*"; undef my @nocomment_array; # array for not commented line undef my @comment_array; # array for commented line undef my @file_list; undef my @filenames; # list of filenames which contains matching para +meter my $file_list_ref; my $dir = cwd; #current working directory @ARGV = $dir unless @ARGV; find(\&process_file,@ARGV); foreach my $file(@$file_list_ref) { next if $file =~ m/$PROGRAM_NAME/i ; chomp($file); open(FH,"<$file") or die "Couldn't open $file for reading: $!\n"; my $line = 0; while(<FH>) { chomp $_; $line = $line +1; if (($_=~ m/$match_param/i) && ($_ !~ m/\s*--\s*$match_par +am/i)|| ($_=~ m/$match_param/i) && ($_ !~ m/$ignore_parameter\s*$matc +h_param/i)) { push(@nocomment_array,"$line\t$_\n"); } } if (@nocomment_array) { push(@filenames,"$file\n") if defined $nocomment_array[0]; print "\n$file\n" if defined $nocomment_array[0]; print @nocomment_array if defined $nocomment_array[0]; } undef @nocomment_array; } my $count = scalar(@filenames); print "\n###################################\n"; print "List of $count files in the above output\n"; print "###################################\n"; print @filenames; # Print only the filenames (the whole directory pat +h) # # process_file() subroutine: Processes all files of a directory recurs +ively # sub process_file() { next unless !-d $File::Find::name; # skips directory my $temp = $File::Find::name; chomp($temp); my $file_name = basename($temp); # strips filename if ($file_name =~ m/.sql$/) { push(@file_list,$temp); } $file_list_ref = \@file_list; } # end of process_file()

Replies are listed 'Best First'.
Re^3: How do i search and extract commented lines from a file using perl?
by stevieb (Canon) on Jun 26, 2015 at 15:45 UTC

    Ok. I'm not going to focus on your entire program, but I'll show you one easy-to-understand way to skip over lines between two search terms.

    #!/usr/bin/perl use warnings; use strict; my $comment = 0; while (my $line = <DATA>){ if ($line =~ m#/\*#){ $comment = 1; next; } if ($line =~ m#\*/#){ $comment = 0; next; } next if $comment; print $line; } __DATA__ this that /* comment line 1 comment line 2 comment line 3 */ the other adsf

    -stevieb

      I recommend the same logic, but let the -n switch handle the input and looping.
      Bill
Re^3: How do i search and extract commented lines from a file using perl?
by aaron_baugher (Curate) on Jun 26, 2015 at 20:23 UTC

    If I understand the problem, you want to find all files beneath a directory which contain a certain string, except ignoring the string when it falls in a comment block. Here's a solution that uses perl:

    find . -type f -print0 | while read -d $'\0' f; do if perl -0777pe 's| +/\*.*?\*/||gs' $f | grep certain_string >/dev/null; then echo $f; fi +; done

    Aaron B.
    Available for small or large Perl jobs and *nix system administration; see my home node.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1132163]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-25 23:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found