实例分享shell字符截取命令之cut命令


本文摘自PHP中文网,作者巴扎黑,侵删。

这篇文章主要介绍了shell字符截取命令之cut命令的实例详解的相关资料,希望通过本文大家能够掌握这部分内容,需要的朋友可以参考下

shell字符截取命令之cut命令的实例详解

一 语法


1

2

3

4

cut [选项] 文件名

选项:

-f 列号:提取第几列

-d 分隔符:按照指定分隔符分割列

二 实战


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

[root@localhost ~]# cat student.txt

1 furong F 85

2 fengj F 60

3 cang F 70

[root@localhost ~]# cut -f 2 student.txt

furong

fengj

cang

[root@localhost ~]# cut -f 2,4 student.txt

furong 85

fengj 60

cang 70

[root@localhost ~]# grep "/bin/bash" /etc/passwd|grep -v "root"|cut -f 1 -d ":"

cakin24

test

cls

[root@localhost ~]# cut -d ":" -f 1,3 /etc/passwd

root:0

bin:1

daemon:2

adm:3

lp:4

sync:5

shutdown:6

halt:7

mail:8

operator:11

games:12

ftp:14

nobody:99

systemd-bus-proxy:999

systemd-network:998

dbus:81

polkitd:997

abrt:173

unbound:996

tss:59

colord:995

usbmuxd:113

geoclue:994

rpc:32

rtkit:172

chrony:993

radvd:75

qemu:107

avahi-autoipd:170

setroubleshoot:992

rpcuser:29

nfsnobody:65534

saslauth:991

libstoragemgmt:990

pulse:171

gdm:42

gnome-initial-setup:989

avahi:70

sshd:74

postfix:89

ntp:38

tcpdump:72

cakin24:1000

test:1003

cls:1001

三 cut命令的局限


1

df -h|cut -d " " -f 1,3

只能分割比较规则的文本,一般分割符是制表符或冒号。

不能简单的用空格作为分割符。

以上就是实例分享shell字符截取命令之cut命令的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

shell编程实战之网站文件及数据库文件的备份

一文读懂shell编程三剑客之一的sed命令

shell脚本实现的乘法表实例

linux中如何运行shell程序

linux下shell如何获取某目录下所有文件夹的名称详解

shell--标准输入输出(read&echo)

shell--标准输入输出(read&echo)

如何在shell脚本中传递命令行参数

如何在shell脚本中使用逻辑或&和

如何在运维过程中对shell内建命令

更多相关阅读请进入《shell》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...