How to use sed to replace a word in a file with control characters [on hold]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
-1
down vote

favorite












How to replace this word in a file globally which has control characters. how can i do that ? I have tried the below with no success.



Replace "^Aabc_def_a^B" with "^Adef_a^B" globally


Tried the below



sed -i 's/^Aabc_def_a^B/^Adef_a^B/g' file.txt 

sed -i 's/^Aabc_def_a^B/^Adef_a^B/g'file.txt

sed 's/x01abc_def_ax02/x01def_ax02/g'









share|improve this question















put on hold as off-topic by DopeGhoti, Goro, Thomas, Isaac, andcoz 13 hours ago


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – DopeGhoti, Goro, Thomas, Isaac
If this question can be reworded to fit the rules in the help center, please edit the question.








  • 1




    Your question doesn't make sense, could you edit it to improve clarity.
    – X Tian
    yesterday














up vote
-1
down vote

favorite












How to replace this word in a file globally which has control characters. how can i do that ? I have tried the below with no success.



Replace "^Aabc_def_a^B" with "^Adef_a^B" globally


Tried the below



sed -i 's/^Aabc_def_a^B/^Adef_a^B/g' file.txt 

sed -i 's/^Aabc_def_a^B/^Adef_a^B/g'file.txt

sed 's/x01abc_def_ax02/x01def_ax02/g'









share|improve this question















put on hold as off-topic by DopeGhoti, Goro, Thomas, Isaac, andcoz 13 hours ago


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – DopeGhoti, Goro, Thomas, Isaac
If this question can be reworded to fit the rules in the help center, please edit the question.








  • 1




    Your question doesn't make sense, could you edit it to improve clarity.
    – X Tian
    yesterday












up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











How to replace this word in a file globally which has control characters. how can i do that ? I have tried the below with no success.



Replace "^Aabc_def_a^B" with "^Adef_a^B" globally


Tried the below



sed -i 's/^Aabc_def_a^B/^Adef_a^B/g' file.txt 

sed -i 's/^Aabc_def_a^B/^Adef_a^B/g'file.txt

sed 's/x01abc_def_ax02/x01def_ax02/g'









share|improve this question















How to replace this word in a file globally which has control characters. how can i do that ? I have tried the below with no success.



Replace "^Aabc_def_a^B" with "^Adef_a^B" globally


Tried the below



sed -i 's/^Aabc_def_a^B/^Adef_a^B/g' file.txt 

sed -i 's/^Aabc_def_a^B/^Adef_a^B/g'file.txt

sed 's/x01abc_def_ax02/x01def_ax02/g'






linux sed






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 1 hour ago

























asked yesterday









Tom

21




21




put on hold as off-topic by DopeGhoti, Goro, Thomas, Isaac, andcoz 13 hours ago


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – DopeGhoti, Goro, Thomas, Isaac
If this question can be reworded to fit the rules in the help center, please edit the question.




put on hold as off-topic by DopeGhoti, Goro, Thomas, Isaac, andcoz 13 hours ago


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – DopeGhoti, Goro, Thomas, Isaac
If this question can be reworded to fit the rules in the help center, please edit the question.







  • 1




    Your question doesn't make sense, could you edit it to improve clarity.
    – X Tian
    yesterday












  • 1




    Your question doesn't make sense, could you edit it to improve clarity.
    – X Tian
    yesterday







1




1




Your question doesn't make sense, could you edit it to improve clarity.
– X Tian
yesterday




Your question doesn't make sense, could you edit it to improve clarity.
– X Tian
yesterday










2 Answers
2






active

oldest

votes

















up vote
0
down vote













Seems to work for me:



$ echo -n "^Aword^B" | sed 's/^Aword^B/^Alexical unit^B/' | hexdump -C
00000000 01 6c 65 78 69 63 61 6c 20 75 6e 69 74 02 0a |.lexical unit..|
0000000f


01 represents ^A; 02 represents ^B.



Bear in mind that I used literal control characters, not a caret (^) followed by the A or B. To enter them when crafting your sed command, use Ctrl-V followed by the control character to insert it literally rather than having readline parse it as input.



In other words, to insert a ^A, press Ctrl-V followed by Ctrl-A, and you will see ^A displayed as your input. If you try to arrow past this, you will observe your cursor always treating this as a single unit even though it is comprised of two characters on your screen.






share|improve this answer



























    up vote
    0
    down vote













    In GNU sed, xHH works:



    $ printf '01foo02n' | sed 's/x01foox02/x01barx02/g' | od -c
    0000000 001 b a r 002 n


    If you don't have GNU sed, but have Bash, you can use the $'' quoting to generate the control characters in the shell:



    $ printf '01foo02n' | sed $'s/x01foox02/x01barx02/g' | od -c
    0000000 001 b a r 002 n


    If you have neither, there's always Perl which also understands xHH:



    $ printf '01foo02n' | perl -pe 's/x01foox02/x01barx02/g' | od -c
    0000000 001 b a r 002 n


    Of course you can do the same with -i.






    share|improve this answer



























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote













      Seems to work for me:



      $ echo -n "^Aword^B" | sed 's/^Aword^B/^Alexical unit^B/' | hexdump -C
      00000000 01 6c 65 78 69 63 61 6c 20 75 6e 69 74 02 0a |.lexical unit..|
      0000000f


      01 represents ^A; 02 represents ^B.



      Bear in mind that I used literal control characters, not a caret (^) followed by the A or B. To enter them when crafting your sed command, use Ctrl-V followed by the control character to insert it literally rather than having readline parse it as input.



      In other words, to insert a ^A, press Ctrl-V followed by Ctrl-A, and you will see ^A displayed as your input. If you try to arrow past this, you will observe your cursor always treating this as a single unit even though it is comprised of two characters on your screen.






      share|improve this answer
























        up vote
        0
        down vote













        Seems to work for me:



        $ echo -n "^Aword^B" | sed 's/^Aword^B/^Alexical unit^B/' | hexdump -C
        00000000 01 6c 65 78 69 63 61 6c 20 75 6e 69 74 02 0a |.lexical unit..|
        0000000f


        01 represents ^A; 02 represents ^B.



        Bear in mind that I used literal control characters, not a caret (^) followed by the A or B. To enter them when crafting your sed command, use Ctrl-V followed by the control character to insert it literally rather than having readline parse it as input.



        In other words, to insert a ^A, press Ctrl-V followed by Ctrl-A, and you will see ^A displayed as your input. If you try to arrow past this, you will observe your cursor always treating this as a single unit even though it is comprised of two characters on your screen.






        share|improve this answer






















          up vote
          0
          down vote










          up vote
          0
          down vote









          Seems to work for me:



          $ echo -n "^Aword^B" | sed 's/^Aword^B/^Alexical unit^B/' | hexdump -C
          00000000 01 6c 65 78 69 63 61 6c 20 75 6e 69 74 02 0a |.lexical unit..|
          0000000f


          01 represents ^A; 02 represents ^B.



          Bear in mind that I used literal control characters, not a caret (^) followed by the A or B. To enter them when crafting your sed command, use Ctrl-V followed by the control character to insert it literally rather than having readline parse it as input.



          In other words, to insert a ^A, press Ctrl-V followed by Ctrl-A, and you will see ^A displayed as your input. If you try to arrow past this, you will observe your cursor always treating this as a single unit even though it is comprised of two characters on your screen.






          share|improve this answer












          Seems to work for me:



          $ echo -n "^Aword^B" | sed 's/^Aword^B/^Alexical unit^B/' | hexdump -C
          00000000 01 6c 65 78 69 63 61 6c 20 75 6e 69 74 02 0a |.lexical unit..|
          0000000f


          01 represents ^A; 02 represents ^B.



          Bear in mind that I used literal control characters, not a caret (^) followed by the A or B. To enter them when crafting your sed command, use Ctrl-V followed by the control character to insert it literally rather than having readline parse it as input.



          In other words, to insert a ^A, press Ctrl-V followed by Ctrl-A, and you will see ^A displayed as your input. If you try to arrow past this, you will observe your cursor always treating this as a single unit even though it is comprised of two characters on your screen.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered yesterday









          DopeGhoti

          41.6k55180




          41.6k55180






















              up vote
              0
              down vote













              In GNU sed, xHH works:



              $ printf '01foo02n' | sed 's/x01foox02/x01barx02/g' | od -c
              0000000 001 b a r 002 n


              If you don't have GNU sed, but have Bash, you can use the $'' quoting to generate the control characters in the shell:



              $ printf '01foo02n' | sed $'s/x01foox02/x01barx02/g' | od -c
              0000000 001 b a r 002 n


              If you have neither, there's always Perl which also understands xHH:



              $ printf '01foo02n' | perl -pe 's/x01foox02/x01barx02/g' | od -c
              0000000 001 b a r 002 n


              Of course you can do the same with -i.






              share|improve this answer
























                up vote
                0
                down vote













                In GNU sed, xHH works:



                $ printf '01foo02n' | sed 's/x01foox02/x01barx02/g' | od -c
                0000000 001 b a r 002 n


                If you don't have GNU sed, but have Bash, you can use the $'' quoting to generate the control characters in the shell:



                $ printf '01foo02n' | sed $'s/x01foox02/x01barx02/g' | od -c
                0000000 001 b a r 002 n


                If you have neither, there's always Perl which also understands xHH:



                $ printf '01foo02n' | perl -pe 's/x01foox02/x01barx02/g' | od -c
                0000000 001 b a r 002 n


                Of course you can do the same with -i.






                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  In GNU sed, xHH works:



                  $ printf '01foo02n' | sed 's/x01foox02/x01barx02/g' | od -c
                  0000000 001 b a r 002 n


                  If you don't have GNU sed, but have Bash, you can use the $'' quoting to generate the control characters in the shell:



                  $ printf '01foo02n' | sed $'s/x01foox02/x01barx02/g' | od -c
                  0000000 001 b a r 002 n


                  If you have neither, there's always Perl which also understands xHH:



                  $ printf '01foo02n' | perl -pe 's/x01foox02/x01barx02/g' | od -c
                  0000000 001 b a r 002 n


                  Of course you can do the same with -i.






                  share|improve this answer












                  In GNU sed, xHH works:



                  $ printf '01foo02n' | sed 's/x01foox02/x01barx02/g' | od -c
                  0000000 001 b a r 002 n


                  If you don't have GNU sed, but have Bash, you can use the $'' quoting to generate the control characters in the shell:



                  $ printf '01foo02n' | sed $'s/x01foox02/x01barx02/g' | od -c
                  0000000 001 b a r 002 n


                  If you have neither, there's always Perl which also understands xHH:



                  $ printf '01foo02n' | perl -pe 's/x01foox02/x01barx02/g' | od -c
                  0000000 001 b a r 002 n


                  Of course you can do the same with -i.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered yesterday









                  ilkkachu

                  52.8k679145




                  52.8k679145












                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Bahrain

                      Postfix configuration issue with fips on centos 7; mailgun relay