[TAG] 2cent tip
Ben Okopnik
ben at linuxgazette.net
Fri Jun 6 20:28:09 MSD 2008
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.
2) While you're at it, you might as well remove blank lines.
3) It creates a file ('original_name.free') instead of just fixing the
original file.
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
''
You could also do something similar with 'sed', which also supports a
'-i' (in-place edit) option.
--
* Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *
More information about the TAG
mailing list