Help with sed script?

Paul Lussier p.lussier at comcast.net
Fri Sep 8 09:31:00 EDT 2006


"Michael ODonnell" <michael.odonnell at comcast.net> writes:

> One of the config files I'll be working with is /etc/fstab
> and I'll be keying on the mountpoint field i.e.  the second
> whitespace-delimited token on any line that hasn't been
> commented out.  So far, that RE does appear to be having the
> desired results, though I'm sure many refinements are possible.

I don't exactly what you're trying to do, but if you just want to pick
out certain fields from a file like /etc/fstab, I've found awk
extremely helpful for quick and dirty solutions.  For example, I
routinely do this type of thing in single user mode on a horked system
needing fsucking:

  for i in `awk awk '/^\/dev/ && !/cdrom|floppy/ {print $1}' /etc/fstab`
  do
    fsck -y $i
  done

You could easily adapt this your needs.  It might also be easier[1] to
break the regexp matching down into slightly smaller chunks rather
than one really long sed command.  For example, you've stated that you
don't want to deal with lines that begin with a hash, so grep them out
first, then operate only on what you have left:

       egrep -v '^( *\#|\#) foo.txt | (sed|awk|perl) ....

Granted, that's multiple processes, which for some reason might be
undesirable[2], but if what you're doing gets complicated enough,
it'll likely end up in a script.  If the multiple processes is a
problem, evolve it into perl script which will be a single process and
can handle everything under that one umbrella.

[1]  Easier:
       - to develop "the right thing"
       - to understand (both for you and others)
       - to maintain and expand later

[2] It's really tough to advise in any direction without knowing what
    the desired result is :)

HTH.
-- 
Seeya,
Paul



More information about the gnhlug-discuss mailing list