use sed to replace each white space with a backslash












0














just want to escape spaces in windows filepath. I'm trying this
echo "111 1111 "| sed -e "s/[[:space:]]/\ /g" that only match spaces but do not replace.










share|improve this question
























  • 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 at 14:53


















0














just want to escape spaces in windows filepath. I'm trying this
echo "111 1111 "| sed -e "s/[[:space:]]/\ /g" that only match spaces but do not replace.










share|improve this question
























  • 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 at 14:53
















0












0








0







just want to escape spaces in windows filepath. I'm trying this
echo "111 1111 "| sed -e "s/[[:space:]]/\ /g" that only match spaces but do not replace.










share|improve this question















just want to escape spaces in windows filepath. I'm trying this
echo "111 1111 "| sed -e "s/[[:space:]]/\ /g" that only match spaces but do not replace.







command-line text-processing sed windows-subsystem-for-linux






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 15 at 14:51

























asked Dec 14 at 14:37









Yurij

809




809












  • 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 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 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 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 at 14:53












2 Answers
2






active

oldest

votes


















3














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// /\ }





share|improve this answer























  • What would the out put be like?
    – George Udosen
    Dec 14 at 14:47










  • +1 for Alternative method.
    – WinEunuuchs2Unix
    Dec 14 at 23:57










  • nice method with variable, thanks. It is useful for my .bashrc
    – Yurij
    Dec 15 at 14:53



















1














[[: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 substitutes 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.






share|improve this answer























  • Thanks, it works too. an unusual form of regex. not so widely common?
    – Yurij
    Dec 15 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 at 15:13











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
});


}
});














draft saved

draft discarded


















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









3














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// /\ }





share|improve this answer























  • What would the out put be like?
    – George Udosen
    Dec 14 at 14:47










  • +1 for Alternative method.
    – WinEunuuchs2Unix
    Dec 14 at 23:57










  • nice method with variable, thanks. It is useful for my .bashrc
    – Yurij
    Dec 15 at 14:53
















3














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// /\ }





share|improve this answer























  • What would the out put be like?
    – George Udosen
    Dec 14 at 14:47










  • +1 for Alternative method.
    – WinEunuuchs2Unix
    Dec 14 at 23:57










  • nice method with variable, thanks. It is useful for my .bashrc
    – Yurij
    Dec 15 at 14:53














3












3








3






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// /\ }





share|improve this answer














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// /\ }






share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 14 at 15:01

























answered Dec 14 at 14:44









RoVo

6,8351740




6,8351740












  • What would the out put be like?
    – George Udosen
    Dec 14 at 14:47










  • +1 for Alternative method.
    – WinEunuuchs2Unix
    Dec 14 at 23:57










  • nice method with variable, thanks. It is useful for my .bashrc
    – Yurij
    Dec 15 at 14:53


















  • What would the out put be like?
    – George Udosen
    Dec 14 at 14:47










  • +1 for Alternative method.
    – WinEunuuchs2Unix
    Dec 14 at 23:57










  • nice method with variable, thanks. It is useful for my .bashrc
    – Yurij
    Dec 15 at 14:53
















What would the out put be like?
– George Udosen
Dec 14 at 14:47




What would the out put be like?
– George Udosen
Dec 14 at 14:47












+1 for Alternative method.
– WinEunuuchs2Unix
Dec 14 at 23:57




+1 for Alternative method.
– WinEunuuchs2Unix
Dec 14 at 23:57












nice method with variable, thanks. It is useful for my .bashrc
– Yurij
Dec 15 at 14:53




nice method with variable, thanks. It is useful for my .bashrc
– Yurij
Dec 15 at 14:53













1














[[: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 substitutes 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.






share|improve this answer























  • Thanks, it works too. an unusual form of regex. not so widely common?
    – Yurij
    Dec 15 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 at 15:13
















1














[[: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 substitutes 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.






share|improve this answer























  • Thanks, it works too. an unusual form of regex. not so widely common?
    – Yurij
    Dec 15 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 at 15:13














1












1








1






[[: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 substitutes 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.






share|improve this answer














[[: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 substitutes 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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 14 at 23:01

























answered Dec 14 at 19:48









dessert

21.9k55997




21.9k55997












  • Thanks, it works too. an unusual form of regex. not so widely common?
    – Yurij
    Dec 15 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 at 15:13


















  • Thanks, it works too. an unusual form of regex. not so widely common?
    – Yurij
    Dec 15 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 at 15:13
















Thanks, it works too. an unusual form of regex. not so widely common?
– Yurij
Dec 15 at 15:01




Thanks, it works too. an unusual form of regex. not so widely common?
– Yurij
Dec 15 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 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 at 15:13


















draft saved

draft discarded




















































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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


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




draft saved


draft discarded














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





















































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







Popular posts from this blog

How did Captain America manage to do this?

迪纳利

南乌拉尔铁路局