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

James Morris jwm.art.net at gmail.com
Thu Jan 19 10:57:15 UTC 2012


On 19 January 2012 10:47, David Halliday <david.halliday at gmail.com> wrote:
> 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).

Does this help?
http://stackoverflow.com/questions/3045023/sed-or-awk-or-perl-remove-all-not-uniq-lines-except-the-first-line-file-name



More information about the Kent mailing list