#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0

    Question Pattern Matching And Substitution with sed


    I have a string like /abc/def/ghi/.. and i dont know how many backslashes are there in a string provided by the user. I want to escape all the backslashes with a forward slash using sed . I am not able to accomplish this task.

    I am trying to write it like this sed -i -e 's/://\:/'. I am confused where i am getting wrong or comitting a mistake.
    Help me out.

    Regards
    Codecatcher89
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    If you want to apply the change to all matches on a line, use the 'g' suffix (for global).

    Eg
    sed -i -e 's/://\:/g'
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by salem
    If you want to apply the change to all matches on a line, use the 'g' suffix (for global).

    Eg
    sed -i -e 's/://\:/g'
    But i am not getting the desired result with this sed -i -e 's/://\:/g'

    i am incorporating it in a c shell script and it gives an error " sed: -e expression #1, char 6: unknown option to `s' "

    Regards
    codecatcher89
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    What are all those : for?

    > I want to escape all the backslashes with a forward slash using sed .
    Try
    sed 's/\\/\//g'


    If you're still stuck, I suggest posting an actual example of your input text, and a manual edit of what you want it to look like. Your original post is very unclear.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by salem
    What are all those : for?

    > I want to escape all the backslashes with a forward slash using sed .
    Try
    sed 's/\\/\//g'


    If you're still stuck, I suggest posting an actual example of your input text, and a manual edit of what you want it to look like. Your original post is very unclear.
    My orignal string : /home/prashant/dev/tools.abc/df2
    and i want this to be like this : \/home\/prashant\/dev\/tools.abc\/df2
  10. #6
  11. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    Right

    $ sed 's/\//\\\//g'
    /foo/bar/baz
    \/foo\/bar\/baz


    Or since sed allows you to pick any character as the field separator,
    $ sed 's@/@\\/@g'
    /foo/bar/baz
    \/foo\/bar\/baz
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    spaceBAR Central
    Posts
    229
    Rep Power
    42
    This will match you specified string:
    Code:
    $ echo '/home/prashant/dev/tools.abc/df2' | sed '#\/[a-z]+\/[a-z]+\/[a-z]+\/[a-z]+\.[a-z]+\/[a-z]+#'
    /home/prashant/dev/tools.abc/df2
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0
    I have one more doubt

    set Root_name="sed -i -e 's/\//\\\//g'" abc.txt in a c shell script
    but it gives an error " set: Variable name must begin with a letter. "

    where i am comitting mistake.

    Regards
    codecatcher89
  16. #9
  17. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    The first mistake is using csh

    The second mistake is passing -i to sed, which means you're doing an in-place edit of the input text.

    The third mistake is it seems you want to collect the output of a command and store it in a variable.
    The syntax of which is (in broken csh), something like
    variable=`pwd`
    echo $variable

    Note carefully the direction of the `` quotes around pwd, it's not your regular ''.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo