SlackwareThis Forum is for the discussion of Slackware Linux.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Could someone provide me with the right command needed in order for sed to replace slash with backslash?
The default syntax "sed s/replace_this//with_this/g will work fine with letters but not with special charechters such as \ or /.
poison has the key - however, the kicker is that this is very awkward to do straight from the command line because the shell will want to get in and parse the escape sequences in ways and times that will get in the way. However, if you store the sed commands in a file, it is quite simple.
For example, create a 1 line file namede "front2back.sed" containing
Code:
s/\//\\/g
then you can invoke sed like so:
Code:
sed -f front2back.sed < file2change.txt > newfile.txt
I know, this is way too late but hopefully will help someone (like myself) who wants the command-line version to work instead.
I needed to double-backslash the contents of a file (SQL Insert statements, if you're wondering) in a command pipeline, looked here, kept on fiddling, and found that the simple solution is to use single rather than double quotes, which stops the shell from interpreting the contents of the sed command.
echo some-file.sql | sed -e 's/\\/\\\\/g' >some-other-file.sql
I know you asked specifically about sed, but with Unix, it's often better to ask "how to do x" rather than "how to do x using y", unless you specifically need to use 'y' that is. Quite often there are better alternatives.
For a simple replacement such as you describe 'tr' might suit you better and would possibly be more efficient to run.
Code:
tr '\' '/' <input.file >output.file
If you need to use sed because you're doing something more complex than that, then a little known tip is that you can actually use other delimiters to make it more readable. Though you still need to escape the '\' to stop sed misinterpreting it, you won't need to escape the '/'
I'm sure you'll agree that
Code:
sed -e 's:\\:/:g' input.file
is much clearer than
Code:
sed -e 's/\\/\//g' input.file
You've already had some answers so you've probably sorted this out, but I thought I'd share this anyway. Anyone who ever has to look at your code in future would thank you for using either of these techniques.
edit: oops, just noticed this was a revived thread. That'll teach me to look at the dates more closely. Ahh well, Maybe someone will find it useful if they stumble upon it.
I should tell that have found the tip on custom delimiters useful That is when dealing with file path expressions in sed. For instance to dynamically remove an iSCSI LUN from ietd.conf one could simple put the following or kinda:
Code:
su -c "cat /etc/ietd.conf | sed -e \"s:Lun $LUN Path=$LUN_DEV,Type=blockio::g\" > /tmp/ietd.conf"
su -c "cp /tmp/ietd.conf /etc"
Without the custom delimiter the path expression '$LUN_DEV' would generate a sed error message...
is there a possibility to use a plugin to change file paths in mac into pc format? this means backslash has to change into fslash. I don't want to do it manually because it's about lot's of files veryday (work related).
Mainly i want to select a path (in file info) and copy it in a mail or a document with chanched slashes. Nobody finds a way.
have you actually tried sending windows users the paths with forward slashes? Thing is, forward slashes work in windows the same way, as backslashes do. Try it.
i want to escape the dots of an url so i wrote the following code:
Code:
url="google.de"
echo $url # output: google.de
escapedurl="`echo $url | sed 's:\.:\\\.:g'`"
echo $escapedurl # output: google\.de
# content of /var/www/template :
# {url}
# {escapedurl}
sed "s:{url}:$url:g" /var/www/template | sed "s:{escapedurl}:$escapedurl:g"
# output:
# google.de
# google.de
the last output should be the escaped url but it doesn't work. the backslash is ignored. i guess it's a problem with the quotation marks. i also tried single quotes but then the variable is not interpreted.
so can anybody tell me how to fix this?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.