Linux命令(83)之cut

linux命令之cut

1.cut介绍

linux命令cut用来从文本文件或者标准输入提取文本列

2.cut用法

cut [参数] [提取范围] 文本文件

|----|---------------|
| 参数 | 说明 |
| -c | 从指定提取范围中提取字符 |
| -f | 从指定提取范围中提取字符段 |
| -d | 指定分隔符 |
[cut常用参数]

提取范围:

n:第n项

n-:第n项到行尾

-m:行首到第m项

n,m:第n项和第m项(单个的)

n-m:第n项到第m项(连续的)

3.实例

3.1.提取12345678的第5个字符

命令:

echo 12345678 | cut -c 5

备注:cut下标默认从1开始

复制代码
[root@centos79-3 ~]# echo 12345678 | cut -c 5
5
[root@centos79-3 ~]# 

3.2.提取12345678的第2,4,6个字符

命令:

echo 12345678 | cut -c 2,4,6

复制代码
[root@centos79-3 ~]# echo 12345678 | cut -c 2,4,6
246
[root@centos79-3 ~]# 

3.3.提取12345678的第2个字符到结尾

命令:

echo 12345678 | cut -c 2-

复制代码
[root@centos79-3 ~]# echo 12345678 | cut -c 2-
2345678
[root@centos79-3 ~]# 

3.4.提取12345678的行首到第4个字符

命令:

echo 12345678 | cut -c -4

复制代码
[root@centos79-3 ~]# echo 12345678 | cut -c -4
1234
[root@centos79-3 ~]# 

3.5.提取12345678的第2到第5个字符

命令:

echo 12345678 | cut -c 2-5

复制代码
[root@centos79-3 ~]# echo 12345678 | cut -c 2-5
2345
[root@centos79-3 ~]# 

3.6.以冒号(:)为分隔符,提取/etc/passwd文件中第1、3个字段

命令:

cat /etc/passwd | cut -d : -f 1,3

复制代码
[root@centos79-3 ~]# cat /etc/passwd | cut -d : -f 1,3
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
ztj:1000
[root@centos79-3 ~]# 
相关推荐
改一下配置文件15 分钟前
Ubuntu24.04安装NVIDIA驱动完整指南(含Secure Boot解决方案)
linux
碳基沙盒43 分钟前
OpenClaw 多 Agent 配置实战指南
运维
深紫色的三北六号10 小时前
Linux 服务器磁盘扩容与目录迁移:rsync + bind mount 实现服务无感迁移(无需修改配置)
linux·扩容·服务迁移
SudosuBash14 小时前
[CS:APP 3e] 关于对 第 12 章 读/写者的一点思考和题解 (作业 12.19,12.20,12.21)
linux·并发·操作系统(os)
哈基咪怎么可能是AI1 天前
为什么我就想要「线性历史 + Signed Commits」GitHub 却把我当猴耍 🤬🎙️
linux·github
十日十行2 天前
Linux和window共享文件夹
linux
Sinclair2 天前
简单几步,安卓手机秒变服务器,安装 CMS 程序
android·服务器
木心月转码ing2 天前
WSL+Cpp开发环境配置
linux
Rockbean3 天前
用40行代码搭建自己的无服务器OCR
服务器·python·deepseek
蝎子莱莱爱打怪3 天前
Centos7中一键安装K8s集群以及Rancher安装记录
运维·后端·kubernetes