...making Linux just a little more fun!
Thomas Bonham [thomasbonham at bonhamlinux.org]
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 $_ }
Thomas
Kapil Hari Paranjape [kapil at imsc.res.in]
Hello,
On Fri, 06 Jun 2008, Thomas Bonham wrote:
> I thought I would sure this little perl script that will remove the > comments out of a configuration file.
Why not just the following?
grep -v '^#.*' < input > output
Kapil. --
Thomas Bonham [thomasbonham at bonhamlinux.org]
> Why not just the following? > > grep -v '^#.*' < input > output >For the same reason I did the last one in perl and not using find. I do not like doing anything in bash and I don't remember all of those switch that goes with stuff.
I would write a 1000 line C program before I write anything in bash or just sh.
Thomas
Joey Prestia [joey at linuxamd.com]
Kapil Hari Paranjape wrote:
> Hello, > > On Fri, 06 Jun 2008, Thomas Bonham wrote: >> I thought I would sure this little perl script that will remove the >> comments out of a configuration file. > > Why not just the following? > > grep -v '^#.*' < input > output
I take it a step farther and make it executable and put it in the path of system commands like /bin/ and this way it can be called from anywhere.
[joey@xen ~]$ cat /bin/clean #!/bin/bash cd $PWD grep -v '\#' $1 | sed /'^$'/d > $1.clean
-- Joey Prestia L. G. Mirror Coordinator Joey@linuxamd.com http://linuxamd.com Main Site http://linuxgazette.net
Thomas Bonham [thomasbonham at bonhamlinux.org]
Joey Prestia wrote:
> Kapil Hari Paranjape wrote: > >> Hello, >> >> On Fri, 06 Jun 2008, Thomas Bonham wrote: >> >>> I thought I would sure this little perl script that will remove the >>> comments out of a configuration file. >>> >> Why not just the following? >> >> grep -v '^#.*' < input > output >> > I take it a step farther and make it executable and put it in the path > of system commands like /bin/ and this way it can be called from anywhere. > > [joey@xen ~]$ cat /bin/clean > #!/bin/bash > cd $PWD > grep -v '\#' $1 | sed /'^$'/d > $1.clean
I put my perl scripts in /usr/local/bin and then add that to my path on the mac and then I can run them from any where I would like to.
Thomas
Ben Okopnik [ben at linuxgazette.net]
On Fri, Jun 06, 2008 at 09:13:47AM -0700, Joey Prestia wrote:
> > I take it a step farther and make it executable and put it in the path > of system commands like /bin/ and this way it can be called from anywhere.In my opinion, it would be a mistake to add things to '/bin' - that's why we have '/usr/local/bin'.
-- * Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *
Thomas Bonham [thomasbonham at bonhamlinux.org]
Ben Okopnik wrote:
> On Fri, Jun 06, 2008 at 09:13:47AM -0700, Joey Prestia wrote: > >> I take it a step farther and make it executable and put it in the path >> of system commands like /bin/ and this way it can be called from anywhere. >> > > In my opinion, it would be a mistake to add things to '/bin' - that's > why we have '/usr/local/bin'.
I second this part here. I don't think you should put anything you write in /bin. I think everything should go into /usr/local/bin or create something with in your home directory and then add it to your path.
Thomas
Joey Prestia [joey at linuxamd.com]
Thomas Bonham wrote:
> Ben Okopnik wrote: >> On Fri, Jun 06, 2008 at 09:13:47AM -0700, Joey Prestia wrote: >> >>> I take it a step farther and make it executable and put it in the path >>> of system commands like /bin/ and this way it can be called from anywhere. >>> >> >> In my opinion, it would be a mistake to add things to '/bin' - that's >> why we have '/usr/local/bin'. >> >> >> > I second this part here. I don't think you should put anything you write > in /bin. I think everything should go into /usr/local/bin or create > something with in your home directory and then add it to your path.
Thats what I really like about this list. Is with the other input you get you can apply to your ways to improve and enhance ones ways of doing things. One must just remember to not get offended and accept the correction in the constructive way it is intended. I know I do.
-- Joey Prestia L. G. Mirror Coordinator Joey@linuxamd.com http://linuxamd.com Main Site http://linuxgazette.net
Ben Okopnik [ben at linuxgazette.net]
On Fri, Jun 06, 2008 at 09:56:50AM -0700, Joey Prestia wrote:
> > Thats what I really like about this list. Is with the other input you > get you can apply to your ways to improve and enhance ones ways of doing > things. One must just remember to not get offended and accept the > correction in the constructive way it is intended. I know I do.
Pretty smart of you to spot that, Joey - it's one of my favorite bits of TAG as well. I've learned a lot here over the years, and keep learning new things all the time. I think that continuing to learn over the long term requires courage and humility - the ability to say "I don't know." A lot of people miss that part and stay stuck right where they are.
-- * Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *
Thomas Adam [thomas.adam22 at gmail.com]
2008/6/6 Ben Okopnik <ben@linuxgazette.net>:
> On Fri, Jun 06, 2008 at 09:13:47AM -0700, Joey Prestia wrote: >> >> I take it a step farther and make it executable and put it in the path >> of system commands like /bin/ and this way it can be called from anywhere. > > In my opinion, it would be a mistake to add things to '/bin' - that's > why we have '/usr/local/bin'.
Or "~/bin" -- neither that nor /usr/local/bin are generally in the global PATH set by any distro that I am aware of, so setting both of them is trivial, IMO.
-- Thomas Adam
Ben Okopnik [ben at linuxgazette.net]
On Sun, Jun 08, 2008 at 01:11:38PM +0100, Thomas Adam wrote:
> 2008/6/6 Ben Okopnik <ben@linuxgazette.net>: > > On Fri, Jun 06, 2008 at 09:13:47AM -0700, Joey Prestia wrote: > >> > >> I take it a step farther and make it executable and put it in the path > >> of system commands like /bin/ and this way it can be called from anywhere. > > > > In my opinion, it would be a mistake to add things to '/bin' - that's > > why we have '/usr/local/bin'. > > Or "~/bin" -- neither that nor /usr/local/bin are generally in the > global PATH set by any distro that I am aware of, so setting both of > them is trivial, IMO.
There's a different purpose to each of those, of course. For any programs that I'm only going to use for myself, there's '~/bin'; for anything that I want to share with the other users on the system, '/usr/local/bin' is the way to go. I tend to presume the latter in most situations - and write my scripts with that in mind.
-- * Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *
Neil Youngman [ny at youngman.org.uk]
On Friday 06 June 2008 17:13, Joey Prestia wrote:
> Kapil Hari Paranjape wrote: > > Hello, > > > > On Fri, 06 Jun 2008, Thomas Bonham wrote: > >> I thought I would sure this little perl script that will remove the > >> comments out of a configuration file. > > > > Why not just the following? > > > > grep -v '^#.*' < input > output
The '.*' seems superfluous to me.
grep -v '^#'
> > Kapil. > > -- > > I take it a step farther and make it executable and put it in the path > of system commands like /bin/ and this way it can be called from anywhere.
Maybe an alias or a shell function if it's just a one liner?
Neil
Ben Okopnik [ben at linuxgazette.net]
On Fri, Jun 06, 2008 at 09:27:34PM +0530, Kapil Hari Paranjape wrote:
> Hello, > > On Fri, 06 Jun 2008, Thomas Bonham wrote: > > I thought I would sure this little perl script that will remove the > > comments out of a configuration file. > > Why not just the following? > > grep -v '^#.*' < input > output
Or perhaps 'egrep -v '^[ ]*(#|$)' < input > output' (the stuff between the brackets is a space and a tab) - but that still doesn't fix the original file.
-- * Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *
Ben Okopnik [ben at linuxgazette.net]
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 *
Thomas Bonham [thomasbonham at bonhamlinux.org]
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
Ben Okopnik [ben at linuxgazette.net]
On Fri, Jun 06, 2008 at 10:03:52AM -0700, Thomas Bonham wrote:
> Ben Okopnik wrote: > > > > 1) What about indented comments? This script would miss them. > > > This is one designed for configuration files like httpd.conf, snmpd.conf.
Why not make it as generally applicable as possible rather than restricting it? If you're going to build a tool, make is as robust as possible. You'll find that this is usually a matter of a tiny incremental change - which is exactly what it is in this case.
> > 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.
In that case, you can always use the one-liner I cited without the '-i' option. This will allow you to redirect the output to a file, or pipe it to, say, 'less' so you can examine it.
-- * Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *