zoukankan      html  css  js  c++  java
  • shell 截取字符串(转)

    linux中对字符串的处理:

    1.字符串分割例如  AAAAA-BBBBBB  按-分割去前后两部分

     cut :

    [rich@localhost ~]$ str=AAAAA-BBBBBB
    [rich@localhost ~]$ echo $str | cut -d "-" -f 1
    AAAAA
    [rich@localhost ~]$ echo $str | cut -d "-" -f 2
    BBBBBB

    解释:A | B  将A命令的输出 作为B命令的输入

    cut

    -d  :分隔符

    -f   : field  分割后的数组索引 只不过是从1开始

    如果不存在分割符则返回全部:

    [rich@localhost ~]$ echo $str | cut -d "c" -f 1
    AAAAA-BBBBBB

    expr  && cut:

    取“-”的索引

    [rich@localhost ~]$ index=`expr index  "${str}" "-"`

    [rich@localhost ~]$ echo $str | cut -c`expr "${index}" + 1`-
    BBBBBB
    [rich@localhost ~]$ echo $str | cut -c1-`expr ${index} - 1`
    AAAAA

    cut

    -c 截取字符串 索引从1开始

    -cA-   A到末尾

    -cA  A位置

    -c A-B   索引A-B

    -c-A   截取前A个字符

    substr:

    [rich@localhost ~]$ index=`expr ${index} - 1`

    [rich@localhost ~]$ echo `expr substr "${str}" 1 ${index}` 
    AAAAA

    substr  str A  B

    从索引A开始截取B长度的字符串

    转自 http://blog.csdn.net/a276202460/article/details/5080551

  • 相关阅读:
    团队作业
    第四次作业
    第三次作业
    从电梯问题,看c和c++之间的区别(有点懂了)错觉错觉
    团队作业2
    游戏方案
    电梯调度程序4
    电梯调度程序3
    电梯调度程序2
    电梯调度程序1
  • 原文地址:https://www.cnblogs.com/SamuelSun/p/4792152.html
Copyright © 2011-2022 走看看