943,614 Members | Top Members by Rank

Ad:
Jan 15th, 2008
0

Finding a Sub String

Expand Post »
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.
Similar Threads
Reputation Points: 16
Solved Threads: 11
Junior Poster in Training
lookof2day is offline Offline
83 posts
since Aug 2007
Jan 15th, 2008
0

Re: Finding a Sub String

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"'") }'`
Reputation Points: 16
Solved Threads: 11
Junior Poster in Training
lookof2day is offline Offline
83 posts
since Aug 2007
Jan 15th, 2008
0

Re: Finding a Sub String

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
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
radoulov is offline Offline
54 posts
since Feb 2007
Jan 15th, 2008
0

Re: Finding a Sub String

Shell Scripting Syntax (Toggle Plain Text)
  1. # echo $HTML | sed 's/<html><body>\(.*\)<\/body><\/html>/\1/'
  2. OK
Reputation Points: 75
Solved Threads: 44
Junior Poster
ghostdog74 is offline Offline
156 posts
since Apr 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: Begginer with Linux here nid help
Next Thread in Shell Scripting Forum Timeline: Multiple spaces condense in shell script variable?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC