Replace hex eight digits only, ignore if less than or more than 8 digits together. Using sed command

Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
Example:
Input:
0x12345678 0aef1234 0098adefa 123456789
Expected Output:
0x00000000 00000000 0098adefa 123456789
Tried this:
sed -E "s/[0-9a-fA-F]8/00000000/g"
But this replaces even if there are more than 8 continues hex digits.
linux shell-script sed
add a comment |Â
up vote
2
down vote
favorite
Example:
Input:
0x12345678 0aef1234 0098adefa 123456789
Expected Output:
0x00000000 00000000 0098adefa 123456789
Tried this:
sed -E "s/[0-9a-fA-F]8/00000000/g"
But this replaces even if there are more than 8 continues hex digits.
linux shell-script sed
Very nearly a dupe of unix.stackexchange.com/questions/451130
â Kusalananda
Jun 22 at 11:38
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
Example:
Input:
0x12345678 0aef1234 0098adefa 123456789
Expected Output:
0x00000000 00000000 0098adefa 123456789
Tried this:
sed -E "s/[0-9a-fA-F]8/00000000/g"
But this replaces even if there are more than 8 continues hex digits.
linux shell-script sed
Example:
Input:
0x12345678 0aef1234 0098adefa 123456789
Expected Output:
0x00000000 00000000 0098adefa 123456789
Tried this:
sed -E "s/[0-9a-fA-F]8/00000000/g"
But this replaces even if there are more than 8 continues hex digits.
linux shell-script sed
edited Jun 22 at 11:04
Thomas
3,38941023
3,38941023
asked Jun 22 at 11:00
Hidayathullah Khan
111
111
Very nearly a dupe of unix.stackexchange.com/questions/451130
â Kusalananda
Jun 22 at 11:38
add a comment |Â
Very nearly a dupe of unix.stackexchange.com/questions/451130
â Kusalananda
Jun 22 at 11:38
Very nearly a dupe of unix.stackexchange.com/questions/451130
â Kusalananda
Jun 22 at 11:38
Very nearly a dupe of unix.stackexchange.com/questions/451130
â Kusalananda
Jun 22 at 11:38
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
5
down vote
You can use word boundary b to mark the beginning or the end of a word, and additionally one needs to take into account that the 0x is possible in front of the number:
sed -E 's/b(0x|)[0-9a-fA-F]8b/100000000/g'
Still, there are special cases which you need to think through what to do with them, like 00x12345678, in the above pattern it is not replaced.
2
@NicHartley:sedsupports?without extended regular expressions, but it must be escaped with a backslash: for instance,sed -e 's/a?b/'.
â wchargin
Jun 22 at 17:55
add a comment |Â
up vote
4
down vote
In Perl, you can use the "look-around assertions", i.e. you can say "not preceded by a hexdigit" and "not followed by a hexdigit":
perl -pe 's/(?<![0-9a-fA-F])[0-9a-fA-F]8(?![0-9a-fA-F])/00000000/g'
The look-arounds don't count into the matched string, i.e. they aren't replaced and they can match more than once (once in the look-behind and once in the look-ahead).
You can also use the [:xdigit:] POSIX class instead of 0-9a-fA-F.
add a comment |Â
up vote
1
down vote
$ echo '0x12345678 0aef1234 0098adefa 123456789' | sed -E 's/(<|x)[[:xdigit:]]8>/100000000/g'
0x00000000 00000000 0098adefa 123456789
$ echo '12345678 0aef1234 0098adefa 123456789' | sed -E 's/(<|x)[[:xdigit:]]8>/100000000/g'
00000000 00000000 0098adefa 123456789
The regular expression
(<|x)[[:xdigit:]]8>
will match a hexadecimal number that is eight digits long. It should be a complete word, or be preceded by an x. The preceding character (the x, if there is one) is saved and inserted before the zeros in the replacement.
Thanks. Command is working as expected except for first word. For input: "12345678 0x12345678 abcd12345" the output is "12345678 0x00000000 abcd12345" it did not replace first word though 8 digit hex. Expected Output: "00000000 0x00000000 abcd12345"
â Hidayathullah Khan
Jun 22 at 12:19
@HidayathullahKhan There was no indication in the question that input on that format was expected. You should add that there.
â Kusalananda
Jun 22 at 12:23
@HidayathullahKhan I have taken this into account now.
â Kusalananda
Jun 22 at 12:26
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
You can use word boundary b to mark the beginning or the end of a word, and additionally one needs to take into account that the 0x is possible in front of the number:
sed -E 's/b(0x|)[0-9a-fA-F]8b/100000000/g'
Still, there are special cases which you need to think through what to do with them, like 00x12345678, in the above pattern it is not replaced.
2
@NicHartley:sedsupports?without extended regular expressions, but it must be escaped with a backslash: for instance,sed -e 's/a?b/'.
â wchargin
Jun 22 at 17:55
add a comment |Â
up vote
5
down vote
You can use word boundary b to mark the beginning or the end of a word, and additionally one needs to take into account that the 0x is possible in front of the number:
sed -E 's/b(0x|)[0-9a-fA-F]8b/100000000/g'
Still, there are special cases which you need to think through what to do with them, like 00x12345678, in the above pattern it is not replaced.
2
@NicHartley:sedsupports?without extended regular expressions, but it must be escaped with a backslash: for instance,sed -e 's/a?b/'.
â wchargin
Jun 22 at 17:55
add a comment |Â
up vote
5
down vote
up vote
5
down vote
You can use word boundary b to mark the beginning or the end of a word, and additionally one needs to take into account that the 0x is possible in front of the number:
sed -E 's/b(0x|)[0-9a-fA-F]8b/100000000/g'
Still, there are special cases which you need to think through what to do with them, like 00x12345678, in the above pattern it is not replaced.
You can use word boundary b to mark the beginning or the end of a word, and additionally one needs to take into account that the 0x is possible in front of the number:
sed -E 's/b(0x|)[0-9a-fA-F]8b/100000000/g'
Still, there are special cases which you need to think through what to do with them, like 00x12345678, in the above pattern it is not replaced.
edited Jun 22 at 11:36
answered Jun 22 at 11:23
jimmij
28.7k86598
28.7k86598
2
@NicHartley:sedsupports?without extended regular expressions, but it must be escaped with a backslash: for instance,sed -e 's/a?b/'.
â wchargin
Jun 22 at 17:55
add a comment |Â
2
@NicHartley:sedsupports?without extended regular expressions, but it must be escaped with a backslash: for instance,sed -e 's/a?b/'.
â wchargin
Jun 22 at 17:55
2
2
@NicHartley:
sed supports ? without extended regular expressions, but it must be escaped with a backslash: for instance, sed -e 's/a?b/'.â wchargin
Jun 22 at 17:55
@NicHartley:
sed supports ? without extended regular expressions, but it must be escaped with a backslash: for instance, sed -e 's/a?b/'.â wchargin
Jun 22 at 17:55
add a comment |Â
up vote
4
down vote
In Perl, you can use the "look-around assertions", i.e. you can say "not preceded by a hexdigit" and "not followed by a hexdigit":
perl -pe 's/(?<![0-9a-fA-F])[0-9a-fA-F]8(?![0-9a-fA-F])/00000000/g'
The look-arounds don't count into the matched string, i.e. they aren't replaced and they can match more than once (once in the look-behind and once in the look-ahead).
You can also use the [:xdigit:] POSIX class instead of 0-9a-fA-F.
add a comment |Â
up vote
4
down vote
In Perl, you can use the "look-around assertions", i.e. you can say "not preceded by a hexdigit" and "not followed by a hexdigit":
perl -pe 's/(?<![0-9a-fA-F])[0-9a-fA-F]8(?![0-9a-fA-F])/00000000/g'
The look-arounds don't count into the matched string, i.e. they aren't replaced and they can match more than once (once in the look-behind and once in the look-ahead).
You can also use the [:xdigit:] POSIX class instead of 0-9a-fA-F.
add a comment |Â
up vote
4
down vote
up vote
4
down vote
In Perl, you can use the "look-around assertions", i.e. you can say "not preceded by a hexdigit" and "not followed by a hexdigit":
perl -pe 's/(?<![0-9a-fA-F])[0-9a-fA-F]8(?![0-9a-fA-F])/00000000/g'
The look-arounds don't count into the matched string, i.e. they aren't replaced and they can match more than once (once in the look-behind and once in the look-ahead).
You can also use the [:xdigit:] POSIX class instead of 0-9a-fA-F.
In Perl, you can use the "look-around assertions", i.e. you can say "not preceded by a hexdigit" and "not followed by a hexdigit":
perl -pe 's/(?<![0-9a-fA-F])[0-9a-fA-F]8(?![0-9a-fA-F])/00000000/g'
The look-arounds don't count into the matched string, i.e. they aren't replaced and they can match more than once (once in the look-behind and once in the look-ahead).
You can also use the [:xdigit:] POSIX class instead of 0-9a-fA-F.
edited Jun 22 at 11:26
answered Jun 22 at 11:21
choroba
24.2k33967
24.2k33967
add a comment |Â
add a comment |Â
up vote
1
down vote
$ echo '0x12345678 0aef1234 0098adefa 123456789' | sed -E 's/(<|x)[[:xdigit:]]8>/100000000/g'
0x00000000 00000000 0098adefa 123456789
$ echo '12345678 0aef1234 0098adefa 123456789' | sed -E 's/(<|x)[[:xdigit:]]8>/100000000/g'
00000000 00000000 0098adefa 123456789
The regular expression
(<|x)[[:xdigit:]]8>
will match a hexadecimal number that is eight digits long. It should be a complete word, or be preceded by an x. The preceding character (the x, if there is one) is saved and inserted before the zeros in the replacement.
Thanks. Command is working as expected except for first word. For input: "12345678 0x12345678 abcd12345" the output is "12345678 0x00000000 abcd12345" it did not replace first word though 8 digit hex. Expected Output: "00000000 0x00000000 abcd12345"
â Hidayathullah Khan
Jun 22 at 12:19
@HidayathullahKhan There was no indication in the question that input on that format was expected. You should add that there.
â Kusalananda
Jun 22 at 12:23
@HidayathullahKhan I have taken this into account now.
â Kusalananda
Jun 22 at 12:26
add a comment |Â
up vote
1
down vote
$ echo '0x12345678 0aef1234 0098adefa 123456789' | sed -E 's/(<|x)[[:xdigit:]]8>/100000000/g'
0x00000000 00000000 0098adefa 123456789
$ echo '12345678 0aef1234 0098adefa 123456789' | sed -E 's/(<|x)[[:xdigit:]]8>/100000000/g'
00000000 00000000 0098adefa 123456789
The regular expression
(<|x)[[:xdigit:]]8>
will match a hexadecimal number that is eight digits long. It should be a complete word, or be preceded by an x. The preceding character (the x, if there is one) is saved and inserted before the zeros in the replacement.
Thanks. Command is working as expected except for first word. For input: "12345678 0x12345678 abcd12345" the output is "12345678 0x00000000 abcd12345" it did not replace first word though 8 digit hex. Expected Output: "00000000 0x00000000 abcd12345"
â Hidayathullah Khan
Jun 22 at 12:19
@HidayathullahKhan There was no indication in the question that input on that format was expected. You should add that there.
â Kusalananda
Jun 22 at 12:23
@HidayathullahKhan I have taken this into account now.
â Kusalananda
Jun 22 at 12:26
add a comment |Â
up vote
1
down vote
up vote
1
down vote
$ echo '0x12345678 0aef1234 0098adefa 123456789' | sed -E 's/(<|x)[[:xdigit:]]8>/100000000/g'
0x00000000 00000000 0098adefa 123456789
$ echo '12345678 0aef1234 0098adefa 123456789' | sed -E 's/(<|x)[[:xdigit:]]8>/100000000/g'
00000000 00000000 0098adefa 123456789
The regular expression
(<|x)[[:xdigit:]]8>
will match a hexadecimal number that is eight digits long. It should be a complete word, or be preceded by an x. The preceding character (the x, if there is one) is saved and inserted before the zeros in the replacement.
$ echo '0x12345678 0aef1234 0098adefa 123456789' | sed -E 's/(<|x)[[:xdigit:]]8>/100000000/g'
0x00000000 00000000 0098adefa 123456789
$ echo '12345678 0aef1234 0098adefa 123456789' | sed -E 's/(<|x)[[:xdigit:]]8>/100000000/g'
00000000 00000000 0098adefa 123456789
The regular expression
(<|x)[[:xdigit:]]8>
will match a hexadecimal number that is eight digits long. It should be a complete word, or be preceded by an x. The preceding character (the x, if there is one) is saved and inserted before the zeros in the replacement.
edited Jun 22 at 12:26
answered Jun 22 at 11:45
Kusalananda
101k13199312
101k13199312
Thanks. Command is working as expected except for first word. For input: "12345678 0x12345678 abcd12345" the output is "12345678 0x00000000 abcd12345" it did not replace first word though 8 digit hex. Expected Output: "00000000 0x00000000 abcd12345"
â Hidayathullah Khan
Jun 22 at 12:19
@HidayathullahKhan There was no indication in the question that input on that format was expected. You should add that there.
â Kusalananda
Jun 22 at 12:23
@HidayathullahKhan I have taken this into account now.
â Kusalananda
Jun 22 at 12:26
add a comment |Â
Thanks. Command is working as expected except for first word. For input: "12345678 0x12345678 abcd12345" the output is "12345678 0x00000000 abcd12345" it did not replace first word though 8 digit hex. Expected Output: "00000000 0x00000000 abcd12345"
â Hidayathullah Khan
Jun 22 at 12:19
@HidayathullahKhan There was no indication in the question that input on that format was expected. You should add that there.
â Kusalananda
Jun 22 at 12:23
@HidayathullahKhan I have taken this into account now.
â Kusalananda
Jun 22 at 12:26
Thanks. Command is working as expected except for first word. For input: "12345678 0x12345678 abcd12345" the output is "12345678 0x00000000 abcd12345" it did not replace first word though 8 digit hex. Expected Output: "00000000 0x00000000 abcd12345"
â Hidayathullah Khan
Jun 22 at 12:19
Thanks. Command is working as expected except for first word. For input: "12345678 0x12345678 abcd12345" the output is "12345678 0x00000000 abcd12345" it did not replace first word though 8 digit hex. Expected Output: "00000000 0x00000000 abcd12345"
â Hidayathullah Khan
Jun 22 at 12:19
@HidayathullahKhan There was no indication in the question that input on that format was expected. You should add that there.
â Kusalananda
Jun 22 at 12:23
@HidayathullahKhan There was no indication in the question that input on that format was expected. You should add that there.
â Kusalananda
Jun 22 at 12:23
@HidayathullahKhan I have taken this into account now.
â Kusalananda
Jun 22 at 12:26
@HidayathullahKhan I have taken this into account now.
â Kusalananda
Jun 22 at 12:26
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%2f451270%2freplace-hex-eight-digits-only-ignore-if-less-than-or-more-than-8-digits-togethe%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
Very nearly a dupe of unix.stackexchange.com/questions/451130
â Kusalananda
Jun 22 at 11:38