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
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
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
for just translating one char into another throughout a string, tr
is the best tool:
tr '\\' '/'
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 '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.
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
.
This might work for you:
sed 'y/\\/\//'
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.