您的位置:首页 > 运维架构 > Linux

Linux命令: cut命令的使用方法

2017-11-26 13:50 691 查看
1、cut用法(文本显示剪切)

   -d : 制定字段分隔符,默认是空格 

   -f :制定要显示的字段 

         -f1 :显示第一个字段 

         -f 1,3 显示第一个和第三个 

         -f 1-3 显示第一个到第三个 

   -b : 显示字节数 

   -c : 显示字符

2、示例

现在有一个test.c文件如下:
nii@mch:~/code$ cat test.c
lightdm:x:119:129:Light Display Manager:/var/lib/lightdm:/bin/false
nii:x:1000:1000:wzj,,,:/home/nii:/bin/bash
sshd:x:120:65534::/var/run/sshd:/usr/sbin/nologin
mysql:x:121:132:MySQL Server,,,:/nonexistent:/bin/false
x2gouser:x:122:133::/var/lib/x2go:/bin/false
gdm:x:123:134:Gnome Display Manager:/var/lib/gdm:/bin/false
debian-spamd:x:124:135::/var/lib/spamassassin:/bin/sh
ftp:x:125:137:ftp daemon,,,:/srv/ftp:/bin/false
statd:x:126:65534::/var/lib/nfs:/bin/false
1
2
3
4
5
6
7
8
9
10
11

cut -b 1-8 test.c //显示文件的第一个到第八个字节
nii@mch:~/code$ cut -b 1-8 test.c
lightdm:
nii:x:10
sshd:x:1
mysql:x:
x2gouser
gdm:x:12
debian-s
ftp:x:12
statd:x:
1
2
3
4
5
6
7
8
9
10

cut -b 1,8 test.c //显示文件的第一个和第八个字节
nii@mch:~/code$ cut -b 1,8 test.c
l:
n0
s1
m:
xr
g2
ds
f2
s:
1
2
3
4
5
6
7
8
9
10
11

cut -d : -f1 test.c //:为分割符,显示分割之后的第一个区域
nii@mch:~/code$ cut -d : -f1 test.c
lightdm
nii
sshd
mysql
x2gouser
gdm
debian-spamd
ftp
statd
1
2
3
4
5
6
7
8
9
10

cut -d : -f1,3 test.c //:为分割符,显示分割之后的第一个和第三个区域
nii@mch:~/code$ cut -d : -f1,3 test.c
lightdm:119
nii:1000
sshd:120
mysql:121
x2gouser:122
gdm:123
debian-spamd:124
ftp:125
statd:126


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: