Forums

Skip to content

Advanced search
  • Quick links
    • Unanswered topics
    • Active topics
    • Search
  • FAQ
  • Login
  • Register
  • Board index Discussion & Documentation Documentation, Tips & Tricks
  • Search

TIP Strip comments when posting config files

Unofficial documentation for various parts of Gentoo Linux. Note: This is not a support forum.
Post Reply
Advanced search
41 posts
  • 1
  • 2
  • Next
Author
Message
tomk
Bodhisattva
Bodhisattva
User avatar
Posts: 7221
Joined: Tue Sep 23, 2003 1:41 pm
Location: Sat in front of my computer

TIP Strip comments when posting config files

  • Quote

Post by tomk » Sun Apr 11, 2004 2:22 pm

I got tired of searching through long config files which have loads of comments looking for some option I'd set. It also annoys me when someone posts their huge config files which are mainly comments, not only does it take up valuable database space it also makes it harder to see what the problem is.

So I wrote a small script called confcat which will just show you the important parts of your config files.

Code: Select all

#!/bin/bash
# Code to cat a config file removing all comments and blank lines.
 
grep -vh '^[[:space:]]*\(#\|$\)' "$@"
Save this to a file called confcat in your $PATH (It's in ~/bin on my box), make it executable:

Code: Select all

chmod 755 /path/to/confcat
Then use it like cat:

Code: Select all

confcat /path/to/configfile
Basically it strips and lines beginning with a hash '#' and any empty lines.

I know it doesn't look much, but I ran a few tests to see how many lines are outputted:

Code: Select all

$ confcat /etc/make.conf | wc -l
      9
$ cat /etc/make.conf | wc -l
    156
$ confcat /etc/X11/XF86Config | wc -l 
     82
$ cat /etc/X11/XF86Config | wc -l
    462
$ confcat /etc/mutt/Muttrc | wc -l
     19
$ cat /etc/mutt/Muttrc | wc -l
   3690
$ confcat /usr/src/linux/.config | wc -l
    200
$ cat /usr/src/linux/.config | wc -l
   1119
And an example of it in action:

Code: Select all

$ confcat /etc/make.conf
USE="-* x86 crypt berkdb pam ncurses readline ssl tcltk tcpd X mmx xml2 \
truetype java gtk jpeg tiff png python 3dfx voodoo3 opengl imap apache2 \
postgres sasl"
CHOST="i686-pc-linux-gnu"
CFLAGS="-O3 -march=pentium3 -fprefetch-loop-arrays -funroll-loops -pipe"
CXXFLAGS="${CFLAGS}"
PORTDIR_OVERLAY="/usr/local/portage"
GENTOO_MIRRORS="http://www.mirror.ac.uk/sites/www.ibiblio.org/gentoo/"
SYNC="rsync://rsync.uk.gentoo.org/gentoo-portage"
Hopefully someone will find it useful.

Edit: made it so it includes comments that appear after whitespace at the beginning of the line, lines that only have whitespace and so that it only uses one grep process
Last edited by tomk on Fri Nov 24, 2006 11:59 pm, edited 2 times in total.
Search | Read | [topic=119906]Answer[/topic] | [topic=28820]Report[/topic] | [topic=160179]Strip[/topic]
Top
ikaro
Advocate
Advocate
User avatar
Posts: 2527
Joined: Mon Jul 14, 2003 2:04 pm
Location: Denmark

  • Quote

Post by ikaro » Sun Apr 11, 2004 2:53 pm

Cool tip.
I had once something similar for vim.
Thanks.
linux: #232767
Top
Earthwings
Bodhisattva
Bodhisattva
User avatar
Posts: 7753
Joined: Mon Apr 14, 2003 8:13 pm
Location: Germany

  • Quote

Post by Earthwings » Sun Apr 11, 2004 3:27 pm

Nice. Extending it to strip whitespace leaded comments saves me another 70 lines for XF86Config.

Code: Select all

sed -e '/^\(\t\| \)*#.*\|^\(\t\| \)*$/d' -e 's/\(.*\)#.*/\1/' "$@"
This also strips comments at the end of a line, though it looks a little bit ugly :o
Top
tomk
Bodhisattva
Bodhisattva
User avatar
Posts: 7221
Joined: Tue Sep 23, 2003 1:41 pm
Location: Sat in front of my computer

  • Quote

Post by tomk » Sun Apr 11, 2004 8:19 pm

Earthwings wrote:Extending it to strip whitespace leaded comments saves me another 70 lines for XF86Config.
Good idea. I've obviously got some more sed to learn, here's how to do it with grep.

Code: Select all

#!/bin/bash
# Code to cat a config file removing all comments and blank lines.
 
grep -vh '^[[:space:]]*#' "$@" | grep -v '^$' 
Search | Read | [topic=119906]Answer[/topic] | [topic=28820]Report[/topic] | [topic=160179]Strip[/topic]
Top
agmoe
n00b
n00b
Posts: 8
Joined: Tue Apr 01, 2003 4:48 pm
Location: Sweden

  • Quote

Post by agmoe » Mon Apr 12, 2004 1:07 am

Nice, I've been thinking of something like this for a while after going trough some XF86Configs generated with fglrxconfig, containing 95% comments, I've been to lazy to try to do anything myself though :)
God apa gavs galna anlag svag apa dog
Top
water
Guru
Guru
User avatar
Posts: 387
Joined: Wed Jun 19, 2002 9:56 am
Location: Zierikzee, The Netherlands

  • Quote

Post by water » Tue Apr 13, 2004 9:24 am

I've seen something before at this subforum, but it's a good thing.
Groeten uit Holland
Top
Boohbah
Apprentice
Apprentice
User avatar
Posts: 250
Joined: Fri Oct 17, 2003 11:41 pm
Location: Seattle

  • Quote

Post by Boohbah » Tue Apr 13, 2004 9:37 am

I do the same thing with a perl script i whipped up one night to take the comments out of my kernel .config.

Code: Select all

#!/usr/bin/perl
while(<>) {
        print unless m/^#/ or m/^\s+/;
}
EDIT:
Mine's not as cool as yours though as it only removes comments at the beginning of the line :oops:
Never try to explain computers to a layman. It's easier to explain sex to a virgin.
-- Robert Heinlein

(Note, however, that virgins tend to know a lot about computers.)
Top
ett_gramse_nap
Apprentice
Apprentice
User avatar
Posts: 252
Joined: Wed Oct 01, 2003 6:54 am
Location: Göteborg, Sweden

  • Quote

Post by ett_gramse_nap » Tue Apr 13, 2004 10:20 am

Thanks! I've thought of this at least a douzen of times but never managed to do something about it...
Don't bother!
Top
Boohbah
Apprentice
Apprentice
User avatar
Posts: 250
Joined: Fri Oct 17, 2003 11:41 pm
Location: Seattle

  • Quote

Post by Boohbah » Wed Apr 14, 2004 12:15 am

While my script works fine for kernel configs, it doesn't work for X configs because i foolishly remove every line that starts with whitespace :oops: Here is my new and improved version that works by anchoring the whitespace to the whole line:

Code: Select all

#!/usr/bin/perl       
while(<>) { print unless m/^#/ or m/^\s+$/; }
Now i feel better :wink:
Never try to explain computers to a layman. It's easier to explain sex to a virgin.
-- Robert Heinlein

(Note, however, that virgins tend to know a lot about computers.)
Top
Boohbah
Apprentice
Apprentice
User avatar
Posts: 250
Joined: Fri Oct 17, 2003 11:41 pm
Location: Seattle

  • Quote

Post by Boohbah » Wed Apr 14, 2004 12:20 am

In further testing i realized my script doesn't catch lines beginning with whitespace before a comment, so here is my newer and even more improved version.

Code: Select all

#!/usr/bin/perl
while(<>) { print unless m/^\s*#/ or m/^\s+$/; }
Yes, i am a perl n00b :lol:
Never try to explain computers to a layman. It's easier to explain sex to a virgin.
-- Robert Heinlein

(Note, however, that virgins tend to know a lot about computers.)
Top
ecatmur
Advocate
Advocate
User avatar
Posts: 3595
Joined: Mon Oct 20, 2003 8:07 pm
Location: Edinburgh
Contact:
Contact ecatmur
Website

  • Quote

Post by ecatmur » Wed Apr 14, 2004 1:36 am

A shorter sed version:

Code: Select all

#!/bin/sed -f
s/#.*//
/^\s*$/d
or

Code: Select all

sed -e 's/#.*//;/^\s*$/d' "$@"
No more cruft
dep: Revdeps that work
Using command-line ACCEPT_KEYWORDS?
Top
Genone
Retired Dev
Retired Dev
User avatar
Posts: 9656
Joined: Fri Mar 14, 2003 6:02 pm
Location: beyond the rim

  • Quote

Post by Genone » Sun May 02, 2004 1:52 pm

Nice thing, but for make.conf I still recommend `emerge --info`.
Top
Shan
Guru
Guru
User avatar
Posts: 558
Joined: Tue Nov 04, 2003 7:16 pm
Location: /dev/null

  • Quote

Post by Shan » Sat Jun 26, 2004 2:27 am

Not meaning to wake up the dead here, but wouldn't it just be easier to put this as an alias in your /etc/profile ? eg

Code: Select all

alias confcat="sed -e 's/#.*//;/^\s*$/d' "$@""
and you can still run it the same way as if you'd put it in a script and chmodded it and all, but it just saves you from having to make another executable file.
{ NO -U } { STRIP }
{ TINY }
Top
Jazz
Guru
Guru
User avatar
Posts: 543
Joined: Sun Nov 16, 2003 10:50 pm
Location: Melbourne, Australia

  • Quote

Post by Jazz » Sat Jun 26, 2004 4:34 am

emerge info sux, it also shows things that arent even present in the make.conf, that creates a lot of confusion
In 2010, M$ Windows will be a quantum processing emulation layer for a 128-bit mod of a 64-bit hack of a 32-bit patch to a 16-bit GUI for an 8-bit operating system written for a 4-bit processor from a 2-bit company that can't stand 1 bit of competition.
Top
Genone
Retired Dev
Retired Dev
User avatar
Posts: 9656
Joined: Fri Mar 14, 2003 6:02 pm
Location: beyond the rim

  • Quote

Post by Genone » Sat Jun 26, 2004 2:38 pm

Jazz wrote:emerge info sux, it also shows things that arent even present in the make.conf, that creates a lot of confusion
Well, we depend on that information for solving bugs (things like kernel version, used profile, gcc/libc versions, ...)
Top
Shan
Guru
Guru
User avatar
Posts: 558
Joined: Tue Nov 04, 2003 7:16 pm
Location: /dev/null

AR

  • Quote

Post by Shan » Sun Jun 27, 2004 11:23 pm

Jazz wrote:emerge info sux, it also shows things that arent even present in the make.conf, that creates a lot of confusion
That may be so, but those things that are added are needed for proper debugging / help purposes. For example, it shows your currently running kernel, installed gcc and glibc, as well as automake / autoconf and your baselayout. Tell me how those are "bad" things that are shown?

For instance Not every user "knows" that certain versions of the (now old) KDE3.2 betas had trouble with the ARCH version of autoconf or automake, so when they posted questions, they get a faster answer if they include the output from emerge --info, which shows what version they're using, and thus allows the person replying to simply say "Upgrade auto* to ~ARCH"

And for the record, I answerd my own earlier post asking if it could be used as an alias. It can, and works just the same (atleast with the one ecatmur posted)
{ NO -U } { STRIP }
{ TINY }
Top
tomk
Bodhisattva
Bodhisattva
User avatar
Posts: 7221
Joined: Tue Sep 23, 2003 1:41 pm
Location: Sat in front of my computer

  • Quote

Post by tomk » Mon Jun 28, 2004 11:23 am

Jazz wrote:emerge info sux, it also shows things that arent even present in the make.conf, that creates a lot of confusion
Most of the things that emerge --info shows which aren't present in make.conf are defaults which are tucked away in other files on your box. emerge --info gives you an easy way to get all this information about your system in one simple command.
Shan wrote:And for the record, I answerd my own earlier post asking if it could be used as an alias. It can, and works just the same (atleast with the one ecatmur posted)
This works just as well, and the sed version seems to be quicker than the grep version. As I said before I really need to learn more sed.
Search | Read | [topic=119906]Answer[/topic] | [topic=28820]Report[/topic] | [topic=160179]Strip[/topic]
Top
UberLord
Retired Dev
Retired Dev
User avatar
Posts: 6838
Joined: Thu Sep 18, 2003 10:26 am
Location: Blighty
Contact:
Contact UberLord
Website

  • Quote

Post by UberLord » Mon Jun 28, 2004 11:49 am

Nice tip :)
Use dhcpcd for all your automated network configuration needs
Use dhcpcd-ui (GTK+/Qt) as your System Tray Network tool
Top
syn_ack
n00b
n00b
User avatar
Posts: 31
Joined: Mon Jan 26, 2004 6:55 am

  • Quote

Post by syn_ack » Mon Jul 12, 2004 7:25 am

ecatmur wrote:A shorter sed version:

Code: Select all

#!/bin/sed -f
s/#.*//
/^\s*$/d
or

Code: Select all

sed -e 's/#.*//;/^\s*$/d' "$@"
I've recently started learning/studying SED and GREP thanks to this thread. I'm unsure if this is correct or not but doesn't the following basically accomplish stripping out "commented lines" as well as "blank ones" in a shorter amount of key strokes? Please let me know what I'm missing here.

Code: Select all


cat .config | sed '/^#/ d' | sed '/^$/ d'

cat .config | sed -e '/^#/ d' -e '/^$/ d'

cat .config | grep  '.' | grep -v '#'

cat .config | grep -v '#' | grep  '[^$]'
And ecatmur, I would just like to say thanks for the awesome work on the "dep script" here. You have seriously motivated me to start learning the power of the BASH.
There have been so many things that I would like to fix or (actually), make better for myself and possibly the Gentoo community. But things seem to all of the sudden start becoming understandable for what ever reason. Your inspirational and make Linux truely that much more fun.

I feel like I'm starting to break some wild horse.. :D Not there yet. But I know if I stick with it and have good teachers like you guys/gals I'll get there.. Trying to stay focused in one Linux area over another seems to be my biggest problem. Kid in the candy store effect.
Top
hardcore
l33t
l33t
User avatar
Posts: 626
Joined: Sat Nov 01, 2003 1:58 pm
Location: MSU, MI

  • Quote

Post by hardcore » Wed Feb 16, 2005 3:23 am

This is just what i was looking for. Thanks :)
Nothing can stop me now, cuz I just don't care.
Top
randolph
n00b
n00b
User avatar
Posts: 10
Joined: Wed Mar 16, 2005 6:57 pm
Location: Berlin

  • Quote

Post by randolph » Wed Jun 29, 2005 7:20 am

syn_ack wrote: And ecatmur, I would just like to say thanks for the awesome work on the "dep script" here. You have seriously motivated me to start learning the power of the BASH....
...You're inspirational and make Linux truely much more fun....
I would like to step in line to this. As I am a noob "chmod 755" gave me just the tip I needed. Not knowing how to execute a script. And the rest speeks for it self.

Thank you very much ecatmur
Top
x1jmp
n00b
n00b
Posts: 28
Joined: Mon Jun 13, 2005 3:16 pm
Location: March-Hugstetten, Germany

  • Quote

Post by x1jmp » Mon Aug 22, 2005 1:22 pm

Thanks for this tip with sed!

My version is this, which also deletes lines with ";", like the smb.conf

Code: Select all

sed -e 's/[#;].*//;/^\s*$/d'
It's also possible to add exactly this into the .bashrc as an alias.
Top
msalerno
Veteran
Veteran
User avatar
Posts: 1338
Joined: Tue Dec 17, 2002 6:20 pm
Location: Sweating in South Florida

  • Quote

Post by msalerno » Fri Aug 26, 2005 8:55 pm

A perl update to the one above:

Code: Select all

perl -pne 's/^\s*([#;].*|\s*)\n$//' <filename>
Top
dundas
Guru
Guru
User avatar
Posts: 317
Joined: Thu Dec 16, 2004 6:28 am
Location: China, Earth

  • Quote

Post by dundas » Tue Jan 31, 2006 12:11 pm

cool, will do, thx tomk.
Appreciate Gentoo: Best Devs, Best Forums. YOU could help too: Help Answer
Top
nanafunk
n00b
n00b
Posts: 36
Joined: Wed Jun 29, 2005 2:03 am

  • Quote

Post by nanafunk » Wed Feb 01, 2006 8:55 am

some of you might find this url useful :)

Useless Use of Cat (cat foo | grep bar). See http://www.ruhr.de/home/smallo/award.html
Top
Post Reply

41 posts
  • 1
  • 2
  • Next

Return to “Documentation, Tips & Tricks”

Jump to
  • Assistance
  • ↳   News & Announcements
  • ↳   Frequently Asked Questions
  • ↳   Installing Gentoo
  • ↳   Multimedia
  • ↳   Desktop Environments
  • ↳   Networking & Security
  • ↳   Kernel & Hardware
  • ↳   Portage & Programming
  • ↳   Gamers & Players
  • ↳   Other Things Gentoo
  • ↳   Unsupported Software
  • Discussion & Documentation
  • ↳   Documentation, Tips & Tricks
  • ↳   Gentoo Chat
  • ↳   Gentoo Forums Feedback
  • ↳   Duplicate Threads
  • International Gentoo Users
  • ↳   中文 (Chinese)
  • ↳   Dutch
  • ↳   Finnish
  • ↳   French
  • ↳   Deutsches Forum (German)
  • ↳   Diskussionsforum
  • ↳   Deutsche Dokumentation
  • ↳   Greek
  • ↳   Forum italiano (Italian)
  • ↳   Forum di discussione italiano
  • ↳   Risorse italiane (documentazione e tools)
  • ↳   Polskie forum (Polish)
  • ↳   Instalacja i sprzęt
  • ↳   Polish OTW
  • ↳   Portuguese
  • ↳   Documentação, Ferramentas e Dicas
  • ↳   Russian
  • ↳   Scandinavian
  • ↳   Spanish
  • ↳   Other Languages
  • Architectures & Platforms
  • ↳   Gentoo on ARM
  • ↳   Gentoo on PPC
  • ↳   Gentoo on Sparc
  • ↳   Gentoo on Alternative Architectures
  • ↳   Gentoo on AMD64
  • ↳   Gentoo for Mac OS X (Portage for Mac OS X)
  • Board index
  • All times are UTC
  • Delete cookies

© 2001–2026 Gentoo Foundation, Inc.

Powered by phpBB® Forum Software © phpBB Limited

Privacy Policy