sed: delete all occurrences of a string except the first one

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

With GNU sed:

sed 's/pattern//2g'

The 2 specifies that the second pattern and all the restg should remove. So this will keep the first one.

Method 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/'

Method 3

sed -e ':begin;s/pattern//2;t begin'

or without the sed goto:

sed -e 's/(pattern)/1n/;h;s/.*n//;s/pattern//g;H;g;s/n.*n//'

The generic solutions to remove from the nth (3 for example) position are:

sed -e ':begin;s/pattern//4;t begin'
sed -e 's/(pattern)/1n/;h;s/.*n//3;s/pattern//g;H;g;s/n.*n//'

Method 4

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

sed 's/p(attern)/pn1/;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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x