zoukankan      html  css  js  c++  java
  • shell 中cut

    和awk差不多的功能

    上面的例子中,把 root:x:0:0:root:/root:/bin/bash 重定向到cut命令里,-d表示分隔符,这里使用冒号: 作为分隔符,-f 表示字段,选择了第1,和第5个字段,

    $ a=`echo root:x:0:0:root:/root:/bin/bash | cut -d : -f 1,5`
     
    shuohailhl@shuohailhl-PC /cygdrive/d
    $ echo $a
    root:root

    例 2,只打印第一个字段field

    例 5 截取指定个数的字符

    shuohailhl@shuohailhl-PC /cygdrive/d
    $ a=`echo root:x:0:0:root:/root:/bin/bash | cut -c 2-5 ` // 截取第2到第5个字符
     
     
    shuohailhl@shuohailhl-PC /cygdrive/d
    $ echo $a
    oot:
     
    shuohailhl@shuohailhl-PC /cygdrive/d
    $ a=`echo root:x:0:0:root:/root:/bin/bash | cut -c 2-7 `  // 截取第2到第7个字符
     
    shuohailhl@shuohailhl-PC /cygdrive/d
    $ echo $a
    oot:x:
     
    shuohailhl@shuohailhl-PC /cygdrive/d
    $ a=`echo root:x:0:0:root:/root:/bin/bash | cut -c -2 `   // 截取前两个字符
     
    shuohailhl@shuohailhl-PC /cygdrive/d
    $ echo $a
    ro
     
    shuohailhl@shuohailhl-PC /cygdrive/d
    $ a=`echo root:x:0:0:root:/root:/bin/bash | cut -c 2- `    // 截取第2个以后的
     
    shuohailhl@shuohailhl-PC /cygdrive/d
    $ echo $a
    oot:x:0:0:root:/root:/bin/bash 
  • 相关阅读:
    Redis 的基本操作、Key的操作及命名规范
    python离线安装外部库(第三方库)
    STL之deque
    STL之list
    STL学习之vector
    STL三种标准容器
    Lua系统库
    Lua输入输出库
    Lua字符串库
    Lua面向对象
  • 原文地址:https://www.cnblogs.com/kakaisgood/p/11639982.html
Copyright © 2011-2022 走看看