和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