|
I have a package that needs to add a text fragment, stored in fileA, to another file fileB. Due to the current requirements and the control I have over the file, I can make the assumption that if the first line of fileA is not present, the others won't be there, either. So this becomes a trivial two-liner:
Now of course the tricky part is to remove those lines at the time the package is uninstalled. One approach would be:
Now this becomes tricky when other packages (or users) can manipulate fileB in the mean time, so there may be content following the last line of fileA that I do not want to remove.
I then attempted to rewrite the patch on the fly into the reverse of the same patch, ie an "unpatch". And that kind of worked:
But that seems rather silly. Another, more obvious approach would be to:
And guess what, our good friend sed(1) will happily do the right thing for us: sed -e "/$(head -1 fileA)/,/$(tail -1 fileA)/d" fileB You need double quotes to have the subshell expansion work. Add '/./,/^$/!d' to suppress multiple blank lines. And yes, this may break depending on the contents of fileA, for example if the first or last line contains a / (and which is why I actually did end up using the "unpatch" approach noted above). But it seemd entertaining enough at the time... If you have a better or simpler way to do this (one-liner, please)... March 2, 2012 |