use sed to replace each white space with a backslash
just want to escape spaces in windows filepath. I'm trying thisecho "111 1111 "| sed -e "s/[[:space:]]/\ /g"
that only match spaces but do not replace.
command-line text-processing sed windows-subsystem-for-linux
add a comment |
just want to escape spaces in windows filepath. I'm trying thisecho "111 1111 "| sed -e "s/[[:space:]]/\ /g"
that only match spaces but do not replace.
command-line text-processing sed windows-subsystem-for-linux
I agree, the title is misleading ;-) But "ecran[sic] spaces in ... filepath" is pretty clear.echo "111 1111 "
has a fixed number of whitespace, so I don't really get your quesiton.
– RoVo
Dec 14 '18 at 14:53
add a comment |
just want to escape spaces in windows filepath. I'm trying thisecho "111 1111 "| sed -e "s/[[:space:]]/\ /g"
that only match spaces but do not replace.
command-line text-processing sed windows-subsystem-for-linux
just want to escape spaces in windows filepath. I'm trying thisecho "111 1111 "| sed -e "s/[[:space:]]/\ /g"
that only match spaces but do not replace.
command-line text-processing sed windows-subsystem-for-linux
command-line text-processing sed windows-subsystem-for-linux
edited Dec 15 '18 at 14:51
Yurij
asked Dec 14 '18 at 14:37
YurijYurij
9010
9010
I agree, the title is misleading ;-) But "ecran[sic] spaces in ... filepath" is pretty clear.echo "111 1111 "
has a fixed number of whitespace, so I don't really get your quesiton.
– RoVo
Dec 14 '18 at 14:53
add a comment |
I agree, the title is misleading ;-) But "ecran[sic] spaces in ... filepath" is pretty clear.echo "111 1111 "
has a fixed number of whitespace, so I don't really get your quesiton.
– RoVo
Dec 14 '18 at 14:53
I agree, the title is misleading ;-) But "ecran[sic] spaces in ... filepath" is pretty clear.
echo "111 1111 "
has a fixed number of whitespace, so I don't really get your quesiton.– RoVo
Dec 14 '18 at 14:53
I agree, the title is misleading ;-) But "ecran[sic] spaces in ... filepath" is pretty clear.
echo "111 1111 "
has a fixed number of whitespace, so I don't really get your quesiton.– RoVo
Dec 14 '18 at 14:53
add a comment |
2 Answers
2
active
oldest
votes
If you use double quotes, bash
interprets \
and outputs which is then again interpreted from
sed
together with the following space to just the space.
So you need one more backslash:
echo "111 1111 " | sed -e "s/[[:space:]]/\ /g"
but better to use single quotes to prevent the bash interpreting:
echo "111 1111 " | sed -e 's/[[:space:]]/\ /g'
Output:
111 1111
Alternative method:
If you have the file path as a variable, you can use Shell methods:
path="111 1111 "
echo ${path// /\ }
What would the out put be like?
– George Udosen
Dec 14 '18 at 14:47
+1 for Alternative method.
– WinEunuuchs2Unix
Dec 14 '18 at 23:57
nice method with variable, thanks. It is useful for my .bashrc
– Yurij
Dec 15 '18 at 14:53
add a comment |
[[:space:]]
doesn’t match just spaces but rather all whitespace characters including tabs and line breaks. If you really want that, GNU sed
(like in Ubuntu) has the shorthand class s
for it:
sed 's_s_\&_g'
This s
ubstitutes every (g
) whitespace character (s
, matches spaces, tabs and newlines embedded in the pattern/hold spaces) in every line with a backslash (\
) and itself – &
is the whole matched pattern. I use a different delimiter because slashes and backslashes always look confusing together; s/s/\&/g
is of course valid as well.
If you want to replace only space characters, rather use:
sed 's_ _\&_g'
Example run
$ echo "111 1111 " | sed 's_ _\&_g'
111 1111
For further reading on character classes see here on regular-expressions.info.
Thanks, it works too. an unusual form of regex. not so widely common?
– Yurij
Dec 15 '18 at 15:01
@Yurij I didn’t use any regex, do you mean&
for the entire matched pattern? That’s actually quite common…
– dessert
Dec 15 '18 at 15:13
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
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',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1100888%2fuse-sed-to-replace-each-white-space-with-a-backslash%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you use double quotes, bash
interprets \
and outputs which is then again interpreted from
sed
together with the following space to just the space.
So you need one more backslash:
echo "111 1111 " | sed -e "s/[[:space:]]/\ /g"
but better to use single quotes to prevent the bash interpreting:
echo "111 1111 " | sed -e 's/[[:space:]]/\ /g'
Output:
111 1111
Alternative method:
If you have the file path as a variable, you can use Shell methods:
path="111 1111 "
echo ${path// /\ }
What would the out put be like?
– George Udosen
Dec 14 '18 at 14:47
+1 for Alternative method.
– WinEunuuchs2Unix
Dec 14 '18 at 23:57
nice method with variable, thanks. It is useful for my .bashrc
– Yurij
Dec 15 '18 at 14:53
add a comment |
If you use double quotes, bash
interprets \
and outputs which is then again interpreted from
sed
together with the following space to just the space.
So you need one more backslash:
echo "111 1111 " | sed -e "s/[[:space:]]/\ /g"
but better to use single quotes to prevent the bash interpreting:
echo "111 1111 " | sed -e 's/[[:space:]]/\ /g'
Output:
111 1111
Alternative method:
If you have the file path as a variable, you can use Shell methods:
path="111 1111 "
echo ${path// /\ }
What would the out put be like?
– George Udosen
Dec 14 '18 at 14:47
+1 for Alternative method.
– WinEunuuchs2Unix
Dec 14 '18 at 23:57
nice method with variable, thanks. It is useful for my .bashrc
– Yurij
Dec 15 '18 at 14:53
add a comment |
If you use double quotes, bash
interprets \
and outputs which is then again interpreted from
sed
together with the following space to just the space.
So you need one more backslash:
echo "111 1111 " | sed -e "s/[[:space:]]/\ /g"
but better to use single quotes to prevent the bash interpreting:
echo "111 1111 " | sed -e 's/[[:space:]]/\ /g'
Output:
111 1111
Alternative method:
If you have the file path as a variable, you can use Shell methods:
path="111 1111 "
echo ${path// /\ }
If you use double quotes, bash
interprets \
and outputs which is then again interpreted from
sed
together with the following space to just the space.
So you need one more backslash:
echo "111 1111 " | sed -e "s/[[:space:]]/\ /g"
but better to use single quotes to prevent the bash interpreting:
echo "111 1111 " | sed -e 's/[[:space:]]/\ /g'
Output:
111 1111
Alternative method:
If you have the file path as a variable, you can use Shell methods:
path="111 1111 "
echo ${path// /\ }
edited Dec 14 '18 at 15:01
answered Dec 14 '18 at 14:44
RoVoRoVo
7,1211741
7,1211741
What would the out put be like?
– George Udosen
Dec 14 '18 at 14:47
+1 for Alternative method.
– WinEunuuchs2Unix
Dec 14 '18 at 23:57
nice method with variable, thanks. It is useful for my .bashrc
– Yurij
Dec 15 '18 at 14:53
add a comment |
What would the out put be like?
– George Udosen
Dec 14 '18 at 14:47
+1 for Alternative method.
– WinEunuuchs2Unix
Dec 14 '18 at 23:57
nice method with variable, thanks. It is useful for my .bashrc
– Yurij
Dec 15 '18 at 14:53
What would the out put be like?
– George Udosen
Dec 14 '18 at 14:47
What would the out put be like?
– George Udosen
Dec 14 '18 at 14:47
+1 for Alternative method.
– WinEunuuchs2Unix
Dec 14 '18 at 23:57
+1 for Alternative method.
– WinEunuuchs2Unix
Dec 14 '18 at 23:57
nice method with variable, thanks. It is useful for my .bashrc
– Yurij
Dec 15 '18 at 14:53
nice method with variable, thanks. It is useful for my .bashrc
– Yurij
Dec 15 '18 at 14:53
add a comment |
[[:space:]]
doesn’t match just spaces but rather all whitespace characters including tabs and line breaks. If you really want that, GNU sed
(like in Ubuntu) has the shorthand class s
for it:
sed 's_s_\&_g'
This s
ubstitutes every (g
) whitespace character (s
, matches spaces, tabs and newlines embedded in the pattern/hold spaces) in every line with a backslash (\
) and itself – &
is the whole matched pattern. I use a different delimiter because slashes and backslashes always look confusing together; s/s/\&/g
is of course valid as well.
If you want to replace only space characters, rather use:
sed 's_ _\&_g'
Example run
$ echo "111 1111 " | sed 's_ _\&_g'
111 1111
For further reading on character classes see here on regular-expressions.info.
Thanks, it works too. an unusual form of regex. not so widely common?
– Yurij
Dec 15 '18 at 15:01
@Yurij I didn’t use any regex, do you mean&
for the entire matched pattern? That’s actually quite common…
– dessert
Dec 15 '18 at 15:13
add a comment |
[[:space:]]
doesn’t match just spaces but rather all whitespace characters including tabs and line breaks. If you really want that, GNU sed
(like in Ubuntu) has the shorthand class s
for it:
sed 's_s_\&_g'
This s
ubstitutes every (g
) whitespace character (s
, matches spaces, tabs and newlines embedded in the pattern/hold spaces) in every line with a backslash (\
) and itself – &
is the whole matched pattern. I use a different delimiter because slashes and backslashes always look confusing together; s/s/\&/g
is of course valid as well.
If you want to replace only space characters, rather use:
sed 's_ _\&_g'
Example run
$ echo "111 1111 " | sed 's_ _\&_g'
111 1111
For further reading on character classes see here on regular-expressions.info.
Thanks, it works too. an unusual form of regex. not so widely common?
– Yurij
Dec 15 '18 at 15:01
@Yurij I didn’t use any regex, do you mean&
for the entire matched pattern? That’s actually quite common…
– dessert
Dec 15 '18 at 15:13
add a comment |
[[:space:]]
doesn’t match just spaces but rather all whitespace characters including tabs and line breaks. If you really want that, GNU sed
(like in Ubuntu) has the shorthand class s
for it:
sed 's_s_\&_g'
This s
ubstitutes every (g
) whitespace character (s
, matches spaces, tabs and newlines embedded in the pattern/hold spaces) in every line with a backslash (\
) and itself – &
is the whole matched pattern. I use a different delimiter because slashes and backslashes always look confusing together; s/s/\&/g
is of course valid as well.
If you want to replace only space characters, rather use:
sed 's_ _\&_g'
Example run
$ echo "111 1111 " | sed 's_ _\&_g'
111 1111
For further reading on character classes see here on regular-expressions.info.
[[:space:]]
doesn’t match just spaces but rather all whitespace characters including tabs and line breaks. If you really want that, GNU sed
(like in Ubuntu) has the shorthand class s
for it:
sed 's_s_\&_g'
This s
ubstitutes every (g
) whitespace character (s
, matches spaces, tabs and newlines embedded in the pattern/hold spaces) in every line with a backslash (\
) and itself – &
is the whole matched pattern. I use a different delimiter because slashes and backslashes always look confusing together; s/s/\&/g
is of course valid as well.
If you want to replace only space characters, rather use:
sed 's_ _\&_g'
Example run
$ echo "111 1111 " | sed 's_ _\&_g'
111 1111
For further reading on character classes see here on regular-expressions.info.
edited Dec 14 '18 at 23:01
answered Dec 14 '18 at 19:48
dessertdessert
22.4k56198
22.4k56198
Thanks, it works too. an unusual form of regex. not so widely common?
– Yurij
Dec 15 '18 at 15:01
@Yurij I didn’t use any regex, do you mean&
for the entire matched pattern? That’s actually quite common…
– dessert
Dec 15 '18 at 15:13
add a comment |
Thanks, it works too. an unusual form of regex. not so widely common?
– Yurij
Dec 15 '18 at 15:01
@Yurij I didn’t use any regex, do you mean&
for the entire matched pattern? That’s actually quite common…
– dessert
Dec 15 '18 at 15:13
Thanks, it works too. an unusual form of regex. not so widely common?
– Yurij
Dec 15 '18 at 15:01
Thanks, it works too. an unusual form of regex. not so widely common?
– Yurij
Dec 15 '18 at 15:01
@Yurij I didn’t use any regex, do you mean
&
for the entire matched pattern? That’s actually quite common…– dessert
Dec 15 '18 at 15:13
@Yurij I didn’t use any regex, do you mean
&
for the entire matched pattern? That’s actually quite common…– dessert
Dec 15 '18 at 15:13
add a comment |
Thanks for contributing an answer to Ask Ubuntu!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1100888%2fuse-sed-to-replace-each-white-space-with-a-backslash%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I agree, the title is misleading ;-) But "ecran[sic] spaces in ... filepath" is pretty clear.
echo "111 1111 "
has a fixed number of whitespace, so I don't really get your quesiton.– RoVo
Dec 14 '18 at 14:53