On Mon, Nov 30, 2009 at 09:20:12PM -0500, Tom Limoncelli wrote: > I have a file called rc-addition that contains: > > > # BEGIN project > a > bunch > of > lines > # END project > > > I want to append it to the end of /etc/rc.machine but if there is already a > "# BEGIN project" / "# END project" it should replace that text, or delete > it and append the new text to the end. > > This sounds like a job of sed but my sed fu is weak. > > Who can do this in 1 line?
Tom, I would submit that the correct question is not "who?", but "why?". Sorry to bring seriosity into the fun, but: one-liners as in Ken Iverson's write-only code / throwaway code are fun for obfuscated code contests, but for the same reason [obfuscation] should never be used in real life. You can't read 'em [sorry, Phil, truly elegant but ...] and if you can't read 'em you can't figure out WHETHER there are any bugs, WHERE the bugs are ["all software is beta"], or HOW to fix the bugs that must exist. And inserting test conditions for errors in assumptions, error messages, and comments is ... challenging. And you need these for the day you're in Seattle and leave your niece to water the plants, walk the dog, and run the scripts [and hope she doesn't water the scripts, run the dog, and walk the plants]. Me, I'll go with the following, ASSUMing that you can assure me that only one such block exists - otherwise I'd have to write a readline / writeline Turing machine to do the obvious: ======================================================================= #!/bin/bash addfile="rc-addition" begin_label="# BEGIN " # Usage. Token one-liner. if [ "1" != "$#" ]; then echo "usage: $0 filename"; exit 1; fi # The file name "/etc/rc.machine" [e.g.] is passed as an argument. file="$1" # But does it exist? if [ ! -f "$file" ]; then echo "${0}: no such file as \"$file\"." exit 2 fi # Get the block to be added if [ ! -f "$addfile" ]; then echo "${0}: no \"$addfile\" file to be added." exit 3 fi # Is there a block begin label? # No test for multiple such labels, just pick the first ... pattern=`grep "^$begin_label" "$addfile" | head -1` if [ "" = "$pattern" ]; then echo "${0}: No \"$begin_label\" label in \"$addfile\"." exit 4 fi # Is there already a block with this BEGIN label? block=`grep "^$pattern"'$' "$file" # If so, delete it if [ "" != "$block" ]; then epat=`echo "$pattern" | sed 's/BEGIN/END/'` # OK, I'm ASSUMING here that the END label exists; if not, this # edit will fail ... which in that case is a correct action ex - "$file" << EndOfEdit /^$pattern$/;/^$epat$/d wq! EndOfEdit fi # concatenate the new block on cat "$addfile" >> "$file" ======================================================================= -- /*********************************************************************\ ** ** Joe Yao j...@tux.org - Joseph S. D. Yao ** \*********************************************************************/ _______________________________________________ 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/