The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 06: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 05:54 PM
Substring in shell script jyotib Shell Programming and Scripting 5 01-16-2008 07: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 07:44 AM
command/script to extract a substring from a string girisha Shell Programming and Scripting 5 09-21-2006 12:30 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-22-2007
pankajd pankajd is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 18
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
  #2 (permalink)  
Old 11-22-2007
grial's Avatar
grial grial is offline Forum Advisor  
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.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 03:42 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0