Do not replace the second occurrence of pattern in a same word in the file with sed command
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
echo "abcdef12cade 12345678 abcdefba12345678" | sed -E "s/[0-9a-fA-F]8/00000000/g"
the above outputs the following
00000000cade 00000000 0000000000000000
it is replacing the pattern for second occurrence in a same word.
I don't want to replace if there is a second occurrence.
expected ouput
00000000cade 00000000 abcdefba12345678
linux text-processing sed
add a comment |Â
up vote
0
down vote
favorite
echo "abcdef12cade 12345678 abcdefba12345678" | sed -E "s/[0-9a-fA-F]8/00000000/g"
the above outputs the following
00000000cade 00000000 0000000000000000
it is replacing the pattern for second occurrence in a same word.
I don't want to replace if there is a second occurrence.
expected ouput
00000000cade 00000000 abcdefba12345678
linux text-processing sed
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
echo "abcdef12cade 12345678 abcdefba12345678" | sed -E "s/[0-9a-fA-F]8/00000000/g"
the above outputs the following
00000000cade 00000000 0000000000000000
it is replacing the pattern for second occurrence in a same word.
I don't want to replace if there is a second occurrence.
expected ouput
00000000cade 00000000 abcdefba12345678
linux text-processing sed
echo "abcdef12cade 12345678 abcdefba12345678" | sed -E "s/[0-9a-fA-F]8/00000000/g"
the above outputs the following
00000000cade 00000000 0000000000000000
it is replacing the pattern for second occurrence in a same word.
I don't want to replace if there is a second occurrence.
expected ouput
00000000cade 00000000 abcdefba12345678
linux text-processing sed
edited Jun 21 at 15:47
Jeff Schaller
30.8k846104
30.8k846104
asked Jun 21 at 15:32
hidayath
1
1
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
3
down vote
If you only want to replace the first occurrence of a match, don't use the g
suffix to the command:
$ echo 'aa' | sed 's/a/b/g'
bb
$ echo 'aa' | sed 's/a/b/'
ba
The g
option stands for 'global', which is explicitly telling sed
to replace all matches and not just the first (which is the default behavior).
add a comment |Â
up vote
1
down vote
It seems like you're looking for words that are between 8 and 15 chars, and replace the first 8 hex digits:
sed -E 's/<[[:xdigit:]]8([[:xdigit:]]0,7)>/000000001/g' <<END
abcdef12cade 12345678 abcdefba12345678 12345 123456789
END
00000000cade 00000000 abcdefba12345678 12345 000000009
Where, <
and >
are word boundaries, and [:xdigit:]
matches a hex digit.
add a comment |Â
up vote
0
down vote
The g
at the end make sed
repeat the substitution as many times as possible on the line. You only want to do it twice.
Let's do that with GNU awk
:
echo 'abcdef12cade 12345678 abcdefba12345678' |
awk ' sub("[0-9a-fA-F]8", "00000000", $1)
sub("[0-9a-fA-F]8", "00000000", $2)
print '
This performs the substitution on the two first whitespace-delimited fields only, and then prints the resulting line.
OP didn't want the second match replaced though?
â DopeGhoti
Jun 21 at 16:10
@DopeGhoti Their expected output is00000000cade 00000000 abcdefba12345678
, which is what this produces.
â Kusalananda
Jun 21 at 16:44
@DopeGhoti ... and I didn't read the title of the question until now, which leaves me more than slightly confused.
â Kusalananda
Jun 21 at 17:03
Yeah, the actual prose, title, and expected output presented are all somewhat contadictory.
â DopeGhoti
Jun 21 at 17:30
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
If you only want to replace the first occurrence of a match, don't use the g
suffix to the command:
$ echo 'aa' | sed 's/a/b/g'
bb
$ echo 'aa' | sed 's/a/b/'
ba
The g
option stands for 'global', which is explicitly telling sed
to replace all matches and not just the first (which is the default behavior).
add a comment |Â
up vote
3
down vote
If you only want to replace the first occurrence of a match, don't use the g
suffix to the command:
$ echo 'aa' | sed 's/a/b/g'
bb
$ echo 'aa' | sed 's/a/b/'
ba
The g
option stands for 'global', which is explicitly telling sed
to replace all matches and not just the first (which is the default behavior).
add a comment |Â
up vote
3
down vote
up vote
3
down vote
If you only want to replace the first occurrence of a match, don't use the g
suffix to the command:
$ echo 'aa' | sed 's/a/b/g'
bb
$ echo 'aa' | sed 's/a/b/'
ba
The g
option stands for 'global', which is explicitly telling sed
to replace all matches and not just the first (which is the default behavior).
If you only want to replace the first occurrence of a match, don't use the g
suffix to the command:
$ echo 'aa' | sed 's/a/b/g'
bb
$ echo 'aa' | sed 's/a/b/'
ba
The g
option stands for 'global', which is explicitly telling sed
to replace all matches and not just the first (which is the default behavior).
edited Jun 21 at 15:42
answered Jun 21 at 15:38
DopeGhoti
39.7k54779
39.7k54779
add a comment |Â
add a comment |Â
up vote
1
down vote
It seems like you're looking for words that are between 8 and 15 chars, and replace the first 8 hex digits:
sed -E 's/<[[:xdigit:]]8([[:xdigit:]]0,7)>/000000001/g' <<END
abcdef12cade 12345678 abcdefba12345678 12345 123456789
END
00000000cade 00000000 abcdefba12345678 12345 000000009
Where, <
and >
are word boundaries, and [:xdigit:]
matches a hex digit.
add a comment |Â
up vote
1
down vote
It seems like you're looking for words that are between 8 and 15 chars, and replace the first 8 hex digits:
sed -E 's/<[[:xdigit:]]8([[:xdigit:]]0,7)>/000000001/g' <<END
abcdef12cade 12345678 abcdefba12345678 12345 123456789
END
00000000cade 00000000 abcdefba12345678 12345 000000009
Where, <
and >
are word boundaries, and [:xdigit:]
matches a hex digit.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
It seems like you're looking for words that are between 8 and 15 chars, and replace the first 8 hex digits:
sed -E 's/<[[:xdigit:]]8([[:xdigit:]]0,7)>/000000001/g' <<END
abcdef12cade 12345678 abcdefba12345678 12345 123456789
END
00000000cade 00000000 abcdefba12345678 12345 000000009
Where, <
and >
are word boundaries, and [:xdigit:]
matches a hex digit.
It seems like you're looking for words that are between 8 and 15 chars, and replace the first 8 hex digits:
sed -E 's/<[[:xdigit:]]8([[:xdigit:]]0,7)>/000000001/g' <<END
abcdef12cade 12345678 abcdefba12345678 12345 123456789
END
00000000cade 00000000 abcdefba12345678 12345 000000009
Where, <
and >
are word boundaries, and [:xdigit:]
matches a hex digit.
edited Jun 21 at 17:00
answered Jun 21 at 16:03
glenn jackman
45.6k265100
45.6k265100
add a comment |Â
add a comment |Â
up vote
0
down vote
The g
at the end make sed
repeat the substitution as many times as possible on the line. You only want to do it twice.
Let's do that with GNU awk
:
echo 'abcdef12cade 12345678 abcdefba12345678' |
awk ' sub("[0-9a-fA-F]8", "00000000", $1)
sub("[0-9a-fA-F]8", "00000000", $2)
print '
This performs the substitution on the two first whitespace-delimited fields only, and then prints the resulting line.
OP didn't want the second match replaced though?
â DopeGhoti
Jun 21 at 16:10
@DopeGhoti Their expected output is00000000cade 00000000 abcdefba12345678
, which is what this produces.
â Kusalananda
Jun 21 at 16:44
@DopeGhoti ... and I didn't read the title of the question until now, which leaves me more than slightly confused.
â Kusalananda
Jun 21 at 17:03
Yeah, the actual prose, title, and expected output presented are all somewhat contadictory.
â DopeGhoti
Jun 21 at 17:30
add a comment |Â
up vote
0
down vote
The g
at the end make sed
repeat the substitution as many times as possible on the line. You only want to do it twice.
Let's do that with GNU awk
:
echo 'abcdef12cade 12345678 abcdefba12345678' |
awk ' sub("[0-9a-fA-F]8", "00000000", $1)
sub("[0-9a-fA-F]8", "00000000", $2)
print '
This performs the substitution on the two first whitespace-delimited fields only, and then prints the resulting line.
OP didn't want the second match replaced though?
â DopeGhoti
Jun 21 at 16:10
@DopeGhoti Their expected output is00000000cade 00000000 abcdefba12345678
, which is what this produces.
â Kusalananda
Jun 21 at 16:44
@DopeGhoti ... and I didn't read the title of the question until now, which leaves me more than slightly confused.
â Kusalananda
Jun 21 at 17:03
Yeah, the actual prose, title, and expected output presented are all somewhat contadictory.
â DopeGhoti
Jun 21 at 17:30
add a comment |Â
up vote
0
down vote
up vote
0
down vote
The g
at the end make sed
repeat the substitution as many times as possible on the line. You only want to do it twice.
Let's do that with GNU awk
:
echo 'abcdef12cade 12345678 abcdefba12345678' |
awk ' sub("[0-9a-fA-F]8", "00000000", $1)
sub("[0-9a-fA-F]8", "00000000", $2)
print '
This performs the substitution on the two first whitespace-delimited fields only, and then prints the resulting line.
The g
at the end make sed
repeat the substitution as many times as possible on the line. You only want to do it twice.
Let's do that with GNU awk
:
echo 'abcdef12cade 12345678 abcdefba12345678' |
awk ' sub("[0-9a-fA-F]8", "00000000", $1)
sub("[0-9a-fA-F]8", "00000000", $2)
print '
This performs the substitution on the two first whitespace-delimited fields only, and then prints the resulting line.
answered Jun 21 at 15:52
Kusalananda
101k13199312
101k13199312
OP didn't want the second match replaced though?
â DopeGhoti
Jun 21 at 16:10
@DopeGhoti Their expected output is00000000cade 00000000 abcdefba12345678
, which is what this produces.
â Kusalananda
Jun 21 at 16:44
@DopeGhoti ... and I didn't read the title of the question until now, which leaves me more than slightly confused.
â Kusalananda
Jun 21 at 17:03
Yeah, the actual prose, title, and expected output presented are all somewhat contadictory.
â DopeGhoti
Jun 21 at 17:30
add a comment |Â
OP didn't want the second match replaced though?
â DopeGhoti
Jun 21 at 16:10
@DopeGhoti Their expected output is00000000cade 00000000 abcdefba12345678
, which is what this produces.
â Kusalananda
Jun 21 at 16:44
@DopeGhoti ... and I didn't read the title of the question until now, which leaves me more than slightly confused.
â Kusalananda
Jun 21 at 17:03
Yeah, the actual prose, title, and expected output presented are all somewhat contadictory.
â DopeGhoti
Jun 21 at 17:30
OP didn't want the second match replaced though?
â DopeGhoti
Jun 21 at 16:10
OP didn't want the second match replaced though?
â DopeGhoti
Jun 21 at 16:10
@DopeGhoti Their expected output is
00000000cade 00000000 abcdefba12345678
, which is what this produces.â Kusalananda
Jun 21 at 16:44
@DopeGhoti Their expected output is
00000000cade 00000000 abcdefba12345678
, which is what this produces.â Kusalananda
Jun 21 at 16:44
@DopeGhoti ... and I didn't read the title of the question until now, which leaves me more than slightly confused.
â Kusalananda
Jun 21 at 17:03
@DopeGhoti ... and I didn't read the title of the question until now, which leaves me more than slightly confused.
â Kusalananda
Jun 21 at 17:03
Yeah, the actual prose, title, and expected output presented are all somewhat contadictory.
â DopeGhoti
Jun 21 at 17:30
Yeah, the actual prose, title, and expected output presented are all somewhat contadictory.
â DopeGhoti
Jun 21 at 17:30
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%2f451130%2fdo-not-replace-the-second-occurrence-of-pattern-in-a-same-word-in-the-file-with%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