[Klug-general] sed - How to delete all but first occurrence?

David Halliday david.halliday at gmail.com
Thu Jan 19 10:48:04 UTC 2012


I have been playing with sed for a while. Some interesting stuff,
Here: http://www.tutorialspoint.com/unix/unix-regular-expressions.htm
And here: http://sed.sourceforge.net/sed1line.txt

However I am trying to remove from a log file (which is to be sent in an
email) all repeat occurrences of lines starting with "error:". So the log
file looks like this:
====================================================================
some information: blable
This was happening: blah blah

error: something went wrong
error: I'm not happy
error: something went wrong
error: something went wrong
error: something went wrong
error: something went wrong
error: I'm not happy
error: I'm not happy

====================================================================

Now I can use sed happily as:
sed -e '/error:/d'

This removes *all* error messages (In some cases the script throws so many
errors that to include them makes for a nasty email) What I can't find is
how to remove all but the first. I'm only interested in the first error as
in most cases all others will span from that. Does anyone know how to do
this? Or am I now using the wrong tool for the job?

More Ideal would be to include only one instance of the unique error
messages but I believe that will be a little more complicated and more time
than it is worth (not to mention that a more complicated solution is harder
to explain to people I'm working with).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.lug.org.uk/pipermail/kent/attachments/20120119/3aa77173/attachment.htm>


More information about the Kent mailing list