Use sed to replace all backslashes with forward slashes

BashShellSed

Bash Problem Overview


I want to be able to use sed to take an input such as:

C:\Windows\Folder\File.txt

to

C:/Windows/Folder/File.txt

Bash Solutions


Solution 1 - Bash

sed can perform text transformations on input stream from a file or from a pipeline. Example:

echo 'C:\foo\bar.xml' | sed 's/\\/\//g'

gets

C:/foo/bar.xml

Solution 2 - Bash

for just translating one char into another throughout a string, tr is the best tool:

tr '\\' '/'

Solution 3 - Bash

Just use:

sed 's.\\./.g'

There's no reason to use / as the separator in sed. But if you really wanted to:

sed 's/\\/\//g'

Solution 4 - Bash

If your text is in a Bash variable, then Parameter Substitution ${var//\\//} can replace substrings:

$ p='C:\foo\bar.xml'
$ printf '%s\n' "$p"
C:\foo\bar.xml
$ printf '%s\n' "${p//\\//}"
C:/foo/bar.xml

This may be leaner and clearer that filtering through a command such as tr or sed.

Solution 5 - Bash

$ echo "C:\Windows\Folder\File.txt" | sed -e 's/\\/\//g'
C:/Windows/Folder/File.txt

The sed command in this case is 's/OLD_TEXT/NEW_TEXT/g'.

The leading 's' just tells it to search for OLD_TEXT and replace it with NEW_TEXT.

The trailing 'g' just says to replace all occurrences on a given line, not just the first.

And of course you need to separate the 's', the 'g', the old, and the new from each other. This is where you must use forward slashes as separators.

For your case OLD_TEXT == '\' and NEW_TEXT == '/'. But you can't just go around typing slashes and expecting things to work as expected be taken literally while using them as separators at the same time. In general slashes are quite special and must be handled as such. They must be 'escaped' (i.e. preceded) by a backslash.

So for you, OLD_TEXT == '\\' and NEW_TEXT == '\/'. Putting these inside the 's/OLD_TEXT/NEW_TEXT/g' paradigm you get
's/\\/\//g'. That reads as
's / \\ / \/ / g' and after escapes is
's / \ / / / g' which will replace all backslashes with forward slashes.

Solution 6 - Bash

This might work for you:

sed 'y/\\/\//'

Solution 7 - Bash

For me, this replaces one backslash with a forward slash.

sed -e "s/\\\\/\//"  file.txt

Solution 8 - Bash

You can try

sed 's:\\:\/:g'`

The first \ is to insert an input, the second \ will be the one you want to substitute.

So it is 's ":" First Slash "" second slash "" ":" "" to insert input "/" as the new slash that will be presented ":" g'

\\ \/ 

And that's it. It will work.

Solution 9 - Bash

I had to use [\\] or [/] to be able to make this work, FYI.

>awk '!/[\\]/' file > temp && mv temp file

and
>awk '!/[/]/' file > temp && mv temp file

I was using awk to remove backlashes and forward slashes from a list.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEmilyView Question on Stackoverflow
Solution 1 - BashEmiliano PoggiView Answer on Stackoverflow
Solution 2 - Bashglenn jackmanView Answer on Stackoverflow
Solution 3 - Bashuser541686View Answer on Stackoverflow
Solution 4 - BashToby SpeightView Answer on Stackoverflow
Solution 5 - BashJimmyView Answer on Stackoverflow
Solution 6 - BashpotongView Answer on Stackoverflow
Solution 7 - BashtoddcscarView Answer on Stackoverflow
Solution 8 - BashAdilson MarcelinoView Answer on Stackoverflow
Solution 9 - BashtechnerdiusView Answer on Stackoverflow