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
Join Stack Overflow to learn, share knowledge, and build your career.
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
. and then write without double escapement s.\\./.g
– Sandburg
Jan 30 '19 at 13:40
C: \n oar.xml. tr does not either..
– Timo
Jan 26 at 18:34
for just translating one char into another throughout a string, tr is the best tool:
tr '\\' '/'
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
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'
$ 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
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.
$ 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.
For me, this replaces one backslash with a forward slash.
sed -e "s/\\\\/\//" file.txt
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.
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.