[TAG] 2cent tip
Thomas Bonham
thomasbonham at bonhamlinux.org
Fri Jun 6 21:03:52 MSD 2008
Ben Okopnik wrote:
> On Fri, Jun 06, 2008 at 07:51:55AM -0700, Thomas Bonham wrote:
>
>> Hi All,
>>
>> I thought I would sure this little perl script that will remove the
>> comments out of a configuration file.
>>
>> #!/usr/bin/perl -w
>> # Thomas Bonham
>> # 06/06/08
>>
>> if($#ARGV !=0) {
>> print "usage: path to the configuration\n";
>> exit;
>> }
>> $fileName=$ARGV[0];
>> open(O,"<$fileName") || die($!);
>> open(N,">$fileName.free") || die($!);
>> while(<O>) {
>> next if($_ =~/^#.*/) ;
>> print N $_
>> }
>>
>
> This, unfortunately, has several problems.
>
> 1) What about indented comments? This script would miss them.
>
This is one designed for configuration files like httpd.conf, snmpd.conf.
> 2) While you're at it, you might as well remove blank lines.
>
I never thought of that part there.
> 3) It creates a file ('original_name.free') instead of just fixing the
> original file.
>
>
This is how I want it to be because of the fact that a lot of the times
I just want to see what is active in the config file or if I just would
like to work with something that doesn't have all of that trash in the
file. If you want to use it after clean it off you will just have to
move it over your self.
> Here's a one-liner that addresses all of the above, as well as keeping
> a backup of the original (it'll have a '~' as an extension):
>
> ``
> perl -i~ -wne'print unless /^\s*(?:#|$)/' filename
> ''
>
Thank you for that I will play with it later.
Thomas
More information about the TAG
mailing list