Solved

# Sed Command

Posted on 2005-03-03
Medium Priority
736 Views
Hi All,

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

0
Question by:ee_lcpaa
[X]
###### 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
• 2
• 2
• 2
• +1

LVL 45

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

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