Finding a Sub String

Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Aug 2007
Posts: 81
Reputation: lookof2day is an unknown quantity at this point 
Solved Threads: 10
lookof2day lookof2day is offline Offline
Junior Poster in Training

Finding a Sub String

 
0
  #1
Jan 15th, 2008
Hi !
I'm trying to find the substring in a shell script as given:
Shell Scripting Syntax (Toggle Plain Text)
  1. #! /bin/bash
  2. HTML="<html><body>OK</body></html>"
  3. OK="OK"
  4. OFFSET=`expr index $HTML $OK`
  5. OK_LEN=`expr length $OK`
  6.  
  7. var1=`expr "$OFFSET"`
  8. echo $var1
  9. var2=`expr "$OK_LEN"`
  10. echo $var2
  11. INPUT=`echo | awk '{ print substr("'"$HTML"'",var2,var1) }'`
  12.  
  13. echo $INPUT

But on running it does not give me the output. The output I expect is "OK".
If I hardcode values of var1 and var2 ie if I write the expression as
Shell Scripting Syntax (Toggle Plain Text)
  1. INPUT=`echo | awk '{ print substr("'"$HTML"'",13,2) }'`
I'm getting the result as "OK".
Can you please suggest how can I use the variables instead of hard coding the parameters.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 81
Reputation: lookof2day is an unknown quantity at this point 
Solved Threads: 10
lookof2day lookof2day is offline Offline
Junior Poster in Training

Re: Finding a Sub String

 
0
  #2
Jan 15th, 2008
Ah!!

Got it!
I need to use quotes " ' " $var1" ' " with my variables
Shell Scripting Syntax (Toggle Plain Text)
  1. INPUT=`echo | awk '{ print substr("'"$HTML"'","'"$var1"'","'"$var2"'") }'`
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 54
Reputation: radoulov is an unknown quantity at this point 
Solved Threads: 5
radoulov's Avatar
radoulov radoulov is offline Offline
Junior Poster in Training

Re: Finding a Sub String

 
0
  #3
Jan 15th, 2008
Or just:

Shell Scripting Syntax (Toggle Plain Text)
  1. $ awk '{print substr($0,index($0,v),length(v))}' v="$OK"<<<$HTML
  2. OK

or
Shell Scripting Syntax (Toggle Plain Text)
  1. $ awk '{match($0,v);print substr($0,RSTART,RLENGTH)}' v="$OK"<<<$HTML
  2. OK
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 149
Reputation: ghostdog74 is on a distinguished road 
Solved Threads: 40
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: Finding a Sub String

 
0
  #4
Jan 15th, 2008
Shell Scripting Syntax (Toggle Plain Text)
  1. # echo $HTML | sed 's/<html><body>\(.*\)<\/body><\/html>/\1/'
  2. OK
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Shell Scripting
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC