| | |
Finding a Sub String
Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Aug 2007
Posts: 81
Reputation:
Solved Threads: 10
Hi !
I'm trying to find the substring in a shell script as given:
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
I'm getting the result as "OK".
Can you please suggest how can I use the variables instead of hard coding the parameters.
I'm trying to find the substring in a shell script as given:
Shell Scripting Syntax (Toggle Plain Text)
#! /bin/bash HTML="<html><body>OK</body></html>" OK="OK" OFFSET=`expr index $HTML $OK` OK_LEN=`expr length $OK` var1=`expr "$OFFSET"` echo $var1 var2=`expr "$OK_LEN"` echo $var2 INPUT=`echo | awk '{ print substr("'"$HTML"'",var2,var1) }'` 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)
INPUT=`echo | awk '{ print substr("'"$HTML"'",13,2) }'`
Can you please suggest how can I use the variables instead of hard coding the parameters.
•
•
Join Date: Aug 2007
Posts: 81
Reputation:
Solved Threads: 10
Ah!!
Got it!
I need to use quotes " ' " $var1" ' " with my variables
Got it!
I need to use quotes " ' " $var1" ' " with my variables
Shell Scripting Syntax (Toggle Plain Text)
INPUT=`echo | awk '{ print substr("'"$HTML"'","'"$var1"'","'"$var2"'") }'`
Or just:
or
Shell Scripting Syntax (Toggle Plain Text)
$ awk '{print substr($0,index($0,v),length(v))}' v="$OK"<<<$HTML OK
or
Shell Scripting Syntax (Toggle Plain Text)
$ awk '{match($0,v);print substr($0,RSTART,RLENGTH)}' v="$OK"<<<$HTML OK
•
•
Join Date: Apr 2006
Posts: 149
Reputation:
Solved Threads: 40
Shell Scripting Syntax (Toggle Plain Text)
# echo $HTML | sed 's/<html><body>\(.*\)<\/body><\/html>/\1/' OK
![]() |
Similar Threads
- very strange string behaviur.. (C)
- connection string of SqlConnection (C#)
- Search string in a text file (C)
- Comparing two letters in one string with another char string? (C)
- Reverse string arrays (C)
- string size problem (C)
- Finding and Replacing Strings (C++)
Other Threads in the Shell Scripting Forum
- Previous Thread: Begginer with Linux here nid help
- Next Thread: Multiple spaces condense in shell script variable?
| Thread Tools | Search this Thread |
Tag cloud for Shell Scripting





