The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
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

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-22-2007
Registered User
 

Join Date: Oct 2007
Posts: 11
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
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 11-22-2007
grial's Avatar
El UNIX es como un toro
 

Join Date: Jun 2006
Location: Madrid (Spain)
Posts: 531
The reading of the code is very hard. Please, use the "CODE" tags.
Regards.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:49 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0