sravan937 has asked for the wisdom of the Perl Monks concerning the following question:

I am new to Perl programming. Need your help in searching and extracting only commented lines from a file. Below is an example:

This is line 1 /*This is line 2 This is line 3 This is line 4*/ This is line 5

I just want to search and extract only commented lines from above file.

  • Comment on How do i search and extract commented lines from a file using perl?
  • Download Code

Replies are listed 'Best First'.
Re: How do i search and extract commented lines from a file using perl?
by toolic (Bishop) on Jun 26, 2015 at 00:50 UTC

      Just a thought -- while it may be your umpteenth incident of encountering an unannounced cross-posting, it could well be the OP's first time even seeking help, much less seeking it in more than one place.

      While it does make a sort of sense now that I've been exposed to it, there is no way I could have ever predicted that cross-posting without announcing such was considered rude; I learned that here from comments folks made in response to that activity by others.

      It may make sense, but it is not obvious to everyone; and newcomers in particular seem most likely to be the ones who won't know.

      Geeeez....i cannot post on multiple blogs?

        You're welcome to do so, however in the interest of people not wasting time providing a solution you may have already been given elsewhere, it's wise to post a link to other places where you've asked the question. This also helps people browsing in the future who have a similar problem to you. If the answer is not here, one may exist on other sites where you've asked it.

Re: How do i search and extract commented lines from a file using perl?
by stevieb (Canon) on Jun 25, 2015 at 23:11 UTC

    Welcome to PerlMonks!

    Please show us what you've tried so far. Post the code that you've written between <code></code> tags, and we'll help you reach a solution.

    Cheers,

    -stevieb

      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()

        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

        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.

Re: How do i search and extract commented lines from a file using perl?
by 1nickt (Canon) on Jun 25, 2015 at 23:46 UTC

    Hello Srvan937,

    If you are new to Perl but you are familiar with programming in some other language, it won't take you long to figure out how to do what you want in Perl. Perl is actually really good at this and makes it easy for you to do it.

    If you are not only new to Perl but new to programming, then you really only have two avenues open to you.

    1. Pay someone to do the task for you, remembering that if you pay $0.00 then that will probably be the approximate worth of the work you receive.
    2. Put in the work yourself to learn how to do the task. As I said, Perl makes this sort of thing very easy, so once you do the work of learning how, actually completing your task will be trivial.

    Rather than ask here for a quick answer, you should visit perl.org and start with one of the basic tutorials there. Or get the book Beginning Perl, or read it on line for free at https://www.perl.org/books/beginning-perl/. Or visit http://www.perlmaven.com and do Gabor's basic tutorial. Etc.

    Any of these beginner's guides will have you reading in lines from a file and finding text patterns within a couple of days at the most. It's one of the most common things that people come to Perl wanting to do, so you'll find it early on in any tutorial.

    Go, teach yourself the basics, and then when you have some code that you've written that won't do what you want, come back and you'll definitely get help with your questions!

Re: How do i search and extract commented lines from a file using perl?
by Anonymous Monk on Jun 26, 2015 at 07:23 UTC
Re: How do i search and extract commented lines from a file using perl?
by Anonymous Monk on Jun 26, 2015 at 21:05 UTC

    Just for fun, a quick one-liner solution using perl's "flip-flop" (range) operator.

    $ cat 1132036.txt This is line 1 /*This is line 2 This is line 3 This is line 4*/ This is line 5 $ perl -ne 'print unless /\/\*/ .. /\*\//' 1132036.txt This is line 1 This is line 5

    Note that like several of the other solutions presented by other monks, this doesn't fully handle the case of the comment markers appearing in the middle of the lines.

A reply falls below the community's threshold of quality. You may see it by logging in.