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?
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.
Later Mike
If there is a pattern, then grep will tell you the line numbers, combined with head and tail to extract the part of the file you want.
In any case head and tail sounds like what I would use in this situation but I'm not sure I fully understand it.
On 2012-07-30, at 7:26 PM, Mike Pfaiffer wrote:
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.
You are thinking along the right lines though I answered a few of those points already. The lines I'm replaceing with is already in a file (replacement.txt) and the chunk of stuff I'm replacing will be between lines 23 and 56 of the file. The contents of lines 23 through 56 may have different values on each host but they will be represented.
Building up with head and tail is not a bad approach but requires the use of a temporary file and if this data set was huge would be a bit cumbersome (if you use /tmp via mktemp which is could be different file system etc.).
This is not a good problem for cat though... head and tail is serviceable. Thanks for your thoughts on this.
Probably not the answer you want to hear now, but dude, you really need to look into puppet or cfengine. Since I know you're stuck with a bunch of different OSes, the latter might be better.
http://stackoverflow.com/questions/6744639/sed-awk-remove-lines-from-pattern... also help you with the sed /r, they have an example that does what you want using patterns instead of line numbers.
Sean
On Mon, Jul 30, 2012 at 6:02 PM, Sean Cody sean@tinfoilhat.ca 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?
-- Sean P.S. I'm doing this as a bit of a personal change management to replay some changes in the distant future (I'm typically using diff/patch for this though this case is a bit a-typical).
Roundtable mailing list Roundtable@muug.mb.ca http://www.muug.mb.ca/mailman/listinfo/roundtable
On 2012-07-30, at 7:34 PM, Sean Walberg wrote:
Probably not the answer you want to hear now, but dude, you really need to look into puppet or cfengine. Since I know you're stuck with a bunch of different OSes, the latter might be better.
http://stackoverflow.com/questions/6744639/sed-awk-remove-lines-from-pattern... might also help you with the sed /r, they have an example that does what you want using patterns instead of line numbers.
I do need to look into either of those. :)
In the situation/place I need to use this little gem I have zero control over the hosts but am temporarily granted escalated privileges from time to time. When I'm done with the hosts I will probably be around to support them for a while but in that case my account is supposed to be reverted to a restricted power user.