How can I add a backslash using sed? [duplicate]
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgO9GURib1T8z7lCwjOGLQaGtrueEthgQ8LO42ZX8cOfTqDK4jvDDpKkLFwf2J49kYCMNW7d4ABih_XCb_2UXdq5fPJDkoyg7-8g_YfRUot-XnaXkNYycsNp7lA5_TW9td0FFpLQ2APzKcZ/s1600/1.jpg)
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYQ0N5W1qAOxLP7t7iOM6O6AzbZnkXUy16s7P_CWfOb5UbTQY_aDsc727chyphenhyphen5W4IppVNernMMQeaUFTB_rFzAd95_CDt-tnwN-nBx6JyUp2duGjPaL5-VgNO41AVsA_vu30EJcipdDG409/s400/Clash+Royale+CLAN+TAG%2523URR8PPP.png)
up vote
0
down vote
favorite
This question already has an answer here:
Search and replace a pattern containing backslashes
1 answer
I want to add a backslash printed between
Hello
and World
in command line using sed
. It should show output Hello World
.
What do I have to do?
command-line text-processing sed
marked as duplicate by muru, karel, David Foerster, Eric Carvalho, Aaron Apr 24 at 14:49
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |Â
up vote
0
down vote
favorite
This question already has an answer here:
Search and replace a pattern containing backslashes
1 answer
I want to add a backslash printed between
Hello
and World
in command line using sed
. It should show output Hello World
.
What do I have to do?
command-line text-processing sed
marked as duplicate by muru, karel, David Foerster, Eric Carvalho, Aaron Apr 24 at 14:49
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
that backslash is not very useful. If it's intended to escape the space when the string is used again for something, it needs to come before the space, likehello world
â Zanna
Apr 22 at 7:05
1
Related: Search and replace a pattern containing backslashes
â Zanna
Apr 22 at 7:06
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
This question already has an answer here:
Search and replace a pattern containing backslashes
1 answer
I want to add a backslash printed between
Hello
and World
in command line using sed
. It should show output Hello World
.
What do I have to do?
command-line text-processing sed
This question already has an answer here:
Search and replace a pattern containing backslashes
1 answer
I want to add a backslash printed between
Hello
and World
in command line using sed
. It should show output Hello World
.
What do I have to do?
This question already has an answer here:
Search and replace a pattern containing backslashes
1 answer
command-line text-processing sed
edited Apr 24 at 12:08
![](https://i.stack.imgur.com/h5FrU.png?s=32&g=1)
![](https://i.stack.imgur.com/h5FrU.png?s=32&g=1)
anonymous2
3,14541746
3,14541746
asked Apr 22 at 6:42
Garry14
61
61
marked as duplicate by muru, karel, David Foerster, Eric Carvalho, Aaron Apr 24 at 14:49
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by muru, karel, David Foerster, Eric Carvalho, Aaron Apr 24 at 14:49
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
that backslash is not very useful. If it's intended to escape the space when the string is used again for something, it needs to come before the space, likehello world
â Zanna
Apr 22 at 7:05
1
Related: Search and replace a pattern containing backslashes
â Zanna
Apr 22 at 7:06
add a comment |Â
1
that backslash is not very useful. If it's intended to escape the space when the string is used again for something, it needs to come before the space, likehello world
â Zanna
Apr 22 at 7:05
1
Related: Search and replace a pattern containing backslashes
â Zanna
Apr 22 at 7:06
1
1
that backslash is not very useful. If it's intended to escape the space when the string is used again for something, it needs to come before the space, like
hello world
â Zanna
Apr 22 at 7:05
that backslash is not very useful. If it's intended to escape the space when the string is used again for something, it needs to come before the space, like
hello world
â Zanna
Apr 22 at 7:05
1
1
Related: Search and replace a pattern containing backslashes
â Zanna
Apr 22 at 7:06
Related: Search and replace a pattern containing backslashes
â Zanna
Apr 22 at 7:06
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
2
down vote
If you have a file named hw
containing Hello World
, the sed command would be:
sed 's/ / \ /' hw
This displays the wanted result on the screen. If you want to edit the file, add -i
:
sed -i 's/ / \ /' hw
The command replaces the space by spacespace. You need two \
because is an escape character.
3
Perl people call this leaning toothpick syndrome for obvious reasons.
â PerlDuck
Apr 22 at 9:15
add a comment |Â
up vote
1
down vote
I used a text file containing the texts Hello World
and with this sed command:
sed 's/Hello/Hello \/' helloworld.txt
And the output is:
Hello World
Note: This can be used too:
sed 's/World/\ World/' helloworld.txt
The sed command finds the Hello
text and adds a to the front and that outputs the result seen. The
\
escapes the so it's seen as a real (literal)
not a special character.
add a comment |Â
up vote
-3
down vote
I used at the end of code like this:
sed -i -r "Hello \ World \" mytext.txt
This resulted in Hello World
, as desired. But thank you, all.
Sorry I was wrong typing for my answer and I have edited it..
â Garry14
Apr 23 at 2:53
3
Thanks, that does make a little more sense, but it's still missing an actualsed
command I think? Can you check again what exactly you wrote?
â Zanna
Apr 23 at 5:50
Is this supposed to be an answer or an explanation of what you tried (incorrectly) before you read the other answers?
â David Foerster
Apr 24 at 8:32
2
@Garry14 The command you typed here wassed -i -r "Hello \ World \" mytext.txt
, but each\
was displayed as. That's fixed now, and we've undeleted this, but it still doesn't fully make sense. You need a command, like
c
, to make this work. That is,sed -i -r "cHello \ World \" mytext.txt
(better writtensed -i -r 'cHello World ' mytext.txt
) does work. Is it possible you ran such a command? Can you edit to clarify? Oncec
or something like it is added, the backslash at the end does actually make the one in the middle appear, which is quite interesting.
â Eliah Kagan
Apr 24 at 9:06
3
One possibility is that rather than the-i
flag, or additionally to the-i
flag, you usedsed
'si
(insert) command. That would produce the output you showed, provided there was already something in the file. Note that since the shell will not do anything toi
by itself, it needn't be quoted. This works:sed i"Hello \ World \"
and this also workssed "iHello \ World \"
(whether or not the-i
and-r
flags are included)
â Zanna
Apr 24 at 9:12
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
If you have a file named hw
containing Hello World
, the sed command would be:
sed 's/ / \ /' hw
This displays the wanted result on the screen. If you want to edit the file, add -i
:
sed -i 's/ / \ /' hw
The command replaces the space by spacespace. You need two \
because is an escape character.
3
Perl people call this leaning toothpick syndrome for obvious reasons.
â PerlDuck
Apr 22 at 9:15
add a comment |Â
up vote
2
down vote
If you have a file named hw
containing Hello World
, the sed command would be:
sed 's/ / \ /' hw
This displays the wanted result on the screen. If you want to edit the file, add -i
:
sed -i 's/ / \ /' hw
The command replaces the space by spacespace. You need two \
because is an escape character.
3
Perl people call this leaning toothpick syndrome for obvious reasons.
â PerlDuck
Apr 22 at 9:15
add a comment |Â
up vote
2
down vote
up vote
2
down vote
If you have a file named hw
containing Hello World
, the sed command would be:
sed 's/ / \ /' hw
This displays the wanted result on the screen. If you want to edit the file, add -i
:
sed -i 's/ / \ /' hw
The command replaces the space by spacespace. You need two \
because is an escape character.
If you have a file named hw
containing Hello World
, the sed command would be:
sed 's/ / \ /' hw
This displays the wanted result on the screen. If you want to edit the file, add -i
:
sed -i 's/ / \ /' hw
The command replaces the space by spacespace. You need two \
because is an escape character.
edited Apr 24 at 8:31
![](https://i.stack.imgur.com/E0SEH.png?s=32&g=1)
![](https://i.stack.imgur.com/E0SEH.png?s=32&g=1)
David Foerster
26.1k1361106
26.1k1361106
answered Apr 22 at 6:52
![](https://i.stack.imgur.com/MATL0.jpg?s=32&g=1)
![](https://i.stack.imgur.com/MATL0.jpg?s=32&g=1)
muclux
2,1231521
2,1231521
3
Perl people call this leaning toothpick syndrome for obvious reasons.
â PerlDuck
Apr 22 at 9:15
add a comment |Â
3
Perl people call this leaning toothpick syndrome for obvious reasons.
â PerlDuck
Apr 22 at 9:15
3
3
Perl people call this leaning toothpick syndrome for obvious reasons.
â PerlDuck
Apr 22 at 9:15
Perl people call this leaning toothpick syndrome for obvious reasons.
â PerlDuck
Apr 22 at 9:15
add a comment |Â
up vote
1
down vote
I used a text file containing the texts Hello World
and with this sed command:
sed 's/Hello/Hello \/' helloworld.txt
And the output is:
Hello World
Note: This can be used too:
sed 's/World/\ World/' helloworld.txt
The sed command finds the Hello
text and adds a to the front and that outputs the result seen. The
\
escapes the so it's seen as a real (literal)
not a special character.
add a comment |Â
up vote
1
down vote
I used a text file containing the texts Hello World
and with this sed command:
sed 's/Hello/Hello \/' helloworld.txt
And the output is:
Hello World
Note: This can be used too:
sed 's/World/\ World/' helloworld.txt
The sed command finds the Hello
text and adds a to the front and that outputs the result seen. The
\
escapes the so it's seen as a real (literal)
not a special character.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
I used a text file containing the texts Hello World
and with this sed command:
sed 's/Hello/Hello \/' helloworld.txt
And the output is:
Hello World
Note: This can be used too:
sed 's/World/\ World/' helloworld.txt
The sed command finds the Hello
text and adds a to the front and that outputs the result seen. The
\
escapes the so it's seen as a real (literal)
not a special character.
I used a text file containing the texts Hello World
and with this sed command:
sed 's/Hello/Hello \/' helloworld.txt
And the output is:
Hello World
Note: This can be used too:
sed 's/World/\ World/' helloworld.txt
The sed command finds the Hello
text and adds a to the front and that outputs the result seen. The
\
escapes the so it's seen as a real (literal)
not a special character.
edited Apr 23 at 5:58
Melebius
3,75741636
3,75741636
answered Apr 22 at 6:53
![](https://i.stack.imgur.com/ElvwO.jpg?s=32&g=1)
![](https://i.stack.imgur.com/ElvwO.jpg?s=32&g=1)
George Udosen
17k93559
17k93559
add a comment |Â
add a comment |Â
up vote
-3
down vote
I used at the end of code like this:
sed -i -r "Hello \ World \" mytext.txt
This resulted in Hello World
, as desired. But thank you, all.
Sorry I was wrong typing for my answer and I have edited it..
â Garry14
Apr 23 at 2:53
3
Thanks, that does make a little more sense, but it's still missing an actualsed
command I think? Can you check again what exactly you wrote?
â Zanna
Apr 23 at 5:50
Is this supposed to be an answer or an explanation of what you tried (incorrectly) before you read the other answers?
â David Foerster
Apr 24 at 8:32
2
@Garry14 The command you typed here wassed -i -r "Hello \ World \" mytext.txt
, but each\
was displayed as. That's fixed now, and we've undeleted this, but it still doesn't fully make sense. You need a command, like
c
, to make this work. That is,sed -i -r "cHello \ World \" mytext.txt
(better writtensed -i -r 'cHello World ' mytext.txt
) does work. Is it possible you ran such a command? Can you edit to clarify? Oncec
or something like it is added, the backslash at the end does actually make the one in the middle appear, which is quite interesting.
â Eliah Kagan
Apr 24 at 9:06
3
One possibility is that rather than the-i
flag, or additionally to the-i
flag, you usedsed
'si
(insert) command. That would produce the output you showed, provided there was already something in the file. Note that since the shell will not do anything toi
by itself, it needn't be quoted. This works:sed i"Hello \ World \"
and this also workssed "iHello \ World \"
(whether or not the-i
and-r
flags are included)
â Zanna
Apr 24 at 9:12
add a comment |Â
up vote
-3
down vote
I used at the end of code like this:
sed -i -r "Hello \ World \" mytext.txt
This resulted in Hello World
, as desired. But thank you, all.
Sorry I was wrong typing for my answer and I have edited it..
â Garry14
Apr 23 at 2:53
3
Thanks, that does make a little more sense, but it's still missing an actualsed
command I think? Can you check again what exactly you wrote?
â Zanna
Apr 23 at 5:50
Is this supposed to be an answer or an explanation of what you tried (incorrectly) before you read the other answers?
â David Foerster
Apr 24 at 8:32
2
@Garry14 The command you typed here wassed -i -r "Hello \ World \" mytext.txt
, but each\
was displayed as. That's fixed now, and we've undeleted this, but it still doesn't fully make sense. You need a command, like
c
, to make this work. That is,sed -i -r "cHello \ World \" mytext.txt
(better writtensed -i -r 'cHello World ' mytext.txt
) does work. Is it possible you ran such a command? Can you edit to clarify? Oncec
or something like it is added, the backslash at the end does actually make the one in the middle appear, which is quite interesting.
â Eliah Kagan
Apr 24 at 9:06
3
One possibility is that rather than the-i
flag, or additionally to the-i
flag, you usedsed
'si
(insert) command. That would produce the output you showed, provided there was already something in the file. Note that since the shell will not do anything toi
by itself, it needn't be quoted. This works:sed i"Hello \ World \"
and this also workssed "iHello \ World \"
(whether or not the-i
and-r
flags are included)
â Zanna
Apr 24 at 9:12
add a comment |Â
up vote
-3
down vote
up vote
-3
down vote
I used at the end of code like this:
sed -i -r "Hello \ World \" mytext.txt
This resulted in Hello World
, as desired. But thank you, all.
I used at the end of code like this:
sed -i -r "Hello \ World \" mytext.txt
This resulted in Hello World
, as desired. But thank you, all.
edited Apr 24 at 8:36
Eliah Kagan
79.5k20221359
79.5k20221359
answered Apr 22 at 7:20
Garry14
61
61
Sorry I was wrong typing for my answer and I have edited it..
â Garry14
Apr 23 at 2:53
3
Thanks, that does make a little more sense, but it's still missing an actualsed
command I think? Can you check again what exactly you wrote?
â Zanna
Apr 23 at 5:50
Is this supposed to be an answer or an explanation of what you tried (incorrectly) before you read the other answers?
â David Foerster
Apr 24 at 8:32
2
@Garry14 The command you typed here wassed -i -r "Hello \ World \" mytext.txt
, but each\
was displayed as. That's fixed now, and we've undeleted this, but it still doesn't fully make sense. You need a command, like
c
, to make this work. That is,sed -i -r "cHello \ World \" mytext.txt
(better writtensed -i -r 'cHello World ' mytext.txt
) does work. Is it possible you ran such a command? Can you edit to clarify? Oncec
or something like it is added, the backslash at the end does actually make the one in the middle appear, which is quite interesting.
â Eliah Kagan
Apr 24 at 9:06
3
One possibility is that rather than the-i
flag, or additionally to the-i
flag, you usedsed
'si
(insert) command. That would produce the output you showed, provided there was already something in the file. Note that since the shell will not do anything toi
by itself, it needn't be quoted. This works:sed i"Hello \ World \"
and this also workssed "iHello \ World \"
(whether or not the-i
and-r
flags are included)
â Zanna
Apr 24 at 9:12
add a comment |Â
Sorry I was wrong typing for my answer and I have edited it..
â Garry14
Apr 23 at 2:53
3
Thanks, that does make a little more sense, but it's still missing an actualsed
command I think? Can you check again what exactly you wrote?
â Zanna
Apr 23 at 5:50
Is this supposed to be an answer or an explanation of what you tried (incorrectly) before you read the other answers?
â David Foerster
Apr 24 at 8:32
2
@Garry14 The command you typed here wassed -i -r "Hello \ World \" mytext.txt
, but each\
was displayed as. That's fixed now, and we've undeleted this, but it still doesn't fully make sense. You need a command, like
c
, to make this work. That is,sed -i -r "cHello \ World \" mytext.txt
(better writtensed -i -r 'cHello World ' mytext.txt
) does work. Is it possible you ran such a command? Can you edit to clarify? Oncec
or something like it is added, the backslash at the end does actually make the one in the middle appear, which is quite interesting.
â Eliah Kagan
Apr 24 at 9:06
3
One possibility is that rather than the-i
flag, or additionally to the-i
flag, you usedsed
'si
(insert) command. That would produce the output you showed, provided there was already something in the file. Note that since the shell will not do anything toi
by itself, it needn't be quoted. This works:sed i"Hello \ World \"
and this also workssed "iHello \ World \"
(whether or not the-i
and-r
flags are included)
â Zanna
Apr 24 at 9:12
Sorry I was wrong typing for my answer and I have edited it..
â Garry14
Apr 23 at 2:53
Sorry I was wrong typing for my answer and I have edited it..
â Garry14
Apr 23 at 2:53
3
3
Thanks, that does make a little more sense, but it's still missing an actual
sed
command I think? Can you check again what exactly you wrote?â Zanna
Apr 23 at 5:50
Thanks, that does make a little more sense, but it's still missing an actual
sed
command I think? Can you check again what exactly you wrote?â Zanna
Apr 23 at 5:50
Is this supposed to be an answer or an explanation of what you tried (incorrectly) before you read the other answers?
â David Foerster
Apr 24 at 8:32
Is this supposed to be an answer or an explanation of what you tried (incorrectly) before you read the other answers?
â David Foerster
Apr 24 at 8:32
2
2
@Garry14 The command you typed here was
sed -i -r "Hello \ World \" mytext.txt
, but each \
was displayed as
. That's fixed now, and we've undeleted this, but it still doesn't fully make sense. You need a command, like c
, to make this work. That is, sed -i -r "cHello \ World \" mytext.txt
(better written sed -i -r 'cHello World ' mytext.txt
) does work. Is it possible you ran such a command? Can you edit to clarify? Once c
or something like it is added, the backslash at the end does actually make the one in the middle appear, which is quite interesting.â Eliah Kagan
Apr 24 at 9:06
@Garry14 The command you typed here was
sed -i -r "Hello \ World \" mytext.txt
, but each \
was displayed as
. That's fixed now, and we've undeleted this, but it still doesn't fully make sense. You need a command, like c
, to make this work. That is, sed -i -r "cHello \ World \" mytext.txt
(better written sed -i -r 'cHello World ' mytext.txt
) does work. Is it possible you ran such a command? Can you edit to clarify? Once c
or something like it is added, the backslash at the end does actually make the one in the middle appear, which is quite interesting.â Eliah Kagan
Apr 24 at 9:06
3
3
One possibility is that rather than the
-i
flag, or additionally to the -i
flag, you used sed
's i
(insert) command. That would produce the output you showed, provided there was already something in the file. Note that since the shell will not do anything to i
by itself, it needn't be quoted. This works: sed i"Hello \ World \"
and this also works sed "iHello \ World \"
(whether or not the -i
and -r
flags are included)â Zanna
Apr 24 at 9:12
One possibility is that rather than the
-i
flag, or additionally to the -i
flag, you used sed
's i
(insert) command. That would produce the output you showed, provided there was already something in the file. Note that since the shell will not do anything to i
by itself, it needn't be quoted. This works: sed i"Hello \ World \"
and this also works sed "iHello \ World \"
(whether or not the -i
and -r
flags are included)â Zanna
Apr 24 at 9:12
add a comment |Â
1
that backslash is not very useful. If it's intended to escape the space when the string is used again for something, it needs to come before the space, like
hello world
â Zanna
Apr 22 at 7:05
1
Related: Search and replace a pattern containing backslashes
â Zanna
Apr 22 at 7:06