Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

regex basics

by jott19 (Initiate)
on Jan 31, 2015 at 07:57 UTC ( [id://1115169]=perlquestion: print w/replies, xml ) Need Help??

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

new to perl... I want to purge my php.ini file of all comments, mostly.

some comments are commented directives that look like this:

;some_directive

explanatory comments look like this:

; this directive does such and such

I want to keep the commented directives and purge the explanatory comments.

1 #!/usr/bin/perl 2 $iniprod = "php.ini-production"; 3 $ininocm = "php.ini-nocomments"; 4 open(NOCM, ">$ininocm"); 5 $txt = perl -ne 's/;\s.*// $iniprod'; 6 print NOCM $txt; 7 close NOCM or die "Could not close $ininocm";

I'm trying to assign the results of the regex purge to $txt, then write it to a new file.

any help appreciated... thx

Replies are listed 'Best First'.
Re: regex basics
by CountZero (Bishop) on Jan 31, 2015 at 08:18 UTC
    Welcome to the world of Perl!

    Strange (or not) as it may be, "perl" is not a keyword within Perl! You are actually mixing up the Perl "one liner" style and proper scripting style.

    Script:

    use Modern::Perl qw /2014/; my $iniprod = 'php.ini-production'; my $ininocm = 'php.ini-nocomments'; open my $IN, '<', $iniprod or die "Could not open $iniprod for readin +g: $!"; open my $OUT, '>', $ininocm or die "Could not open $ininocm for writin +g: $!"; while (<$IN>) { print $OUT $_ unless /;\s+/ ; } close $IN or die "Error closing $iniprod: $!"; close $OUT or die "Error closing $ininocm: $!";

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
      print $OUT $_ unless /;\s+/ ;

      I'm not familiar with the syntax or conventions of PHP .ini files, but wouldn't the quoted statement eliminate lines of the format
          some_directive ; this directive does such and such
      also?


      Give a man a fish:  <%-(-(-(-<

        That is very well possible. I just used the basic regex in the OP's post. /^;\s+/ is probably better, but I don't know PHP enough to be sure.

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        My blog: Imperial Deltronics

      Hi CountZero and thanks for the reply.

      I tried this (using CentOS 6.6):

      #!/usr/bin/env perl use Modern::Perl qw /2014/; my $iniprod = 'php.ini-production'; my $ininocm = 'php.ini-nocomments'; open my $IN, '<', $iniprod or die "Could not open $iniprod for readin +g: $!"; open my $OUT, '>', $ininocm or die "Could not open $ininocm for writin +g: $!"; while (<$IN>) { print $OUT $_ unless /;\s+/ ; } close $IN or die "Error closing $iniprod: $!"; close $OUT or die "Error closing $ininocm: $!";
      results:
      [/etc/php.d] # ./php.ini-remove-comments.pl Can't locate Modern/Perl.pm in @INC (@INC contains: /usr/local/lib64/p +erl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/p +erl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at ./php.ini-re +move-comments.pl line 2. BEGIN failed--compilation aborted at ./php.ini-remove-comments.pl line + 2.
      I checked...
      [/etc/php.d] # perl -v This is perl, v5.10.1 (*) built for x86_64-linux-thread-multi

      The release date for 5.10.1 was 2009-Aug-06. Apparently a lot has changed in 5 years... latest version at 5.21.3?

      I commented 'use Modern::Perl qw /2014/;' and it worked perfectly--except it left blank lines like tombstones where the comments used to be.

      I thought another pass would do it:
      1 #!/usr/bin/env perl 2 # use Modern::Perl qw /2014/; 3 4 my $iniprod = 'php.ini-production'; 5 my $ininocm = 'php.ini-nocomments'; 6 7 open my $IN, '<', $iniprod or die "Could not open $iniprod fo +r reading: $!"; 8 open my $OUT, '>', $ininocm or die "Could not open $ininocm fo +r writing: $!"; 9 10 while (<$IN>) { 11 print $OUT $_ unless /;\s+/; 12 } 13 while (<$OUT>) { 14 print $OUT $_ unless /\n+/; 15 } 16 close $IN or die "Error closing $iniprod: $!"; 17 close $OUT or die "Error closing $ininocm: $!"; 18
      did not work. I tried to come up with something like this...
      #!/usr/bin/env perl # use Modern::Perl qw /2014/; my $iniprod = 'php.ini-production'; my $ininocm = 'php.ini-nocomments'; open my $IN, '<', $iniprod or die "Could not open $iniprod for readin +g: $!"; open my $OUT, '>', $ininocm or die "Could not open $ininocm for writin +g: $!"; while (<$IN>) { if (!$_ /;\s+/) { print $OUT $_ } } close $IN or die "Error closing $iniprod: $!"; close $OUT or die "Error closing $ininocm: $!";

      but I'm getting an error...

      Substitution pattern not terminated

      Is this any different form 'print OUT $_ unless...' ?

      Should I use chomp here?

        Try
        #!/usr/bin/env perl use strict; use warnings; my $iniprod = 'php.ini-production'; my $ininocm = 'php.ini-nocomments'; my $log = 'removed.log'; open my $IN, '<', $iniprod or die "Could not open $iniprod for reading: $!"; open my $LOG, '>', $log or die "Could not open $log for writing: $!"; my $count=0; my $text; while (<$IN>) { ++$count; if (/;\s+/) { print $LOG "$count : $_"; } else { $text .= $_; } } close $IN or die "Error closing $iniprod: $!"; # squash multiple blank lines to one $text =~ s{\n{3,}}{\n\n}gm; open my $OUT, '>', $ininocm or die "Could not open $ininocm for writing: $!"; print $OUT $text; close $OUT or die "Error closing $ininocm: $!"; __DATA__ line 1 ; this directive does such and such ; and some more ;some_directive line 2 line 3 line 4 line 5
        poj
        Modern::Perl is indeed for more recent versions of Perl. I'm running Perl 5.18. You can safely delete it in this script and add use strict; use warnings; in its place.

        Those blank lines you see are mysterious. There shouldn't be any such lines as nothing is printed when a comment is found.

        !$_ /;\s+/ is invalid syntax and confused the Perl-interpreter hence the rather unhelpful error message.

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        My blog: Imperial Deltronics
Re: regex basics
by Anonymous Monk on Jan 31, 2015 at 10:02 UTC
Re: regex basics
by karlgoethebier (Abbot) on Feb 02, 2015 at 15:23 UTC

    I suggest this:

    #!/usr/bin/perl while(<>) { next if /;{2,}/; next if /;\s{1}/; print unless /^\n/; } __END__ strip_ini.pl php.ini [PHP] ;user_ini.filename = ".user.ini" ;user_ini.filename = ;user_ini.cache_ttl = 300 engine = On short_open_tag = Off asp_tags = Off precision = 14 y2k_compliance = On output_buffering = 4096 ;output_handler = zlib.output_compression = Off ;zlib.output_compression_level = -1 ...about 300 lines more

    Usage: strip_ini.pl php.ini > php.stripped.ini

    Note:

    ;;;;;;;;;;;;;;;;;;; <-- the first next skips the ASCI art ; About php.ini ; <-- the second next skips the explanations ;;;;;;;;;;;;;;;;;;;
    The print unless /^\n/ prevents unwanted printing of emtpy lines. I don't know for the moment why they are there...

    Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

      I think you're correct - the ascii art lines (;;;) SHOULD evaluate to TRUE using ($_ !~ /;\s+/). But the art is NOT present in the output (with the latest posted code)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-03-28 18:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found