Kshell: length of a string and substring

Kshell: length of a string and substring

Post by zol » Fri, 25 Jan 2002 01:13:30



Hi there
Someone can tell me how to get the length n of a string variable
and to extract for example the first to m-th (<n) characters substring?

Thanx
Olivier

 
 
 

Kshell: length of a string and substring

Post by Ben Altma » Fri, 25 Jan 2002 02:44:44



Quote:> Hi there
> Someone can tell me how to get the length n of a string variable
> and to extract for example the first to m-th (<n) characters substring?

Sounds like homework, but you can use ${#variable} to get a length and to
extract a char there are many ways. E.g. sed.  you could use pure ksh, by
using typeset defining some vars with restricted lengths to the left and
right.  Look at the man page of ksh under typeset for more details.

regards,
Ben

 
 
 

Kshell: length of a string and substring

Post by Al Shark » Fri, 25 Jan 2002 04:12:47



> Someone can tell me how to get the length n of a string variable
> and to extract for example the first to m-th (<n) characters
> substring?

Length:
print ${#string}

Substring examples for the first five characters:

(destructively)
typeset -L5 string
print $string

Non-destructively:
typeset -L5 string2=$string
print $string2

or without using a second variable
print ${string%${string#?????}}

 
 
 

Kshell: length of a string and substring

Post by richy » Fri, 25 Jan 2002 11:52:22



> Hi there
> Someone can tell me how to get the length n of a string variable
> and to extract for example the first to m-th (<n) characters substring?

> Thanx
> Olivier

try if any of these works:

for the length:
expr length "string"
expr ${variable} : ".*"

for the substr:
expr substr "string" 1 m
echo "string" | cut -c1-m

 
 
 

1. How to compare String b is element of String a (substrings)

Hi,

Can someone help me with my Problem?
I am a newbie at shellscripting.

I have 2 input files.

$INPUTFILE="feed.txt"
$SPECIAL_FILE="special.txt"

-- content of feed.txt
http://some.domain/path/file
http://some.otherdomain/otherfile
http://111.222.333.444/file1
http://some.domain2/otherfile2
http://some.domain/otherpath/file3
http://111.222.333.444/spomepath/file4
..
--

-- content of special.txt
http://some.domain/
http://111.222.333.444/
..
--

-- myscript

..
..
..
special()
{
        grep http $SPECIAL_FILE | while read SPECIAL_URL
        do
                # If SPECIAL_URL is Part of URL
                #  set SPECIAL=1
                # This is my problem !!!!!
                if [ "^$SPECIAL_URL"="$URL" ] ; then
                        SPECIAL=1
                fi
        done

grep http $INPUTFILE | while read URL
do
        # call function special()
        special
        # if SPECIAL=1
        if [ $SPECIAL == 1 ] ; then
                // Do special action
                echo "Special Action for: $URL is used"
                ...
                // Reset SPECIAL
                SPECIAL=-1
        # normal URL
        else
                // Do normal stuff
                echo "Everything is okay with $URL"
        fi
done

My problem:
============
If an $URL is in SPECIAL_FILE than special action
has to be performed with $URL

or

if $SPECIAL_URL is part of $URL than special action
has to be performed with $URL

the comparison of the substring does not work.

Thanks for your help, hints...
Lars

2. Trouble with D-Link DFE-530TX+

3. *** NUMERIC STRINGS WHITH CSHELL OR KSHELL ??? ***

4. there are a "simple" method to manage 2 diferent domains on sendmail+rh6.0

5. process table list

6. substring from string

7. Apache and Location Header

8. Extracting substrings from strings

9. Extract a substring of n digits from a string

10. Question: How to find substring in a string?

11. extract substrings from a string

12. Removing substring from string