SED complex string replacement

sed -i 's:"ps -ef | grep $(cat $PID_FILE) | grep -v grep":"ps -C java -o pid,cmd | grep ${SERVER_NAME} | cut -d' ' -f1 | grep -v grep":g' scriptName

That's what I'm attempting to do. I'm attempting to replace this:

ps -ef | grep $(cat $PID_FILE) | grep -v grep

with this:

ps -C java -o pid,cmd | grep ${SERVER_NAME} | cut -d' ' -f1 | grep -v grep

Thanks for the help

This is the escape anything vaguely risky and change the outer speech marks to different ones to those in the intended string method:

sed -i "s/ps \-ef \| grep \$\(cat \$PID_FILE\) \| grep \-v grep/ps \-C java \-o pid\,cmd \| grep \$\{SERVER_NAME\} \| cut \-d\' \' \-f1 \| grep \-v grep/" scriptName

brutal but is works as long as you drop the g for global replace, because surely the script does not have:

[code]
ps -ef | grep $(cat $PID_FILE) | grep -v grep
[/code
twice in one line?
If not the /g at the end of the sed statement is not needed .

[quote=tonyfullermalv;302333647]
This is the escape anything vaguely risky and change the outer speech marks to different ones to those in the intended string method:

sed -i "s/ps \-ef \| grep \$\(cat \$PID_FILE\) \| grep \-v grep/ps \-C java \-o pid\,cmd \| grep \$\{SERVER_NAME\} \| cut \-d\' \' \-f1 \| grep \-v grep/" scriptName

brutal but is works as long as you drop the g for global replace, because surely the script does not have:

ps -ef | grep $(cat $PID_FILE) | grep -v grep
[/code
twice in one line?
If not the /g at the end of the sed statement is not needed .

ps -C java -o pid,cmd | grep ${SERVER_NAME} | cut -d' ' -f1 | grep -v grep| grep $(cat $PID_FILE) | grep -v grep

That was my result. It semi-worked. The line should end at the first 'grep -v grep'

cut -d' ' -f1 | grep -v grep---------This is where the replacement command ends, and the remainder of this shouldn't be in here-----------------| grep $(cat $PID_FILE)

Here is the actual code it's in:

before replacement:

if test -f $PID_FILE ; then
  if (ps -ef | grep $(cat $PID_FILE) | grep -v grep) ; then
      echo "\nProcess already running, exiting start script."
      exit 1
  fi
fi

After running the sed command:

if test -f $PID_FILE ; then
  if (ps -C java -o pid,cmd | grep ${SERVER_NAME} | cut -d' ' -f1 | grep -v grep| grep $(cat $PID_FILE) | grep -v grep) ; then
      echo "\nProcess already running, exiting start script."
      exit 1
  fi

Still haven't got this if anyone has any ideas.

why do you want to do this? can't you just use the use this code? if that command varies you'll likely encounter more problems

ps -C java -o pid,cmd | grep ${SERVER_NAME} | cut -d' ' -f1 | grep -v grep| grep $(cat $PID_FILE) | grep -v grep

try

sed "s/ps -ef | grep \$(cat \$PID_FILE) | grep -v grep/ps -C java -o pid,cmd | grep \$\{SERVER_NAME\} | cut -d' ' -f1 | grep -v grep/" file
perl -ne 's/ps -ef \| grep \$\(cat \$PID_FILE\) \| grep -v grep/ps -C java -o pid,cmd \| grep \${SERVER_NAME} \| cut -d -f1 \| grep -v grep/;' -ne "s/-d/-d' '/; print" <file_name>

tyler_durden