Linux系统之cut命令的基本使用

简介: Linux系统之cut命令的基本使用

一、cut命令介绍

cut 命令 用来显示行中的指定部分,删除文件中指定字段。cut 命令从文件的每一行剪切字节、字符和字段并将这些字节、字符和字段写至标准输出。

二、cut命令的使用帮助

1.在Linux中的命令帮助

在Linux的命令终端中,查看cut的help帮助。

[root@jeven ~]# cut --help
Usage: cut OPTION... [FILE]...
Print selected parts of lines from each FILE to standard output.

Mandatory arguments to long options are mandatory for short options too.
  -b, --bytes=LIST        select only these bytes
  -c, --characters=LIST   select only these characters
  -d, --delimiter=DELIM   use DELIM instead of TAB for field delimiter
  -f, --fields=LIST       select only these fields;  also print any line
                            that contains no delimiter character, unless
                            the -s option is specified
  -n                      with -b: don't split multibyte characters
      --complement        complement the set of selected bytes, characters
                            or fields
  -s, --only-delimited    do not print lines not containing delimiters
      --output-delimiter=STRING  use STRING as the output delimiter
                            the default is to use the input delimiter
      --help     display this help and exit
      --version  output version information and exit

Use one, and only one of -b, -c or -f.  Each LIST is made up of one
range, or many ranges separated by commas.  Selected input is written
in the same order that it is read, and is written exactly once.
Each range is one of:

  N     N'th byte, character or field, counted from 1
  N-    from N'th byte, character or field, to end of line
  N-M   from N'th to M'th (included) byte, character or field
  -M    from first to M'th (included) byte, character or field

With no FILE, or when FILE is -, read standard input.

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'cut invocation'

2.cut命令的选项解释

cut命令的选项解释

-b:仅显示行中指定直接范围的内容;
-c:仅显示行中指定范围的字符;
-d:指定字段的分隔符,默认的字段分隔符为“TAB”;
-f:显示指定字段的内容;
-n:与“-b”选项连用,不分割多字节字符;
--complement:补足被选择的字节、字符或字段;
--out-delimiter= 字段分隔符:指定输出内容是的字段分割符;
--help:显示指令的帮助信息;
--version:显示指令的版本信息。

三、cut的基本使用

1.查看cut版本

检查cut工具的版本

[root@jeven ~]# cut --version
cut (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by David M. Ihnat, David MacKenzie, and Jim Meyering.

2.指定文本第一列的内容

使用 -f 选项提取指定字段,后面加数字,表示第几列,使用n1,n2表示第n1列到第n2列。

[root@jeven ~]# cut -f 1 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
libstoragemgmt:x:998:995:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
colord:x:997:994:User for colord:/var/lib/colord:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
gluster:x:996:993:GlusterFS daemons:/run/gluster:/sbin/nologin
saslauth:x:995:76:Saslauthd user:/run/saslauthd:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
radvd:x:75:75:radvd user:/:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
unbound:x:994:989:Unbound DNS resolver:/etc/unbound:/sbin/nologin
chrony:x:993:988::/var/lib/chrony:/sbin/nologin
qemu:x:107:107:qemu user:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
geoclue:x:992:986:User for geoclue:/var/lib/geoclue:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
sssd:x:991:985:User for sssd:/:/sbin/nologin
setroubleshoot:x:990:984::/var/lib/setroubleshoot:/sbin/nologin
saned:x:989:983:SANE scanner daemon user:/usr/share/sane:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
gnome-initial-setup:x:988:982::/run/gnome-initial-setup/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
admin:x:1000:1000:admin:/home/admin:/bin/bash
mysql:x:1001:1001::/home/mysql:/sbin/nologin
redis:x:987:980:Redis Database Server:/var/lib/redis:/sbin/nologin
zhangsan:x:1002:1002::/home/zhangsan:/bin/bash
lisi:x:1003:1003::/home/lisi:/bin/bash
kitty:x:1004:1004::/home/kitty:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin

3.指定分隔符提取列的内容

使用-d来指定分隔符,来提取列的内容。

[root@jeven ~]# cut -d ":" -f 1 /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody
systemd-network
dbus
polkitd
libstoragemgmt
colord
rpc
gluster
saslauth
abrt
rtkit
pulse
radvd
rpcuser
nfsnobody
unbound
chrony
qemu
tss
usbmuxd
geoclue
ntp
sssd
setroubleshoot
saned
gdm
gnome-initial-setup
sshd
avahi
postfix
tcpdump
admin
mysql
redis
zhangsan
lisi
kitty
apache

四、cut指定字段的字符或者字节范围

1.cut的指定范围介绍

cut 命令可以将一串字符作为列来显示,字符字段的记法:
N- :从第 N 个字节、字符、字段到结尾;
N-M :从第 N 个字节、字符、字段到第 M 个(包括 M 在内)字节、字符、字段;
-M :从第 1 个字节、字符、字段到第 M 个(包括 M 在内)字节、字符、字段。

-b 表示字节;
-c 表示字符;
-f 表示定义字段。

2.打印文本第1个到第5个字符的列

打印文本第1个到第5个字符的列

[root@jeven ~]# cut -c1-5 /etc/passwd
root:
bin:x
daemo
adm:x
lp:x:
sync:
shutd
halt:
mail:
opera
games
ftp:x
nobod
syste
dbus:
polki
libst
color
rpc:x
glust
sasla
abrt:
rtkit
pulse
radvd
rpcus
nfsno
unbou
chron
qemu:
tss:x
usbmu
geocl
ntp:x
sssd:
setro
saned
gdm:x
gnome
sshd:
avahi
postf
tcpdu
admin
mysql
redis
zhang
lisi:
kitty
apach

3.打印文本第1个到第5个字节的列

打印文本第1个到第5个字节的列


[root@jeven ~]# cut -b1-5 /etc/passwd
root:
bin:x
daemo
adm:x
lp:x:
sync:
shutd
halt:
mail:
opera
games
ftp:x
nobod
syste
dbus:
polki
libst
color
rpc:x
glust
sasla
abrt:
rtkit
pulse
radvd
rpcus
nfsno
unbou
chron
qemu:
tss:x
usbmu
geocl
ntp:x
sssd:
setro
saned
gdm:x
gnome
sshd:
avahi
postf
tcpdu
admin
mysql
redis
zhang
lisi:
kitty
apach

4.打印文本的前两个字符的列

打印文本的前两个字符的列

[root@jeven ~]# cut -c-2 /etc/passwd
ro
bi
da
ad
lp
sy
sh
ha
ma
op
ga
ft
no
sy
db
po
li
co
rp
gl
sa
ab
rt
pu
ra
rp
nf
un
ch
qe
ts
us
ge
nt
ss
se
sa
gd
gn
ss
av
po
tc
ad
my
re
zh
li
ki
ap

5.打印文本后3个字符的列

打印文本每行后3个字符的列

[root@jeven ~]# cat /etc/passwd |rev |cut -c -3 |rev
ash
gin
gin
gin
gin
ync
own
alt
gin
gin
gin
gin
gin
gin
gin
gin
gin
gin
gin
gin
gin
gin
gin
gin
gin
gin
gin
gin
gin
gin
gin
gin
gin
gin
gin
gin
gin
gin
gin
gin
gin
gin
gin
ash
gin
gin
ash
ash
ash
gin

五、提取服务器网卡IP地址

使用cut提取服务器网卡的IP地址

```bash
[root@jeven ~]# ifconfig | egrep "inet>" | tr -s " " | cut -d" " -f3
172.21.0.1
192.168.32.1
172.19.0.1
172.34.0.1
172.18.0.1
172.23.0.1
192.168.48.1
10.89.162.1
172.29.120.1
192.168.16.1
172.17.0.1
192.168.3.166
192.168.3.33
127.0.0.1
192.168.122.1

[root@jeven ~]# ifconfig | egrep "inet>" | tr -s " " | cut -d" " -f3
172.21.0.1
192.168.32.1
172.19.0.1
172.34.0.1
172.18.0.1
172.23.0.1
192.168.48.1
10.89.162.1
172.29.120.1
192.168.16.1
172.17.0.1
192.168.3.166
192.168.3.33
127.0.0.1
192.168.122.1

```[root@jeven ~]# ifconfig | egrep "inet>" | tr -s " " | cut -d" " -f3
172.21.0.1
192.168.32.1
172.19.0.1
172.34.0.1
172.18.0.1
172.23.0.1
192.168.48.1
10.89.162.1
172.29.120.1
192.168.16.1
172.17.0.1
192.168.3.166
192.168.3.33
127.0.0.1
192.168.122.1

相关文章
|
28天前
|
Linux 应用服务中间件 Shell
二、Linux文本处理与文件操作核心命令
熟悉了Linux的基本“行走”后,就该拿起真正的“工具”干活了。用grep这个“放大镜”在文件里搜索内容,用find这个“探测器”在系统中寻找文件,再用tar把东西打包带走。最关键的是要学会使用管道符|,它像一条流水线,能把这些命令串联起来,让简单工具组合出强大的功能,比如 ps -ef | grep 'nginx' 就能快速找出nginx进程。
277 1
二、Linux文本处理与文件操作核心命令
|
1月前
|
Ubuntu Linux Anolis
Linux系统禁用swap
本文介绍了在新版本Linux系统(如Ubuntu 20.04+、CentOS Stream、openEuler等)中禁用swap的两种方法。传统通过注释/etc/fstab中swap行的方式已失效,现需使用systemd管理swap.target服务或在/etc/fstab中添加noauto参数实现禁用。方法1通过屏蔽swap.target适用于新版系统,方法2通过修改fstab挂载选项更通用,兼容所有系统。
136 3
Linux系统禁用swap
|
1月前
|
Linux
Linux系统修改网卡名为eth0、eth1
在Linux系统中,可通过修改GRUB配置和创建Udev规则或使用systemd链接文件,将网卡名改为`eth0`、`eth1`等传统命名方式,适用于多种发行版并支持多网卡配置。
185 3
|
28天前
|
安全 Ubuntu Unix
一、初识 Linux 与基本命令
玩转Linux命令行,就像探索一座新城市。首先要熟悉它的“地图”,也就是/根目录下/etc(放配置)、/home(住家)这些核心区域。然后掌握几个“生存口令”:用ls看周围,cd去别处,mkdir建新房,cp/mv搬东西,再用cat或tail看文件内容。最后,别忘了随时按Tab键,它能帮你自动补全命令和路径,是提高效率的第一神器。
543 56
|
13天前
|
存储 安全 Linux
Linux卡在emergency mode怎么办?xfs_repair 命令轻松解决
Linux虚拟机遇紧急模式?别慌!多因磁盘挂载失败。本文教你通过日志定位问题,用`xfs_repair`等工具修复文件系统,三步快速恢复。掌握查日志、修磁盘、验重启,轻松应对紧急模式,保障系统稳定运行。
131 2
|
23天前
|
缓存 监控 Linux
Linux内存问题排查命令详解
Linux服务器卡顿?可能是内存问题。掌握free、vmstat、sar三大命令,快速排查内存使用情况。free查看实时内存,vmstat诊断系统整体性能瓶颈,sar实现长期监控,三者结合,高效定位并解决内存问题。
71 0
Linux内存问题排查命令详解
|
Ubuntu Linux 网络安全
Linux系统初始化脚本
一款支持Rocky、CentOS、Ubuntu、Debian、openEuler等主流Linux发行版的系统初始化Shell脚本,涵盖网络配置、主机名设置、镜像源更换、安全加固等多项功能,适配单/双网卡环境,支持UEFI引导,提供多版本下载与持续更新。
156 0
Linux系统初始化脚本
|
1月前
|
Unix Linux 程序员
Linux文本搜索工具grep命令使用指南
以上就是对Linux环境下强大工具 `grep` 的基础到进阶功能介绍。它不仅能够执行简单文字查询任务还能够处理复杂文字处理任务,并且支持强大而灵活地正则表达规范来增加查询精度与效率。无论您是程序员、数据分析师还是系统管理员,在日常工作中熟练运用该命令都将极大提升您处理和分析数据效率。
134 16
|
1月前
|
Linux
linux命令—stat
`stat` 是 Linux 系统中用于查看文件或文件系统详细状态信息的命令。相比 `ls -l`,它提供更全面的信息,包括文件大小、权限、所有者、时间戳(最后访问、修改、状态变更时间)、inode 号、设备信息等。其常用选项包括 `-f` 查看文件系统状态、`-t` 以简洁格式输出、`-L` 跟踪符号链接,以及 `-c` 或 `--format` 自定义输出格式。通过这些选项,用户可以灵活获取所需信息,适用于系统调试、权限检查、磁盘管理等场景。
|
2月前
|
运维 Linux 开发者
Linux系统中使用Python的ping3库进行网络连通性测试
以上步骤展示了如何利用 Python 的 `ping3` 库来检测网络连通性,并且提供了基本错误处理方法以确保程序能够优雅地处理各种意外情形。通过简洁明快、易读易懂、实操性强等特点使得该方法非常适合开发者或系统管理员快速集成至自动化工具链之内进行日常运维任务之需求满足。
155 18