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

Thomas Edward Groves teg451013 at freeuk.com
Fri Jan 20 06:47:35 UTC 2012


This might help

{x[$0]++;}END{for (line in x) printf("%4d %s\n", x[line], line); }

and the crude way to use it, as a file called awk.txt, is:

awk -f awk.txt <errorlog >messagefile

Any use?

Tom
  ----- Original Message ----- 
  From: David Halliday 
  To: Kent Linux User Group - General Topics 
  Sent: Thursday, January 19, 2012 10:47 AM
  Subject: [Klug-general] sed - How to delete all but first occurrence?


  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).



------------------------------------------------------------------------------


  _______________________________________________
  Kent mailing list
  Kent at mailman.lug.org.uk
  https://mailman.lug.org.uk/mailman/listinfo/kent
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.lug.org.uk/pipermail/kent/attachments/20120120/4e9b8ff3/attachment.htm>


More information about the Kent mailing list