Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2003
    Posts
    3

    Unanswered: substitute "\" with "\\"

    Hi,

    I need to subsititue a "\" with "\\"

    I tried this:
    $ print -r $abc
    \some\var
    $ res=`print -r $abc | sed -e 's#\\#\\\\#g' `
    sed: Unterminated `s' command

    the backticks and ' are both balanced but it still gives the error. Any ideas?

    thanks,
    Rachana.

  2. #2
    Join Date
    Oct 2003
    Posts
    3

    Re: substitute "\" with "\\"

    Originally posted by rachana
    Hi,

    I need to subsititue a "\" with "\\"

    I tried this:
    $ print -r $abc
    \some\var
    $ res=`print -r $abc | sed -e 's#\\#\\\\#g' `
    sed: Unterminated `s' command

    the backticks and ' are both balanced but it still gives the error. Any ideas?

    thanks,
    Rachana.
    this works :

    # replace the "\" with "/"
    $res=`print -r $abc | tr '\\\\' /`
    ## thats 4 "\"'s in the tr but when i post it one is gone??
    # replace "/" with "\\"
    $res=`print -r $res | sed -e 's#\/#\\\\\\\\#g'`

    did someone figure out a more elegant solution?

    thanks,
    Rachana
    Last edited by rachana; 10-06-03 at 23:34.

  3. #3
    Join Date
    Dec 2002
    Posts
    104

    Re: substitute "\" with "\\"

    Originally posted by rachana
    this works :

    # replace the "\" with "/"
    $res=`print -r $abc | tr '\\\\' /`
    ## thats 4 "\"'s in the tr but when i post it one is gone??
    # replace "/" with "\\"
    $res=`print -r $res | sed -e 's#\/#\\\\\\\\#g'`

    did someone figure out a more elegant solution?

    thanks,
    Rachana
    hi!

    try this command...........

    sed -e 's/\\/\\\\/g' filename > newfilename

    hope this will work for u.

    --pooja

  4. #4
    Join Date
    Oct 2003
    Posts
    3

    Re: substitute "\" with "\\"

    Originally posted by pooja
    hi!

    try this command...........

    sed -e 's/\\/\\\\/g' filename > newfilename

    hope this will work for u.

    --pooja
    it doesnt:
    $abc='\some\var\'
    $ res=`print -r $abc | sed -e 's/\\/\\\\/'`
    sed: command garbled: s/\/\\/

    but I tried this and it works:

    $res=`print -r $abc | sed -e 's#\\\\#\\\\\\\\#g'`
    + + print -r \12\13\
    + sed -e s#\\#\\\\#g
    res=\\12\\13\\

    Needed to escape the backslash twice, once for the shell and then again for sed because i am taking the variable from the print stmt instead of a file.

    Thanks,
    Rachana.

  5. #5
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    12
    Thats correct. You need to do the escaping twice. Else you can put the string in a file and use -f with sed.
    Thanks and Regards
    Karthik R

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •