| View previous topic :: View next topic |
| Author |
Message |
wishkah Guru


Joined: 09 May 2003 Posts: 441 Location: de
|
Posted: Thu Jul 24, 2003 7:58 am Post subject: Comment-killer script |
|
|
Ok, this is a really really simple script. It can be used to remove those annoying comments that xf86config writes into the XF86Config file. It is really really simple, but since people keep saying "share everything", here it is (did I mention it is really basic? ).
| Code: |
#!/usr/bin/perl
#comment killer
$file1 = shift @ARGV;
$file2 = shift @ARGV;
$verbose = 0;
$write_empty_lines = 1;
if($file1 eq "-h" || $file1 eq "--help")
{
print "Comment killer usage:\n comkill [-v] [-ne] FILE_IN FILE_OUT\n -ne Don't copy empty lines\n -v Verbose mode\n";
exit(0);
}
if($file1 eq "-v"){
$verbose = 1;
$file1 = $file2;
$file2 = shift @ARGV;
}
if($file1 eq "-ne"){
print "Not writing empty lines\n";
$write_empty_lines = 0;
$file1 = $file2;
$file2 = shift @ARGV;
}
print "Opening and reading '$file1'... ";
open (FILE_IN, $file1) or die (print $!);
@lines = <FILE_IN>;
close (FILE_IN);
print "done\n";
print "Opening '$file2'... ";
open (FILE_OUT, ">$file2") or die (print $!);
print "done\n";
print "Reading and writing contents... ";
if($verbose) { print "\n"; }
$no_com = 0;
foreach (@lines)
{
if(!m/^[\n\r]/ || $write_empty_lines){
if(!m/^\#/) {
print FILE_OUT $_;
$no_com++;
if($verbose) {
print "Found not commented line: $_";
}
}
}
}
close (FILE_OUT);
print "done\n";
print "Not-commented lines: " . $no_com . "\n";
|
Maybe someone who wants to learn some perl basics can use it... _________________ if only I could fill my heart with love... |
|
| Back to top |
|
 |
ozukir@ Apprentice


Joined: 14 Oct 2002 Posts: 209 Location: USA
|
Posted: Thu Jul 24, 2003 8:43 am Post subject: |
|
|
And this:
| Code: | $ sed -e '/^#| *#/d' /etc/X11/XF86Config > file
# mv file /etc/X11/XF86Config |
Its not perl, but nearly as fun. |
|
| Back to top |
|
 |
wishkah Guru


Joined: 09 May 2003 Posts: 441 Location: de
|
Posted: Thu Jul 24, 2003 9:10 am Post subject: |
|
|
D'OH!
 _________________ if only I could fill my heart with love... |
|
| Back to top |
|
 |
Unne l33t


Joined: 21 Jul 2003 Posts: 616
|
Posted: Thu Jul 24, 2003 5:36 pm Post subject: |
|
|
| Code: | | perl -p -i.bak -e 's/^#.*?\n|^\s*$//' /etc/X11/XF86config |
TMTOWTDI though, right?  |
|
| Back to top |
|
 |
razamatan Apprentice


Joined: 28 Feb 2003 Posts: 155
|
Posted: Thu Jul 24, 2003 10:33 pm Post subject: |
|
|
throw a -i switch in there to inline edit the file using sed... very simple then..
| Code: | | $ sed -i -e '/^#| *#/d' /etc/X11/XF86Config |
| ozukir@ wrote: | And this:
| Code: | $ sed -e '/^#| *#/d' /etc/X11/XF86Config > file
# mv file /etc/X11/XF86Config |
Its not perl, but nearly as fun. |
_________________ a razamatan doth speaketh,
"Never attribute to malice, that which can be adequately explained by stupidity" |
|
| Back to top |
|
 |
ozukir@ Apprentice


Joined: 14 Oct 2002 Posts: 209 Location: USA
|
Posted: Fri Jul 25, 2003 5:40 am Post subject: |
|
|
My version of sed (3.02, on RedHat AS 2.1) doen't have that switch and its not in my man page or my O'reilly sed & awk book. | Quote: | | throw a -i switch in there to inline edit the file using sed... very simple then.. |
I'll check my Gentoo system at home. What version are you using, looks like a new feature. |
|
| Back to top |
|
 |
razamatan Apprentice


Joined: 28 Feb 2003 Posts: 155
|
Posted: Fri Jul 25, 2003 6:00 am Post subject: |
|
|
| Code: | $ sed --version
GNU sed version 4.0.7
Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE,
to the extent permitted by law.
$ uname -a
Linux mustafa 2.4.20-gentoo-r5 #2 Tue Jun 10 18:46:38 EDT 2003 i686 Pentium III (Katmai) GenuineIntel GNU/Linux |
| ozukir@ wrote: | My version of sed (3.02, on RedHat AS 2.1) doen't have that switch and its not in my man page or my O'reilly sed & awk book. | Quote: | | throw a -i switch in there to inline edit the file using sed... very simple then.. |
I'll check my Gentoo system at home. What version are you using, looks like a new feature. |
_________________ a razamatan doth speaketh,
"Never attribute to malice, that which can be adequately explained by stupidity" |
|
| Back to top |
|
 |
ClaesBas n00b

Joined: 20 Jul 2002 Posts: 38 Location: Stockholm
|
Posted: Sat Jul 26, 2003 10:14 am Post subject: Another way is grep.... |
|
|
| Code: |
grep -v "^#" /etc/X11/XF86Config > file && mv file /etc/X11/XF86Config
|
or
| Code: |
grep -v "^#" /etc/X11/XF86Config | grep -v "^$" > file && mv file /etc/X11/XF86Config
|
|
|
| Back to top |
|
 |
think4urs11 Administrator


Joined: 25 Jun 2003 Posts: 6659 Location: above the cloud
|
Posted: Sat Jul 26, 2003 10:29 am Post subject: |
|
|
@Claes
at first i thought about that grep too, but there is a big difference in...
the grep doesn't kill empty lines (with -v "^#", so this makes the sed the shortest way to get 'clean' configs _________________ Nothing is secure / Security is always a trade-off with usability / Do not assume anything / Trust no-one, nothing / Paranoia is your friend / Think for yourself |
|
| Back to top |
|
 |
zhenlin Veteran

Joined: 09 Nov 2002 Posts: 1361
|
Posted: Sat Jul 26, 2003 1:27 pm Post subject: |
|
|
Warning: Do not use any of the following on your "Whitespace" programs!
"To Kill Comments"
"To Kill Empty Lines"
| Code: |
grep -v $'^[ \t]*$'
|
"To Kill Empty Lines, Locale Aware Edition"
| Code: |
grep -v '^[[:space:]]*$'
|
"To Kill Redundant Horizontal Whitespace"
| Code: |
sed 's:[[:space:]][[:space:]]*: :g'
|
"To Kill Useless Characters Effectively"
| Code: |
sed 's:#.*::g;s:[[:space:]][[:space:]]*: :g'|grep -v '^[[:space:]]*$'
|
For some reason, "sed -e '/^#| *#/d' /etc/X11/XF86Config" doesn't work here. |
|
| Back to top |
|
 |
ozukir@ Apprentice


Joined: 14 Oct 2002 Posts: 209 Location: USA
|
Posted: Mon Jul 28, 2003 5:03 am Post subject: |
|
|
D'OH
Updated:
| Code: | $ sed -e '/^#\|^[\t ]*#\|^$/d' /etc/X11/XF86Config > file
# mv file /etc/X11/XF86Config |
Sorry, missing the "\" in front of my "|". This one zonks blank lines too. |
|
| Back to top |
|
 |
|