0

I need to construct SED command that contains "/" character. sed is not consedering this command as valid:

adding 'john/street'

String cmdtest[] = {"sed","-3 a" + "\\","> john\\/street","/scratch/tmp.txt"};

also I need to know how can I go for replacing a string that has "/" included.

this is what I need to execute

cmdtest[] = {"sed","-i","s/ee.jar:/ee.jar:$MY_HOME\\\/lib\\\/jcagent.jar","/scratch/myreg.sh"};

Ouptput should replace {ee.jar} with {ee.jar:$MY_HOME/lib/jcagent.jar} in file myreg.sh

Barranka
  • 20,547
  • 13
  • 65
  • 83
  • 1
    You probably need to use `\\\/` to escape the slash from java first so that the shell sees `\/` and not just `/` there. That being said you can use alternate delimiters to sed to avoid this problem entirely (i.e. `s,foo,bar,` etc.). – Etan Reisner Aug 28 '15 at 15:04
  • 3
    You can use 'waste' characters like '#' instead of '/' in a sed command. You have to replace all '/' characters that would normally format the command with '#' – jim mcnamara Aug 28 '15 at 15:04
  • Please add sample input and your desired output for that sample input to your question. – Cyrus Aug 28 '15 at 15:12
  • added please see, also it is \\\ which is shown as single \ in question – user3356868 Aug 28 '15 at 15:23
  • You will find an example of the Waste from @Jim at [another problem with slashes in sed]( http://stackoverflow.com/questions/22182050/sed-e-expression-1-char-23-unknown-option-to-s) – Walter A Aug 28 '15 at 18:58

3 Answers3

1

you can use any other char as delimiter instead of / in sed, and then you can put / in the string without any escaping, for example this is valid sed command: sed -e 's#something/#something#' that will replace 'something/' with 'something'

Nir Levy
  • 12,750
  • 3
  • 21
  • 38
0

escaping the '/' char can be done by using '/'.

example :

assuming 1.txt contains '123'

$ sed -e 's/1/x/g' 1.txt
// returns 'x23'

$ sed -e 's/1/\\/g' 1.txt
// returns '/23'
chenchuk
  • 5,324
  • 4
  • 34
  • 41
0

Answer is:

use \ \ for every / in the string like a correct answer given by chenchuk:

$ sed -e 's/1/x/g' 1.txt
// returns 'x23'

$ sed -e 's/1/\\/g' 1.txt
// returns '/23'

thanks