shell脚本学习(4)cut

 cut 的两种用法

1种是 -c list   剪切字符串中特定位置的文字,

/etc/passwd中的原始数据:

yuyuyu:x:1000:1000:yuyuyu,,,:/home/yuyuyu:/bin/bash

写法 n-m

yuyuyu@ubuntu:~$ cut -c 1-5 /etc/passwd

显示: yuyuyu

类似数组的索引,但字符位置是从1开始算的。

写法 ,,

yuyuyu@ubuntu:~$ cut -c 1,2,3,4,5 /etc/passwd

显示:
yuyuy

推测 list中的数据顺序不是按递增来写会怎么样,答案是结果还是按原始字符串的顺序显示:

 文字并没有倒叙。

另一种 cut -f list [-d delim] file

[-d delim] 是指定分隔符

-f list 是指定序列的序号

这两种写法都可以

yuyuyu@ubuntu:~$ cut -f 1-3 -d: /etc/passwd 

yuyuyu@ubuntu:~$ cut -d: -f 1-3  /etc/passwd

结果显示

yuyuyu:x:1000

原文地址:https://www.cnblogs.com/mayplestory/p/11594885.html