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

The name of the pictureThe name of the pictureThe name of the pictureClash 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.







share|improve this question





















  • Very nearly a dupe of unix.stackexchange.com/questions/451130
    – Kusalananda
    Jun 22 at 11:38














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.







share|improve this question





















  • Very nearly a dupe of unix.stackexchange.com/questions/451130
    – Kusalananda
    Jun 22 at 11:38












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.







share|improve this question













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.









share|improve this question












share|improve this question




share|improve this question








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
















  • 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










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.






share|improve this answer



















  • 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

















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.






share|improve this answer






























    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.






    share|improve this answer























    • 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










    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    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






























    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.






    share|improve this answer



















    • 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














    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.






    share|improve this answer



















    • 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












    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.






    share|improve this answer















    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.







    share|improve this answer















    share|improve this answer



    share|improve this answer








    edited Jun 22 at 11:36


























    answered Jun 22 at 11:23









    jimmij

    28.7k86598




    28.7k86598







    • 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












    • 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







    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












    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.






    share|improve this answer



























      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.






      share|improve this answer

























        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.






        share|improve this answer















        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.







        share|improve this answer















        share|improve this answer



        share|improve this answer








        edited Jun 22 at 11:26


























        answered Jun 22 at 11:21









        choroba

        24.2k33967




        24.2k33967




















            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.






            share|improve this answer























            • 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














            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.






            share|improve this answer























            • 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












            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.






            share|improve this answer















            $ 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.







            share|improve this answer















            share|improve this answer



            share|improve this answer








            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
















            • 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












             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            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













































































            Popular posts from this blog

            Peggy Mitchell

            Palaiologos

            The Forum (Inglewood, California)