A general sed question

Oliver Fromme olli at lurza.secnetix.de
Wed Oct 7 10:26:52 UTC 2009

David Allen <the.real.david.allen at gmail.com> wrote:
 > I keep bumping up against this, so I thought I'd throw this question out
 > to those who understand sed better than I do.
 > What I'm trying to do is to clean up the contents of some files
 > (/sys/i386/conf/GENERIC would be a good example) to get more readable
 > diffs.  To that end, I'm trying to use sed to
 >  - delete commented lines
 >  - remove inline comments
 >  - remove trailing spaces and/or tabs
 >  - delete blank lines, and/or lines containing just spaces and/or tabs
 >  - expand tabs

I recommend to use the -Bb options of diff.  They cause diff
to ignore blank lines and any changes in the amount of white
space (including tabs).  You can also use -w to ignore *all*
white space, but note that "foo bar" and "foobar" are then
considered equal, which might not be what you want.

So only the removal of comments remains:

sed 's/#.*//'

That will remove all comments.  Afterwards, commented lines
are empty, so the -B option of diff will ignore them, so you
don't have to remove them explicitly.

When using zsh as your shell, you can use a nice feature
called "process substitution", so you don't have to create
temporary files:

diff -Buw <(sed 's/#.*//' GENERIC) <(sed 's/#.*//' MYKERNEL)

I think bash has a similar feature, but I don't know the
syntax, so please see the manpage if you're a bash user.

If you need to do that oftem, it's worth to create an alias
or shell function.

Best regards

Oliver Fromme, secnetix GmbH & Co. KG, Marktplatz 29, 85567 Grafing b. M.
Handelsregister: Registergericht Muenchen, HRA 74606,  Geschäftsfuehrung:
secnetix Verwaltungsgesellsch. mbH, Handelsregister: Registergericht Mün-
chen, HRB 125758,  Geschäftsführer: Maik Bachmann, Olaf Erb, Ralf Gebhart

FreeBSD-Dienstleistungen, -Produkte und mehr:  http://www.secnetix.de/bsd

"People still program in C.  People keep writing shell scripts.  *Most*
people don't realize the shortcomings of the tools they are using because
they a) don't reflect on their workflows and they are b) too lazy to check
out alternatives to realize there is help." -- Simon 'corecode' Schubert

More information about the freebsd-questions mailing list