Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Sed Command

Posted on 2005-03-03
Medium Priority
Last Modified: 2010-04-21
Hi All,

How to replace a "\" character to "\\" characters with the sed command?

Please advise.

Question by:ee_lcpaa
  • 2
  • 2
  • 2
  • +1
LVL 46

Expert Comment

by:Kent Olsen
ID: 13448569

I assume that you want to run sed from the shell, possibly a script, without the editor actually opening in a "window".

Try this:

sed -e 's/\\/\\\\/g' < SourceFile > DestinationFile

LVL 48

Expert Comment

ID: 13452888
$ echo "\foo\bar" | sed 's!\\!\\\\!g'

Author Comment

ID: 13455544
Dear Kent & Tintin,

I tried the two methods proposed by you, but I go the following error:

sed function 's/\\/\\\\/g' cannot be parsed or
sed function 's!\\!\\\\!g' cannot be parsed

Below lists my script


ACCTINFO=`cat $FILEONE | sed 's/\\/\\\\/g'`

The content of FILEONE is mydomain\myaccount mypassword

I hope ACCTINFO will be euqal to "mydomain\\myaccount my paasword" after calling the sed function.

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

LVL 48

Expert Comment

ID: 13455627
What Unix flavour are you running?

Author Comment

ID: 13456039
Hi Tintin,

I am using HP Tru64 Unix Version 5.1B.
LVL 46

Accepted Solution

Kent Olsen earned 500 total points
ID: 13463780
Hi ee_lcpaa,

This is going to look pretty weird, but try this:

ACCTINFO=`cat $FILEONE | sed 's/\\\\/\\\\\\\\/g'`

The backslashes may be being interpretted by both the shell and sed.  If so when the shell gets done with:

> ACCTINFO=`cat $FILEONE | sed 's/\\/\\\\/g'`

what gets passed to sed is:

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

The first slashed is now escaped because of the backslash, so a parse error occurs.

You can also turn on statement trace with stty to see what the sed statement looks like.
LVL 51

Expert Comment

ID: 13474335
ACCTINFO="`sed 's/\\/\\\\/g' $FILEONE`"

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question