ksh splitting string - html forms


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh splitting string - html forms
# 1  
Old 09-18-2004
ksh splitting string - html forms

I a new to using ksh for cgi..

I want to be able to split the query_string...

ie... id=893283923name=bob

How do I specifically extract the value of id in to a variable and the name etc?
# 2  
Old 09-20-2004
Try this:

$
$ x="id=893283923name=bob"
$ x=${x#id=}
$ echo $x
893283923name=bob
$ id=${x%name=*}
$ echo $id
893283923
$ x=${x#$id}
$ echo $x
name=bob
$

and repeat the procedure for any other fields.
# 3  
Old 09-21-2004
thanks for your reply... I found something very useful yesterday to use as a function as it sets the variables perfectly:

{
VFLAG="0"
[[ "_${1}" = "_-v" ]] && VFLAG="1"
CGI_TMP1=`sed -e "s/\&/ /g;s/%\(..\)/\\\`print \\\'ibase=16\\\; \1\\\' \\\| bc \\\| awk \\\'\\\{printf\\\(\\\\\"%c\\\\\",\\\$1\\\)\\
\}\\\'\\\`/g"`

if [[ "_${CGI_TMP1}" != "_" ]]
then
for i in `eval echo "\"${CGI_TMP1}\""`
do
CGI_TMP2=`print "${i}" | sed -e "s/\"/\'/g;s/,/ /g;s/+/ /g"`
CGI_VAR="${CGI_TMP2%%=*}"
CGI_VAL="${CGI_TMP2#*=}"
eval ${CGI_VAR}="\"\${CGI_VAL}\""
[[ ${VFLAG} -eq 1 ]] && print "${CGI_VAR}=\"${CGI_VAL}\""
done
fi
}
# 4  
Old 09-21-2004
frustrated1,

You may want to look into using perl for cgi parsing and scripting. PERL is a great language for parsing text and that way you won't have to call sed/awk from within your ksh script. Just a thought.
# 5  
Old 09-21-2004
Thanks - I am looking at learning perl now...

as you say - it seems to be the way forward for parsing cgi forms etc... thanks!
# 6  
Old 09-22-2004
hi,

perhaps you can use the sed command to separate your string :

with ... |sed 's/name/ name/g'

and with an awk after ?
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question