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

x
?
Solved

Sed Command

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

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

Please advise.

0
Comment
Question by:ee_lcpaa
  • 2
  • 2
  • 2
  • +1
7 Comments
 
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


Kent
0
 
LVL 48

Expert Comment

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

Author Comment

by:ee_lcpaa
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

FILEONE=/tmp/pwd.txt  

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.








0
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

by:Tintin
ID: 13455627
What Unix flavour are you running?
0
 

Author Comment

by:ee_lcpaa
ID: 13456039
Hi Tintin,

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

Accepted Solution

by:
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.
Kent
0
 
LVL 51

Expert Comment

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

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