[RndTbl] Little script puzzle help.

Mike Pfaiffer high.res.mike at gmail.com
Mon Jul 30 19:26:18 CDT 2012

On 12-07-30 06:02 PM, Sean Cody wrote:
> I have a file in which I need to replace a chunk of lines in on a
> bunch of hosts.
> My initial reaction was to use patch but that won't suffice because
> the chunk of lines will be different on each host (possibly) so the
> source part of the patch may not aline to the diff.
> Then I was doing to do sed but I can't get the 'r' command do work as
> I need it to.
> So I wrote down a series of vi commands to do what I need to do now I
> just need to convert that into a sed/ex script...
> sudo vi $someconfigfile 23,56d 23 r ~/replacement.txt ZZ
> So what I'm doing here is just dumping lines 23 through 56 in some
> file and inserting a replacement chunk of text (my not necessarily be
> 56-23 lines long).
> The question here is... is this possible to do with sed and if so how
> (my brain wont' make the right connections right now, must need a
> break/food or something).
> Even better how would this be done with ex (as I can do a bunch of
> manual vi commands, how would one automate a repeatable vi
> session...)?
> I could just run a series of search&replace lines corresponding to
> each section in the replacement text doc but that's violating the
> spirit of the problem (ie. replace a chunk of text at an arbitrary
> point in a file with a different chunk of text located in a different
> file).
> Some rules... solution shouldn't be perl/python/whatever because that
> won't necessarily be found on the UNIX hosts I'm using or may use in
> the future.  Solution shouldn't use GNU extensions of sed/awk due to
> those being incompatible with non-GNU  systems (ie. sysv,bsd,hp
> etc.).
> Any hits, takers?

	I'm going to think a little outside the box. The lines you want to 
insert into the existing file... Is there a pattern? Can they be 
generated ahead of time and stored in temporary files? I gather the 
lines are consecutive... If all this is the case then all you have to do 
is determine the start and end of the section you want to replace. I 
think you could then use head, cat, and tail to recreate the file you 
want then just mv it.


More information about the Roundtable mailing list