![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Rules & FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| shell script for extracting out the shortest substring from the given starting and en | pankajd | Shell Programming and Scripting | 18 | 03-10-2008 02:20 AM |
| need help in finding a string and to send an email using shell script | ranga27 | Shell Programming and Scripting | 10 | 02-19-2008 01:54 PM |
| Substring in shell script | jyotib | Shell Programming and Scripting | 5 | 01-16-2008 03:58 PM |
| Using Awk in shell script to extract an index of a substring from a parent string | sandeepms17 | Shell Programming and Scripting | 2 | 11-07-2007 03:44 AM |
| command/script to extract a substring from a string | girisha | Shell Programming and Scripting | 5 | 09-21-2006 09:30 AM |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
help for shell script of finding shortest substring from given string by user
please give me proper solution for finding a shortest substring from given string if string itself and first char and last char of that substr are also given by user
if S="dpoaoqooroo" and FC="o" and LC="o",then shortest substr is "oo" and rest of the string is "dpoaoqroo" i have code but it is not working properly....so if u can give some new code or modify below code.. echo "enter the string" read str echo -e "\nenter first char of the substring" read fc echo -e "\nenter last char of the substring" read lc len=${#str} no=`echo "$str" |awk -F "$lc" '{print NF}'` no1=`echo "$str" |awk -F "$fc" '{print NF}'` fci=`expr index "$str" $fc` lci=`expr index "$str" $lc` if {(test $no -eq 1 || test $no1 -eq 1)} then echo "substring not found 1" exit 0 fi flag=0 olen=0 ip2="" for ((i=1;i<=$(($no-1));i++)) do ip1=`echo "$str" |cut -d "$lc" -f$i` echo "ip1 for $i times is $ip1" len1=${#ip1} ip2=${ip1##**$fc} echo "ip2 for $i times is $ip2" len2=${#ip2} fcin=`expr index "$ip1" $fc` if {(test $flag -eq 0 && test $len1 -ne 0 && test $fcin -ne 0)} then oip2=$ip2 olen=$len2 flag=1 key=1 else if {(test "$olen" -gt "$len2" && test "$len1" -ne 0 && test "$fcin" -ne 0)} then oip2=$ip2 olen=$len2 key=1 fi fi done if (test "$key" = 1 ) then echo "last shortest string is \"$fc$oip2$lc\"" str2=`echo ${str/$fc$oip2$lc/""}` echo "the rest of the string is \"$str2\"" else echo "substring not found last" fi |
| Forum Sponsor | ||
|
|