On Nov 30, 2009, at 22:16 , Tom Limoncelli wrote:
Ok, do it in awk :-)


This could be one-liner-ed if your awk is liberal about where a pattern-action pair can occur (some require it to be at the beginning of a line).

function go() {
  print "# BEGIN project"
  print "hi"
  print "# END project"
}
/^# BEGIN / {if (!a) {go()}; a = 1}
a != 1      {print}
/^# END /   {if (a) {a++}}
END         {if (!a) {go()}}

(If END composed with other patterns, the function would be unnecessary.)

If you relax the original constraint of replacing an existing BEGIN .. END block in place, the sed is simple (well, as simple as any sed involving multiline text gets):

sed -e '/^# BEGIN /,/^# END /d' -e '$a\
# BEGIN project\
foo\
# END project'

(*csh users beware: it will eat the trailing backslashes; you'll need to use 3 backslashes to pass one through)

If you want to do it in place, you need to (a) copy the block add/ replace code and (b) carry a conditional around somehow. The latter can be done with the hold space, if you're not using it for anything else or if you can use it in such a way that its contents will work as the conditional; the latter is easiest if the condition can be represented as "hold space not empty".

--
brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allb...@kf8nh.com
system administrator [openafs,heimdal,too many hats] allb...@ece.cmu.edu
electrical and computer engineering, carnegie mellon university    KF8NH


Attachment: PGP.sig
Description: This is a digitally signed message part

_______________________________________________
Discuss mailing list
Discuss@lopsa.org
http://lopsa.org/cgi-bin/mailman/listinfo/discuss
This list provided by the League of Professional System Administrators
 http://lopsa.org/

Reply via email to