Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
add comment

2 Answers 2

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
    
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
add comment
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
add comment

Your Answer

 
discard

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.