9.2. Manipulating Strings

9.2. Manipulating Strings

1、String Length

expr "$string" : '.*'

3、Index这个表达式跟java的用法好像不同

expr index $string $substring

    Numerical position in $string of first character in $substring that matches.


   5 echo `expr index "$stringZ" 1c`              # 3

   6 # 'c' (in #3 position) matches before '1'.

4?{string:position:length}负数参数的用法有点怪

    Extracts $length characters of substring from $string at $position.


  16 echo ${stringZ:-4}                           # abcABC123ABCabc

  17 # Defaults to full string, as in ${parameter:-default}.

  18 # However . . .

  19

  20 echo ${stringZ:(-4)}                         # Cabc

  21 echo ${stringZ: -4}                          # Cabc

  22 # Now, it works.

  23 # Parentheses or added space "escape" the position parameter.

  24

  25 # Thank you, Dan Jacobson, for pointing this out.

而且下面这段不懂:


    If the $string parameter is "*" or "@", then this extracts a maximum of

$length positional parameters, starting at $position.

       1 echo ${*:2}          # Echoes second and following positional

parameters.

       2 echo ${@:2}          # Same as above.

       3

       4 echo ${*:2:3}        # Echoes three positional parameters, starting

at second.

5、Example 9-12. Emulating getopt其中的字符串处理不甚明了


  14       if [ ${1:0:1} = '/' ]

  15       then

  16           tmp=${1:1}               # Strip off leading '/' . . .

  17           parameter=${tmp%%=*}     # Extract name.

  18           value=${tmp##*=}         # Extract value.

  19           echo "Parameter: '$parameter', value: '$value'"

  20           eval $parameter=$value

  21       fi

6、awk处理函数不懂


  14 # The awk equivalent of ${string:pos:length} is

substr(string,pos,length).

  15 echo | awk '

  16 { print substr("'"${String}"'",3,4)      # skid

  17 }

  18 '

7、9.2.2. Further Discussion进一步讨论的几个例子没看

英语疑问:

1、emulation, getopt

A simple emulation of getopt  using substring extraction constructs.