A general sed question

George Davidovich freebsd at optimis.net
Thu Oct 8 00:56:38 UTC 2009


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


More information about the freebsd-questions mailing list