51

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
69

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
3
  • 1
    \\ - first '\' escapes the seconds one in order to be used as character and not as directive. – AlikElzin-kilaka Jul 14 '16 at 7:52
  • 1
    you can give sed another separator, for example . and then write without double escapement s.\\./.g – Sandburg Jan 30 '19 at 13:40
  • I need it for wsl2, I use debian in wsl, but here it does not work, result is C: \n oar.xml. tr does not either.. – Timo Jan 26 at 18:34
62

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

tr '\\' '/'
2
  • 3
    BTW, "tr" is much simpler binary than "sed" and faster. – Michał Šrajer Jul 28 '11 at 21:44
  • 2
    tr is not the best tool. String substitution is. While tr is simpler than sed, it is still much more work than using a shell builtin. – Mad Physicist Apr 7 '16 at 17:04
26

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'
1
  • 3
    That's neat, I guess you don't need to escape the forward slash in the first case, because the separator is '.' But you do for the second case. Even the following works! $ echo "C:\Windows\Folder\File.txt" | sed -e 'sf\\f/fg' C:/Windows/Folder/File.txt Does sed just take the first character after the 's' and further occurances of that character must be escaped? – Jimmy Jul 28 '11 at 1:05
12

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.

0
9
$ 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.

2

This might work for you:

sed 'y/\\/\//'
1

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

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

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.

-1

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.