Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a logfile with timestamps in it. Occasionally there are multiple timestamps in one line. Now I would like to remove all of the timestamps from a line but keep the first one.

I can do s/pattern//2 but that only removes the second occurrence and sed doesn't allow something like s/pattern//2-.

Any suggestions?

share|improve this question
I should've told that it's the sed of busybox. Sorry for that. – Folkert van Heusden Aug 8 '11 at 12:08

This should work (replace _ by something else should it clash with your logs):

sed -e 's/pattern/_&/1' -e 's/\([^_]\)pattern//g' -e 's/_\(pattern\)/\1/'
share|improve this answer
if you're ever wanting a unique delimiter, use \n. – mikeserv Dec 19 '14 at 13:35
sed -e ':begin;s/pattern//2;t begin'

or without the sed goto:

sed -e 's/\(pattern\)/\1\n/;h;s/.*\n//;s/pattern//g;H;g;s/\n.*\n//'
share|improve this answer

A slight variation on @jillagre's answer (modified for robustness) could look like:

sed 's/p\(attern\)/p\n\1/;s///g;s/\n//'

...but in some seds you may need to replace the n in the right-hand side of the first s///ubstitution statement with a literal \newline character.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.