Sponsored Content
Top Forums Shell Programming and Scripting help for shell script of finding shortest substring from given string by user Post 302146759 by pankajd on Thursday 22nd of November 2007 06:31:13 AM
Old 11-22-2007
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
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Substring in C shell script?

i am a new user of C-shell script. I want to know can i create a substring in a string. That means when i got a variable $input = "it is number 2" I want to get the "2" to be another variable. Can i do that in C-shell and how to ? Thank you so much dinodash (0 Replies)
Discussion started by: dinodash
0 Replies

2. Shell Programming and Scripting

command/script to extract a substring from a string

I have a long string "<ID type="Oid">{}</ID>" I need to extract "GigabitEthernet0/1" from the above string. How can it be done? :) (5 Replies)
Discussion started by: girisha
5 Replies

3. UNIX for Dummies Questions & Answers

Substring in Shell Script

Hi I'm new to Shell scripting. Someone please help me in extracting a portion of string from a file. Eg: I got a file like, Readme.txt and has the following name value pairs input1 : /homes/input1/ input2 : /homes/input2/ ... ... When I give the parameter input1, the value... (3 Replies)
Discussion started by: smartbuddy
3 Replies

4. Shell Programming and Scripting

Using Awk in shell script to extract an index of a substring from a parent string

Hi All, I am new to this shell scripting world. Struck up with a problem, can anyone of you please pull me out of this. Requirement : Need to get the index of a substring from a parent string Eg : index("Sandy","dy") should return 4 or 3. My Approach : I used Awk function index to... (2 Replies)
Discussion started by: sandeepms17
2 Replies

5. Shell Programming and Scripting

need help in finding a string and to send an email using shell script

Hi All i am writing a shell script which will search for a string "expires". once the search string is found it has to give the email address as the output and send an email to the person This is basically to find the encrypetd keys which are loaded in the unix server Below are sample... (10 Replies)
Discussion started by: ranga27
10 Replies

6. Shell Programming and Scripting

shell script for extracting out the shortest substring from the given starting and en

hi all, i need an urgent help for writing a shell script which will extract out and print a substring which is the shortest substring from the given string where first and last character of that substring will be given by the user. for e.g. if str="abcdpqracdpqaserd" now if the user gives 'a'... (18 Replies)
Discussion started by: pankajd
18 Replies

7. Shell Programming and Scripting

using substring in shell script

This is the data I am having in a file Just for sample I have given 3 records. The file which I am having consists of n number of records. ABC123 10 01/02/2008 2008-01-03-00.00.00.000000 DYUU 22 02/03/2008 2008-01-04-00.00.00.000000 RF33 88 03/05/2008 2008-01-05-00.00.00.000000 ... (24 Replies)
Discussion started by: kmanivan82
24 Replies

8. Shell Programming and Scripting

How to do String manipulations using Substring function in Shell

Hi, I have a scenario to just plug out the file name from the following location path. /opt/project/data/int/holdFiles/csv195687.csv So, how do I get just file name which is "csv195687.csv" from the above line using awk/shell scripting? Can we use indexOf and Substring in awk to get... (7 Replies)
Discussion started by: anilvvnn
7 Replies

9. Shell Programming and Scripting

how to find the shortest line which containing a key string?

hi all, suppose a key string: M0271857 and to find all lines containing this key string in a text file which returns multiple lines but i only want the shortest one is there a way to do that? thanks so much! (4 Replies)
Discussion started by: sunnydanniel
4 Replies
strfind(3GEN)					     String Pattern-Matching Library Functions					     strfind(3GEN)

NAME
strfind, strrspn, strtrns, str - string manipulations SYNOPSIS
cc [ flag ... ] file ... -lgen [ library ... ] #include <libgen.h> int strfind(const char *as1, const char *as2); char *strrspn(const char *string, const char *tc); char * strtrns(const char *string, const char *old, const char *new, char *result); DESCRIPTION
The strfind() function returns the offset of the first occurrence of the second string, as2, if it is a substring of string as1. If the second string is not a substring of the first string strfind() returns -1. The strrspn() function trims chartacters from a string. It searches from the end of string for the first character that is not contained in tc. If such a character is found, strrspn() returns a pointer to the next character; otherwise, it returns a pointer to string. The strtrns() function transforms string and copies it into result. Any character that appears in old is replaced with the character in the same position in new. The new result is returned. USAGE
When compiling multithreaded applications, the _REENTRANT flag must be defined on the compile line. This flag should only be used in mul- tithreaded applications. EXAMPLES
Example 1: An example of the strfind() function. /* find offset to substring "hello" within as1 */ i = strfind(as1, "hello"); /* trim junk from end of string */ s2 = strrspn(s1, "*?#$%"); *s2 = ''; /* transform lower case to upper case */ a1[] = "abcdefghijklmnopqrstuvwxyz"; a2[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; s2 = strtrns(s1, a1, a2, s2); ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |MT-Level |MT-Safe | +-----------------------------+-----------------------------+ SEE ALSO
string(3C), attributes(5) SunOS 5.10 20 Jan 1999 strfind(3GEN)
All times are GMT -4. The time now is 01:10 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy