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

Clash Royale CLAN TAG#URR8PPP
up vote
13
down vote
favorite
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?
sed
add a comment |Â
up vote
13
down vote
favorite
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?
sed
I should've told that it's the sed of busybox. Sorry for that.
â Folkert van Heusden
Aug 8 '11 at 12:08
add a comment |Â
up vote
13
down vote
favorite
up vote
13
down vote
favorite
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?
sed
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?
sed
sed
edited Aug 8 '11 at 11:05
Caleb
49.2k9146185
49.2k9146185
asked Aug 8 '11 at 6:58
Folkert van Heusden
597515
597515
I should've told that it's the sed of busybox. Sorry for that.
â Folkert van Heusden
Aug 8 '11 at 12:08
add a comment |Â
I should've told that it's the sed of busybox. Sorry for that.
â Folkert van Heusden
Aug 8 '11 at 12:08
I should've told that it's the sed of busybox. Sorry for that.
â Folkert van Heusden
Aug 8 '11 at 12:08
I should've told that it's the sed of busybox. Sorry for that.
â Folkert van Heusden
Aug 8 '11 at 12:08
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
4
down vote
accepted
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.
2
That will only work with GNUsed.
â Stéphane Chazelas
Dec 18 '14 at 8:38
1
Which Cygwin has (a port of, apparently) and MacOS does not. Achh! This solution is so much more elegant.
â r_alex_hall
Jun 2 '17 at 4:03
add a comment |Â
up vote
7
down vote
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/'
1
if you're ever wanting a unique delimiter, usen.
â mikeserv
Dec 19 '14 at 13:35
add a comment |Â
up vote
5
down vote
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//'
add a comment |Â
up vote
1
down vote
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.
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
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.
2
That will only work with GNUsed.
â Stéphane Chazelas
Dec 18 '14 at 8:38
1
Which Cygwin has (a port of, apparently) and MacOS does not. Achh! This solution is so much more elegant.
â r_alex_hall
Jun 2 '17 at 4:03
add a comment |Â
up vote
4
down vote
accepted
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.
2
That will only work with GNUsed.
â Stéphane Chazelas
Dec 18 '14 at 8:38
1
Which Cygwin has (a port of, apparently) and MacOS does not. Achh! This solution is so much more elegant.
â r_alex_hall
Jun 2 '17 at 4:03
add a comment |Â
up vote
4
down vote
accepted
up vote
4
down vote
accepted
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.
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.
edited Oct 5 '16 at 16:14
terdonâ¦
123k28232406
123k28232406
answered Dec 18 '14 at 8:24
ñÃÂsýù÷
16k92563
16k92563
2
That will only work with GNUsed.
â Stéphane Chazelas
Dec 18 '14 at 8:38
1
Which Cygwin has (a port of, apparently) and MacOS does not. Achh! This solution is so much more elegant.
â r_alex_hall
Jun 2 '17 at 4:03
add a comment |Â
2
That will only work with GNUsed.
â Stéphane Chazelas
Dec 18 '14 at 8:38
1
Which Cygwin has (a port of, apparently) and MacOS does not. Achh! This solution is so much more elegant.
â r_alex_hall
Jun 2 '17 at 4:03
2
2
That will only work with GNU
sed.â Stéphane Chazelas
Dec 18 '14 at 8:38
That will only work with GNU
sed.â Stéphane Chazelas
Dec 18 '14 at 8:38
1
1
Which Cygwin has (a port of, apparently) and MacOS does not. Achh! This solution is so much more elegant.
â r_alex_hall
Jun 2 '17 at 4:03
Which Cygwin has (a port of, apparently) and MacOS does not. Achh! This solution is so much more elegant.
â r_alex_hall
Jun 2 '17 at 4:03
add a comment |Â
up vote
7
down vote
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/'
1
if you're ever wanting a unique delimiter, usen.
â mikeserv
Dec 19 '14 at 13:35
add a comment |Â
up vote
7
down vote
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/'
1
if you're ever wanting a unique delimiter, usen.
â mikeserv
Dec 19 '14 at 13:35
add a comment |Â
up vote
7
down vote
up vote
7
down vote
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/'
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/'
answered Aug 8 '11 at 11:41
jlliagre
45.2k578125
45.2k578125
1
if you're ever wanting a unique delimiter, usen.
â mikeserv
Dec 19 '14 at 13:35
add a comment |Â
1
if you're ever wanting a unique delimiter, usen.
â mikeserv
Dec 19 '14 at 13:35
1
1
if you're ever wanting a unique delimiter, use
n.â mikeserv
Dec 19 '14 at 13:35
if you're ever wanting a unique delimiter, use
n.â mikeserv
Dec 19 '14 at 13:35
add a comment |Â
up vote
5
down vote
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//'
add a comment |Â
up vote
5
down vote
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//'
add a comment |Â
up vote
5
down vote
up vote
5
down vote
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//'
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//'
edited Aug 30 at 23:06
Isaac
7,18111035
7,18111035
answered Aug 8 '11 at 11:50
jfg956
3,10711223
3,10711223
add a comment |Â
add a comment |Â
up vote
1
down vote
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.
add a comment |Â
up vote
1
down vote
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.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
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.
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.
answered Dec 19 '14 at 13:32
mikeserv
44.4k564150
44.4k564150
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f18303%2fsed-delete-all-occurrences-of-a-string-except-the-first-one%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
I should've told that it's the sed of busybox. Sorry for that.
â Folkert van Heusden
Aug 8 '11 at 12:08