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?

link|improve this question

20% accept rate
I should've told that it's the sed of busybox. Sorry for that. – Folkert van Heusden Aug 8 '11 at 12:08
feedback

2 Answers

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/'
link|improve this answer
That works! Great! Very ellegant solution. I wonder why I didn't think of it myself :-) – Folkert van Heusden Aug 8 '11 at 12:14
feedback
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//'
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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