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

相关实践学习
CentOS 8迁移Anolis OS 8
Anolis OS 8在做出差异性开发同时,在生态上和依赖管理上保持跟CentOS 8.x兼容,本文为您介绍如何通过AOMS迁移工具实现CentOS 8.x到Anolis OS 8的迁移。
相关文章
|
7天前
|
Linux
Linux系统之whereis命令的基本使用
Linux系统之whereis命令的基本使用
50 23
Linux系统之whereis命令的基本使用
|
2月前
|
存储 缓存 监控
Linux缓存管理:如何安全地清理系统缓存
在Linux系统中,内存管理至关重要。本文详细介绍了如何安全地清理系统缓存,特别是通过使用`/proc/sys/vm/drop_caches`接口。内容包括清理缓存的原因、步骤、注意事项和最佳实践,帮助你在必要时优化系统性能。
230 78
|
20天前
|
网络协议 Unix Linux
深入解析:Linux网络配置工具ifconfig与ip命令的全面对比
虽然 `ifconfig`作为一个经典的网络配置工具,简单易用,但其功能已经不能满足现代网络配置的需求。相比之下,`ip`命令不仅功能全面,而且提供了一致且简洁的语法,适用于各种网络配置场景。因此,在实际使用中,推荐逐步过渡到 `ip`命令,以更好地适应现代网络管理需求。
33 11
|
1月前
|
缓存 安全 Linux
Linux系统查看操作系统版本信息、CPU信息、模块信息
在Linux系统中,常用命令可帮助用户查看操作系统版本、CPU信息和模块信息
109 23
|
2月前
|
Linux Shell 网络安全
Kali Linux系统Metasploit框架利用 HTA 文件进行渗透测试实验
本指南介绍如何利用 HTA 文件和 Metasploit 框架进行渗透测试。通过创建反向 shell、生成 HTA 文件、设置 HTTP 服务器和发送文件,最终实现对目标系统的控制。适用于教育目的,需合法授权。
89 9
Kali Linux系统Metasploit框架利用 HTA 文件进行渗透测试实验
|
2月前
|
Linux Shell
Linux 10 个“who”命令示例
Linux 10 个“who”命令示例
99 14
Linux 10 个“who”命令示例
|
2月前
|
Ubuntu Linux
Linux 各发行版安装 ping 命令指南
如何在不同 Linux 发行版(Ubuntu/Debian、CentOS/RHEL/Fedora、Arch Linux、openSUSE、Alpine Linux)上安装 `ping` 命令,详细列出各发行版的安装步骤和验证方法,帮助系统管理员和网络工程师快速排查网络问题。
203 20
|
2月前
|
Linux
linux查看目录下的文件夹命令,find查找某个目录,但是不包括这个目录本身?
通过本文的介绍,您应该对如何在 Linux 系统中查看目录下的文件夹以及使用 `find` 命令查找特定目录内容并排除该目录本身有了清晰的理解。掌握这些命令和技巧,可以大大提高日常文件管理和查找操作的效率。 在实际应用中,灵活使用这些命令和参数,可以帮助您快速定位和管理文件和目录,满足各种复杂的文件系统操作需求。
141 8
|
2月前
|
存储 监控 Linux
嵌入式Linux系统编程 — 5.3 times、clock函数获取进程时间
在嵌入式Linux系统编程中,`times`和 `clock`函数是获取进程时间的两个重要工具。`times`函数提供了更详细的进程和子进程时间信息,而 `clock`函数则提供了更简单的处理器时间获取方法。根据具体需求选择合适的函数,可以更有效地进行性能分析和资源管理。通过本文的介绍,希望能帮助您更好地理解和使用这两个函数,提高嵌入式系统编程的效率和效果。
121 13
|
2月前
|
网络协议 Linux 应用服务中间件
kali的常用命令汇总Linux
kali的常用命令汇总linux
128 7