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

i want to write a script which finds .aspx file in a directory and comment out the <script> tag if it is uncommented and uncomment those <script> tag which is commented..i tried a lot but not getting proper result.i'm very new to perl script .please help me out

  • Comment on perl script to search and replace comment in .aspx file

Replies are listed 'Best First'.
Re: perl script to search and replace comment in .aspx file
by stevieb (Canon) on Jun 07, 2012 at 17:44 UTC

    Here's something that works, but it's pretty ugly with lots of if() statements. If I had more time I would do it differently and make it much more compact.

    It assumes quite a bit regarding how your code is lined up. The <%-- and --> MUST be on the same line as the <script> and </script> tags, and said tags must be the only thing on the line (and must be in the file's first column). If this is not how your code is consistently lined up, it will not work, but hopefully it is a decent enough example to get you started:

    #!/usr/bin/perl use warnings; use strict; open my $fh, '<', 'file.aspx' or die "Can't open the damn file for rea +ding!: $!"; my @file_content; while ( my $line = <$fh> ){ chomp $line; if ( $line =~ /^<script>/ ){ $line = "<%-- $line"; push @file_content, "$line\n"; next; } elsif ( $line =~ /^<\/script>$/ ){ $line .= " -->"; push @file_content, "$line\n"; next; } elsif ( $line =~ /^<%--\s+<script>/ ){ $line =~ s/^<%--\s+//; push @file_content, "$line\n"; next; } elsif ( $line =~ /^<\/script>\s+-->/ ){ $line =~ s/\s+-->//; push @file_content, "$line\n"; next; } push @file_content, "$line\n"; } close $fh; open $fh, '>', 'file.aspx' or die "Can't open the damned file for writ +ing: $!"; print $fh @file_content; close $fh;

    The file I used had these contents:

    <script> </script> <%-- <script> </script> -->

      but if my script structure is like this: <script type="java/js"> </script>,then its not working..any solution

        Any time you're parsing text, a lot depends on how much the patterns you're trying to match may vary. If your tags may or may not span multiple lines, or may have their attributes in different orders, or other variations, parsing them can be very complicated. (Which is why it's often a good idea to use a module if there is one.) If you know your open and close tags will always be on the same line, and never more than one pair on the same line, it could be pretty simple:

        perl -p -i -e 's|(<script.+/script>)|<%-- $1 -->| unless s|<%--(\s*<sc +ript.+/script>\s*)-->|$1|' *.aspx

        Aaron B.
        Available for small or large Perl jobs; see my home node.

Re: perl script to search and replace comment in .aspx file
by temporal (Pilgrim) on Jun 07, 2012 at 19:05 UTC

    You could slurp the file and do a greedy regex substitution over it.

    use strict; my $delim = $/; $/ = ''; my $file = <DATA>; $/ = $delim; $file =~ s/(<%--)?\s*<script(.*)>/($1 ? '' : '<%-- ') . "<script$2>"/i +ge; $file =~ s=</script>\s*(-->)?='</script>' . ($1 ? '' : " -->\n")=ige; print $file; __DATA__ <script type="java/js"> foobie bletch </script> <%-- <script> zelgo mer </script> -->

    Strange things are afoot at the Circle-K.

      this code not working temporal..:(..

        You might tell us how the code fails for you, and on what input, and what output you get.

Re: perl script to search and replace comment in .aspx file
by RichardK (Parson) on Jun 07, 2012 at 17:36 UTC

    You might find it useful to read perlintro for some general pointers.

Re: perl script to search and replace comment in .aspx file
by stevieb (Canon) on Jun 07, 2012 at 16:26 UTC

    Please share with us what you have tried.

      #!/usr/bin/perl my $file_name = "test.txt"; my @aspxfiles=glob('.*aspx'); foreach $file(@aspxfiles) { open(INF,$file) || die "file could not open\n"; my @line; while(@line = <INF>) { if ( $line =~ s/<\/script/<\%\-\-<script/g) { open(INF,'>>$file_name') || die "could not open in writ +e mode"; close(INF); } close(INF); } }

      this is partial coding which i tried.i'm very weak in scripting language

        You are going line-by-line through each file and replacing the string </script with the string <%--<script . I don't know aspx, but I suspect that's not quite what you want. It seems more likely that your opening comment tag would come before your <script> tag, and a closing one after your </script> tag, but you'll have to show us some sample data to know for sure.

        If that's successful, you open a file for appending, but you never write anything to it, and you never write your changed $line anywhere. Presumably you want to write it to a file, and perhaps move that file into the place of the original?

        ADDED: One more thing: by using single quotes around your output filename, you prevent interpolation, so you will open a file named $file_name, not test.txt.

        Aaron B.
        Available for small or large Perl jobs; see my home node.