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
Medium Priority
743 Views
Hi All,

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

0
Question by:ee_lcpaa
• 2
• 2
• 2
• +1

LVL 46

Expert Comment

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

ID: 13452888
$echo "\foo\bar" | sed 's!\\!\\\\!g' \\foo\\bar 0 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 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

LVL 48

Expert Comment

ID: 13455627
What Unix flavour are you running?
0

Author Comment

ID: 13456039
Hi Tintin,

I am using HP Tru64 Unix Version 5.1B.
0

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.
Kent
0

LVL 51

Expert Comment

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

## Featured Post

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
Course of the Month13 days, 7 hours left to enroll