How to use sed to replace a word in a file with control characters [on hold]
Clash 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'
linux sed
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
add a comment |Â
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'
linux sed
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
1
Your question doesn't make sense, could you edit it to improve clarity.
â X Tian
yesterday
add a comment |Â
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'
linux sed
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
linux sed
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
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
1
Your question doesn't make sense, could you edit it to improve clarity.
â X Tian
yesterday
add a comment |Â
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
add a comment |Â
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.
add a comment |Â
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
.
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
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.
answered yesterday
DopeGhoti
41.6k55180
41.6k55180
add a comment |Â
add a comment |Â
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
.
add a comment |Â
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
.
add a comment |Â
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
.
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
.
answered yesterday
ilkkachu
52.8k679145
52.8k679145
add a comment |Â
add a comment |Â
1
Your question doesn't make sense, could you edit it to improve clarity.
â X Tian
yesterday