Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Comment-killer script
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
wishkah
Guru
Guru


Joined: 09 May 2003
Posts: 441
Location: de

PostPosted: Thu Jul 24, 2003 7:58 am    Post subject: Comment-killer script Reply with quote

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? :wink:).

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
View user's profile Send private message
ozukir@
Apprentice
Apprentice


Joined: 14 Oct 2002
Posts: 209
Location: USA

PostPosted: Thu Jul 24, 2003 8:43 am    Post subject: Reply with quote

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
View user's profile Send private message
wishkah
Guru
Guru


Joined: 09 May 2003
Posts: 441
Location: de

PostPosted: Thu Jul 24, 2003 9:10 am    Post subject: Reply with quote

D'OH!

:roll:
_________________
if only I could fill my heart with love...
Back to top
View user's profile Send private message
Unne
l33t
l33t


Joined: 21 Jul 2003
Posts: 616

PostPosted: Thu Jul 24, 2003 5:36 pm    Post subject: Reply with quote

Code:
perl -p -i.bak -e 's/^#.*?\n|^\s*$//' /etc/X11/XF86config


TMTOWTDI though, right? :)
Back to top
View user's profile Send private message
razamatan
Apprentice
Apprentice


Joined: 28 Feb 2003
Posts: 160

PostPosted: Thu Jul 24, 2003 10:33 pm    Post subject: Reply with quote

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
View user's profile Send private message
ozukir@
Apprentice
Apprentice


Joined: 14 Oct 2002
Posts: 209
Location: USA

PostPosted: Fri Jul 25, 2003 5:40 am    Post subject: Reply with quote

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
View user's profile Send private message
razamatan
Apprentice
Apprentice


Joined: 28 Feb 2003
Posts: 160

PostPosted: Fri Jul 25, 2003 6:00 am    Post subject: Reply with quote

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
View user's profile Send private message
ClaesBas
n00b
n00b


Joined: 20 Jul 2002
Posts: 38
Location: Stockholm

PostPosted: Sat Jul 26, 2003 10:14 am    Post subject: Another way is grep.... Reply with quote

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
View user's profile Send private message
think4urs11
Bodhisattva
Bodhisattva


Joined: 25 Jun 2003
Posts: 6659
Location: above the cloud

PostPosted: Sat Jul 26, 2003 10:29 am    Post subject: Reply with quote

@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
View user's profile Send private message
zhenlin
Veteran
Veteran


Joined: 09 Nov 2002
Posts: 1361

PostPosted: Sat Jul 26, 2003 1:27 pm    Post subject: Reply with quote

Warning: Do not use any of the following on your "Whitespace" programs!

"To Kill Comments"
Code:

sed 's:#.*$::g'


"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
View user's profile Send private message
ozukir@
Apprentice
Apprentice


Joined: 14 Oct 2002
Posts: 209
Location: USA

PostPosted: Mon Jul 28, 2003 5:03 am    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum