|NO.Z.00003|——————————|LinuxShell|——|Linux&Shell&字符截取和替换命令.V01|——|cut|

一、cut列提取命令
### --- cut命令选项说明
~~~     选项:
~~~     -f 列号:        提取第几列
~~~     -d 分隔符:      按照指定分隔符分割列
~~~     -c 字符范围:    不依赖分隔符来区分列,而是通过字符范围(行首为 0)来进行字段提取。
~~~     “n-”表示从第 n 个字符到行尾;
~~~     “n-m”从第 n 个字符到第 m个字符;“-m”表示从第 1 个字符到第 m 个字符。
~~~     cut 命令的默认分隔符是制表符,也就是“tab”键,不过对空格符可是支持的不怎么好啊。
~~~     我们先建立一个测试文件,然后看看 cut 命令的作用吧:

[root@localhost ~]#     cut [选项] 文件名
### --- 创建cut测试文本
~~~     注:使用table键制表

[root@localhost ~]# vi student.txt
ID  Name    gendet  Mark
1   Liming  M   86
2   Sc  M   90
3   TM  M   83
### --- 提取第二列内容

[root@localhost ~]# cut -f 2 student.txt
Name
Liming
Sc
TM
### --- 那如果想要提取多列呢?只要列号直接用“,”分开,命令如下:
~~~     cut 可以按照字符进行提取,需要注意“8-”代表的是提取所有行的第十个字符开始到行尾,
~~~     而“10-20”代表提取所有行的第十个字符到第二十个字符,
~~~     而“-8”代表提取所有行从行首到第八个字符:

[root@localhost ~]# cut -f 2,3 student.txt
Name    gendet
Liming  M
Sc  M
TM  M
二、cut 可以按照字符进行提取,
### --- cut 可以按照字符进行提取,
~~~     需要注意“8-”代表的是提取所有行的第十个字符开始到行尾,
~~~     而“10-20”代表提取所有行的第十个字符到第二十个字符,
~~~     而“-8”代表提取所有行从行首到第八个字符:
~~~     注:提取第八个字符开始到行尾,好像很乱啊,那是因为每行的字符个数不相等啊

[root@localhost ~]# cut -c 8- student.txt
    gendet  Mark
g   M   86
90
83
### --- 以“:”作为分隔符,提取/etc/passwd 文件的第一列和第三列

[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-network:192
dbus:81
polkitd:999
sshd:74
postfix:89
### --- 如果我想用 cut 命令截取 df 命令的第一列和第三列,就会出现这样的情况

[root@localhost ~]# df -h | cut -d " " -f 1,3
Filesystem 
/dev/sda8 
devtmpfs 
tmpfs 
tmpfs 
tmpfs 
/dev/sda6 
/dev/sda5 
tmpfs 

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on   yanqi_vip  阅读(27)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示