sed: delete all occurrences of a string except the first one
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/'
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.
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//'
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\)/\1\n/;h;s/.*\n//3;s/pattern//g;H;g;s/\n.*\n//'