The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #2 (permalink)  
Old 05-29-2007
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,433
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.


Code:
$ sed 's/pattern/replacement/flags' file
$ sed 's\§pattern§replacement§flags' file

Your script with little modifications :

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;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)

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"

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:

Code:
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.