Artificial intelligent assistant

Delete all occurrences of a pattern apart from the first one I am trying to clean up the output from a code generator. Unfortunately it generates multiple imports: import Foo ... import Foo Luckily generated text is relatively static, though it gets regenerated often, thus what I'm hoping is that there is an easy way to remove this. I've worked out that if they were on the same line I could: `sed 's/import Foo//2g'` However I don't know sufficient sed to make it consider just all lines. A hacky solution would be to run multiple seds... sed 's/\n/<string I know doesn't appear>/g' sed 's/import Foo//2g' sed 's/<string I know doesn't appear>/\n' But that doesn't feel right to do. Is there a better way to do this?

sed '/^import Foo$/{x;/^$/!d;g;}'


How it works: On each line matching the pattern

* `x`: swap the line with the hold space
* `/^$/!d`: if what's just been fetched from hold space isn't empty, ie. because a previous match was stored there, delete the pattern and advance to the next line
* `g`: otherwise (ie. the first time through) copy the saved line back. It will be printed by default

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy 74bceaff576f68ffe02470e315b9aea3