Error when using sed

Hi,
I have a input file with following contents

---------------------------------------------------------Run Number: 1-----------------------------------------------------------
test_run -layout test_vaal -i [ /x/TEST/batch/temp/20070528_ip.txt /x/TEST/batch/temp/20070528__op.txt]|Started|05/28/2007 02:19:30|TEST|8651
---------------------------------------------------------out_file-----------------------------------------------------------
item_id|status_desc|description
test_run -layout test_vaal -i [ /x/TEST/batch/temp/20070528_ip.txt /x/TEST/batch/temp/20070528__op.txt]|Completed|05/28/2007 02:19:33|TEST|8651
---------------------------------------------------------Run Number: 2-----------------------------------------------------------
test_run -layout test_vaal -i [ /x/TEST/batch/temp/20070528_ip.txt /x/TEST/batch/temp/20070528__op.txt]|Started|05/28/2007 02:27:25|TEST|24745
---------------------------------------------------------out_file-----------------------------------------------------------
item_id|status_desc|description
test_run -layout test_vaal -i [ /x/TEST/batch/temp/20070528_ip.txt /x/TEST/batch/temp/20070528__op.txt]|Completed|05/28/2007 02:27:27|TEST|24745
---------------------------------------------------------Run Number: 3-----------------------------------------------------------
test_run -layout test_vaal -i [ /x/TEST/batch/temp/20070528_ip.txt /x/TEST/batch/temp/20070528__op.txt]|Started|05/28/2007 02:56:32|TEST|5560
---------------------------------------------------------out_file-----------------------------------------------------------
item_id|status_desc|description
test_run -layout test_vaal -i [ /x/TEST/batch/temp/20070528_ip.txt /x/TEST/batch/temp/20070528__op.txt]|Completed|05/28/2007 02:56:34|TEST|5560
---------------------------------------------------------Run Number: 4-----------------------------------------------------------
test_run -layout test_vaal -i [ /x/TEST/batch/temp/20070528_ip.txt /x/TEST/batch/temp/20070528__op.txt]|Started|05/28/2007 02:57:32|TEST|11961
---------------------------------------------------------out_file-----------------------------------------------------------
item_id|status_desc|description
test_run -layout test_vaal -i [ /x/TEST/batch/temp/20070528_ip.txt /x/TEST/batch/temp/20070528__op.txt]|Completed|05/28/2007 02:57:33|TEST|11961
---------------------------------------------------------Run Number: 5-----------------------------------------------------------
test_run -layout test_vaal -i [ /x/TEST/batch/temp/20070528_ip.txt /x/TEST/batch/temp/20070528__op.txt]|Started|05/28/2007 04:17:31|TEST|24685
---------------------------------------------------------out_file-----------------------------------------------------------
item_id|status_desc|description
test_run -layout test_vaal -i [ /x/TEST/batch/temp/20070528_ip.txt /x/TEST/batch/temp/20070528__op.txt]|Completed|05/28/2007 04:17:33|TEST|24685

Input to the script will be

test_run -layout test_vaal -i [ /x/TEST/batch/temp/20070528_ip.txt /x/TEST/batch/temp/20070528__op.txt]

My job is to extract the last occurance of the expression that is from IP_EXPR|Started tp IP_EXPR|Completed

I tried with sed but it gives the following error
sed: -e expression #1, char 40: extra characters after command

Following is my code

IP="test_run -layout test_vaal -i [ /x/TEST/batch/temp/20070528_ip.txt /x/TEST/batch/temp/20070528__op.txt]"
IP_FILE="test.txt"

EXPR=`echo $IP | sed 's/\[//g' | sed 's/\]//g'`
echo "EXPR is : ${EXPR}"

EXTRACTED=`cat $IP_FILE | sed 's/\[//g' | sed 's/\]//g' |sed 's/ /!/g' | sed -n "/${EXPR}/,/${EXPR}/p" `

echo "EXTRACTED is : ${EXTRACTED}"

Can somebody help me out in this

Raghu

Your problem comes from the last sed command, because $EXPR contains the character /. The solution consists to use another character than / for the pattern delimiter, � for example.
The two following commands are equivalent.

$ sed 's/pattern/replacement/flags' file
$ sed 's\�pattern�replacement�flags' file

Your script with little modifications :

IP="test_run -layout test_vaal -i [ /x/TEST/batch/temp/20070528_ip.txt /x/TEST/batch/temp/20070528__op.txt]"
IP_FILE="test.txt"

EXPR=`echo $IP | sed 's/[][]//g;s/  */ /g'`
echo "EXPR is : ${EXPR}"

EXTRACTED=`sed 's/[][]//g;s/  */ /g' $IP_FILE | sed -n "\�${EXPR}|Started�,\�${EXPR}|Completed�p" `

echo "EXTRACTED is : ${EXTRACTED}"

The problem is that all occurences are displayed, not only the last one.

A possible solution (using awk)

IP="test_run -layout test_vaal -i [ /x/TEST/batch/temp/20070528_ip.txt /x/TEST/batch/temp/20070528__op.txt]"
IP_FILE="test.txt"

awk -v ip="$IP" '
   BEGIN {
      gsub(/[][]/, "", ip);
      gsub(/  */, " ", ip);
   }
   {
      gsub(/[][]/, "");
      gsub(/  */, " ");
      if ($0 !~ ip) next;
   }
   /|Started/,/|Completed/ {
      if (/|Started/) count=0;
      line[++count] = $0;
   }
   END {
      for (l=1; l<=count; l++) print line[l];
   }
     ' $IP_FILE

Output:

test_run -layout test_vaal -i /x/TEST/batch/temp/20070528_ip.txt /x/TEST/batch/temp/20070528__op.txt|Started|05/28/2007 04:17:31|TES
T|24685
test_run -layout test_vaal -i /x/TEST/batch/temp/20070528_ip.txt /x/TEST/batch/temp/20070528__op.txt|Completed|05/28/2007 04:17:33|T
EST|24685

Jean-Pierre.

HI,
Thanks a lot.
It started working
After posting it i found that the / was creating a problem.
Thanks a again for ur inputs

Raghuram