How to extract a substring

How to extract a substring

Post by Jean-Philippe JANECE » Thu, 28 Nov 2002 01:32:18



Hi!!
Shell script : sh
I want to extract substring from string. How to do?
'expr substr "string" 1 0' does not work on my platform.
 
 
 

How to extract a substring

Post by John » Thu, 28 Nov 2002 01:58:12



> Hi!!
> Shell script : sh
> I want to extract substring from string. How to do?
> 'expr substr "string" 1 0' does not work on my platform.

There are lots of ways. Are you sure expr did not work,
by the way, as you seem to have asked for zero characters?

Depending on your shell, you can do something like
$ string="hello, world"; echo ${string:0:4}
hell

$ echo "hello, world" | sed 's/\(.\{1,4\}\).*/\1/'
hell

$ expr substr "hello, world" 1 4
hell

John.

 
 
 

How to extract a substring

Post by Stephane Chazela » Thu, 28 Nov 2002 02:05:30




> Shell script : sh
> I want to extract substring from string. How to do?
> 'expr substr "string" 1 0' does not work on my platform.

expr "string" : '.\(...\)'

cut -c2-4 << EOF
string
EOF

POSIX sh:
st="string"
${st#??} -> "ring"
${st%??} -> "stri"

--
Stphane

 
 
 

How to extract a substring

Post by Barry Margoli » Thu, 28 Nov 2002 01:53:09




>Hi!!
>Shell script : sh
>I want to extract substring from string. How to do?
>'expr substr "string" 1 0' does not work on my platform.

If your expr(1) doesn't have a "substr" operator, you can use cut(1):

echo "string" | cut -c1-4

--

Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

How to extract a substring

Post by Brian Hile » Thu, 28 Nov 2002 06:36:51


Jean-Philippe JANECEK <jean-philippe.janecekNOS...@realix.fr> wrote:
> I want to extract substring from string. How to do?
> 'expr substr "string" 1 0' does not work on my platform.

Here is a lot of code to simulate a rather canonical feature...

=Brian

#! /bin/echo error: only source
#*TAG:47372 8:Apr 28 1973:0755:../d.proj-strings/strsub:strsub:
# Author: Brian Hiles <b...@iname.com>
# Copyright: (c) 2001,2002
# Date: 2000/10/02
# Description: extract substring by column numbers
# Name: strsub
# Project: @(#)libstrings.sh 1.2 2000/10 b...@iname.com (Brian Hiles)
# Requires:
# See-also: ftp://ftp.uu.net/usenet/comp.sources.misc/volume1/8711/2 #XXX?
# See-also: ftp://ftp.uu.net/usenet/comp.sources.misc/volume1/8711/11
# See-also: ftp://ftp.uu.net/usenet/comp.sources.misc/volume2/substr
# Usage: strsub [-z] [-f first] [-l length] [-m max] [-u str] string...
# Usage: strsub [-cz] [-f first] [-l last] [-m max] [-u str] string...
# Usage: strsub -v [-z] [-f first] [-l length] [-m max] [-u str] varname...
# Usage: strsub -v [-cz] [-f first] [-l last] [-m max] [-u str] varname...
# Version: 1.02

#01
function strsub # [-cenvz] [-s n1] [-l n2] [-m n3] [-u str] varname|string...
# With -c option, determine substring from column num1 to column num2, else
# determine substring of length num2 starting from column num1. Max length num3
{       set -o noglob
        typeset -i base=1 emitlen=0 max=999999 ncol=0 nref=0 rc=0 setrc=0
        #XXX annul variable SUBSTR even if function fails?
        SUBSTR= typeset IFS first last=999999 opt usage=\
'usage: strsub [-z] [-f first] [-l length] [-m max] [-u str] string...
        strsub [-cz] [-f first] [-l last] [-m max] [-u str] string...
        strsub -v [-z] [-f first] [-l length] [-m max] [-u str] varname...
        strsub -v [-cz] [-f first] [-l last] [-m max] [-u str] varname...
-c      - -l option argument is column number [substring length]
-e      - set the exit value (implies -n option)
-f num  - set first column <num> of substring
-l num  - if option -c set, set <num> as length, else as last column
-m num  - set maximum length <num> of substring
-n      - determine substring length [value]
-v      - arguments are variable names [strings]
-z      - columns are understood to be zero-based [one-based]'
        while getopts :CcEef:l:m:Nnu:VvZz opt
        do      case $opt in
                (c)     ncol=1 ;;
                (+c|C)  ncol=0 ;;
                (e)     emitlen=1 setrc=1 ;;    # implies -n option
                (+e|E)  setrc=0 ;;              # may not entirely undo -e opt!
                (f)     (((first=OPTARG)>=0)) && continue
                        print -ru2 $0: error: bad first number
                        return 2 ;;
                (l)     (((last=OPTARG)>=0)) && continue
                        print -ru2 $0: error: bad second number
                        return 2 ;;
                (m)     (((max=OPTARG)>=0)) && continue
                        print -ru2 $0: error: bad length number
                        return 2 ;;
                (n)     emitlen=1 ;;
                (+n|N)  emitlen=0 ;;
                (u)     usage=$OPTARG ;;
                (v)     nref=1 ;;               # side-effect upon var args!
                (+v|V)  nref=0 ;;
                (z)     base=0 ;;
                (+z|Z)  base=1 ;;
                ([:?])  print -ru2 "$0: error: unknown option \"$opt\""
                        print -ru2 -- "$usage"
                        return 2 ;;
                esac
        done
        shift OPTIND-1
        ((!$#)) && print -ru2 -- "$usage" && return 2
        typeset -i col1=${first:-1} col2=$last len
        set X "$@"
        while   shift
                (($#))
        do      if ((nref))
                then    eval SUBSTR=\$$1
                else    SUBSTR=$1
                fi
                ((col2<col1)) &&
                print -ru2 "$0: error: col2 < col1" && rc=rc+1 continue
                ((!base)) && let col1+=1 col2+=1        # normalize to 1-based
                ((!ncol)) && let col2=col2+col1-1       # calc ending column
                (((len=${#SUBSTR})<max)) && let max=len
                (((col2-col1+1)>max)) && let col2=col1+max-1
                ((col1>len || max==0)) && col1=0     # emit null string
                ((col2>len)) && col2=len
${DIAG:+print -ru2 "[ $0: col1=$col1, col2=$col2, SUBSTR=\"$SUBSTR\" ]"}
                # Ksh Bug: typeset -L/-R strips leading/following whitespace!
                # To work around: temporarily delimit with an "X" character
                typeset -R$((len-col1+2)) SUBSTR=${SUBSTR}X
                typeset +R SUBSTR
                SUBSTR=${SUBSTR%X}
${DIAG:+print -ru2 "[ $0: stripped leftmost: SUBSTR=\"$SUBSTR\" ]"}
                # Ksh Bug: "typeset -L0 var" is a no-op!
                #XXX delete above cmd and change below to SUBSTR=X${SUBSTR%X}
                typeset -L$((col2-col1+2)) SUBSTR=X$SUBSTR
                typeset +L SUBSTR
                SUBSTR=${SUBSTR#X}
${DIAG:+print -ru2 "[ $0: stripped rightmost: SUBSTR=\"$SUBSTR\" ]"}
                if      ((!col1)) && SUBSTR=            # col1==0 special case
                        ((emitlen)) && SUBSTR=${#SUBSTR}
                        ((setrc)) && rc=$SUBSTR
                        ((nref))
                then    eval $1=\$SUBSTR
                else    #print -r -- "|$SUBSTR|"
                        print -r -- "$SUBSTR"
                fi
        done
        return $rc

}

#02 EMBEDDED MANPAGE FOR "src2man"
: '
#++
NAME
        strsub -- determine substring

SYNOPSIS
        strsub [-z] [-f first] [-l length] [-m max] [-u str] string...
        strsub [-cz] [-f first] [-l last] [-m max] [-u str] string...
        strsub -v [-z] [-f first] [-l length] [-m max] [-u str] varname...
        strsub -v [-cz] [-f first] [-l last] [-m max] [-u str] varname...

DESCRIPTION
        Strsub determines a substring given either first column number and
        length numbers or, with the -c option, first and last column numbers.

        A -v option allows the first parameter to be understood to be a
        variable name, which will be redefined, else this parameter is
        the string argument.

        If the -n option is given, the length of the calculated substring
        will be output. If the -e option is given this number will become
        the return value. The -e option implies the -n option.

        Option -m specifies a maximum length the substring must conform to.

OPTIONS
        -c      - -l option argument is column number [substring length].
        -e      - Set the exit value (implies -n option).
        -f num  - Set first column <num> of substring.
        -l num  - If option -c set, set <num> as length, else as last column.
        -m num  - Set maximum length <num> of substring.
        -n      - Determine substring length [value].
        -v      - Arguments are variable names [strings].
        -z      - Columns are understood to be zero-based [one-based].

ENVIRONMENT
        SUBSTR  - holds substring.

NOTES
        The arguments to -f, -l, and -m may be arithmetric expressions.

        Strsub(3S) can be used to emulate substring feature in versions of
        ksh newer than 1993:

                strsub -fN1 -lN2 -v varname

        is equivalent to ksh93 command:

                varname=${varname:<N1>:<N2>}

        Similarly,

                strsub -fN1 -lN2 -mN3 -v varname

        is equivalent to ksh93 command:

                typeset +L<N3> varname=${varname:<N1>:<N2>}

        If the -c option is used, N2 above is expressed as N1-N2+1 .

        Keep in mind that a side-effect is created upon the variable SUBSTR.

BUGS
        In ksh88 "typeset -L0 var" is a no-operation upon "var". Also,
        "typeset -L/R var" strips leading/following whitespace from "var"
        before applying this attribute.

        Any of the three parameters may be the number zero; however, in
        some cases the output is not defined when the -z option is not set.

AUTHOR
        Brian Hiles <b...@iname.com>

SEE ALSO
        strcpy(3S), strcmp(3S), strlen(3S), strncmp(3S), strncpy(3S),
        strcat(3S), strchr(3S), stricmp(3S), strcspn(3S), strdup(3S),
        strftime(3S), strncat(3S), strpbrk(3S), strptime(3S), strrchr(3S),
        strspn(3S), strstr(3S)

#--
'

Try this:

print =\> strsub -c -f3 -l4 foobar ... should print \"ob\"
strsub -c -f3 -l4 foobar
print =\> strsub -f10 -l11 \" foobar \" ... should print \"\"
strsub -f10 -l11 " foobar "
print =\> strsub -f3 -l4 -m100 foobar ... should print \"obar\"
strsub -f3 -l4 -m100 foobar
print =\> strsub -f3 -l4 foobar ... should print \"obar\"
strsub -f3 -l4 foobar
print =\> strsub -c -f3 -l4 -m1 foobar ... should print \"o\"
strsub -c -f3 -l4 -m1 foobar
print "=> strsub -f1 -l8 -m999 \" foobar \" \"  foo\" \"foo \""
strsub -f1 -l8 -m999 " foobar " "  foo" "foo "
'

 
 
 

How to extract a substring

Post by William Par » Thu, 28 Nov 2002 07:08:03



> Hi!!
> Shell script : sh
> I want to extract substring from string. How to do?
> 'expr substr "string" 1 0' does not work on my platform.

You are returning zero-length string starting at position 1.  So, it should
return empty string.

man expr:
       substr STRING POS LENGTH
              substring of STRING, POS counted from 1

--

Linux solution for data management and processing.

 
 
 

1. Extracting substrings from strings

I'm working with C scripts and I'm trying to get a substring from a
string.  So if $1 = "STRING", I want to read the 1st element
which would be "S" and put it into a string of its own.  The solution
in VMS would be to use f$extract but I don't know what a UNIX
equivalent is.  Any help would be appreciated.  Please respond
by e-mail as I don't usually read this group.  Thanks, Steve

2. poll and select

3. How to extract the substring from file record

4. Newbie with networking problems-Please Help!

5. grep problem - need to extract a substring

6. For file in ....

7. Extract a substring of n digits from a string

8. wav files under linux?

9. extract substrings from a string

10. Extracting substring from string BASH

11. (patch for Bash) extracting substring with BEGIN and END

12. newbie question:extracting a substring

13. tar question: how do I extract some files without extracting directories