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

举报
江湖有缘 发表于 2023/03/14 14:36:28 2023/03/14
【摘要】 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地址

[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












【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。