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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
LVL 45

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.

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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 45

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
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…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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
Course of the Month15 days, 8 hours left to enroll

743 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