Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 7 Oct 2009 17:56:36 -0700
From:      George Davidovich <freebsd@optimis.net>
To:        freebsd-questions@freebsd.org
Subject:   Re: A general sed question
Message-ID:  <20091008005636.GA38899@marvin.optimis.net>
In-Reply-To: <2daa8b4e0910062345r83fa23aj113b062af114887f@mail.gmail.com>
References:  <2daa8b4e0910062345r83fa23aj113b062af114887f@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Tue, Oct 06, 2009 at 11:45:36PM -0700, David Allen 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

For the following note that what's contained in the square brackets is a
space character followed by a literal TAB character (typically created
by entering ^V followed by TAB).

>  - delete commented lines
>  - remove inline comments

s/[     ]*#.*//   # takes care of both, but will leave \t\t\t\n

>  - remove trailing spaces and/or tabs

s/[ 	]*$//     # handy, but not needed if using diff -b

>  - delete blank lines, and/or lines containing just spaces and/or tabs

/^[ 	]*$/d

>  - expand tabs

This is overly complex with sed and probably unecessary.  Instead I'd
suggest using your editor (in vim, it's ':set expandtab | retab'), or
for interactive use, relying on expand(1) and using a value for -t that
matches the tab spacing you typically use for your pager and/or editor.
Alternatively, to get better visual alignment when using diff(1), just
use the -t option.

Putting the above together, you get

sed -e 's/[ 	]*#.*//' -e 's/[ 	]*$//' -e '/^[ 	]*$/d'  

Hardly ideal but it's readable enough and satisfies the 80/20 rule.  If
used as a simple alias, shell function or script as Oliver Fromme
suggested (yes, this works in bash), my suggestion is

diff -ubBt <(cleanup /sys/i386/conf/GENERIC) <(cleanup /path/to/NEWKERNEL)

-- 
George



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20091008005636.GA38899>