当前位置:网站首页>cut命令的使用实例
cut命令的使用实例
2022-08-09 06:29:00 【qq_36412526】
cut命令用于文本处理。通过选择列,可以使用此命令从文件中提取文本的一部分
cut命令的选项
Cut基本语法:
cut OPTION... [FILE]...
选项:
-f : 通过指定哪一个字段进行提取。cut命令使用“TAB”作为默认的字段分隔符。
-d : “TAB”是默认的分隔符,使用此选项可以更改为其他的分隔符。
–complement : 此选项用于排除所指定的字段。
–output-delimiter : 更改输出内容的分隔符。
如何分割
cut最常用的选项是-d和-f的组合。它基本上会根据特定的分隔符和列出的字段提取内容。
下面的代码仅使用分隔符:打印/etc/passwd文件中每一行的第一个字段。
[email protected]:~# cut -d ':' -f 1 /etc/passwd
root
daemon
bin
sys
sync
games
man
lp
mail
news
uucp
proxy
www-data
backup
list
irc
gnats
下面的代码从/etc/passwd文件提取第一和第六个字段:
[email protected]:~# grep '/bin/bash' /etc/passwd| cut -d ':' -f 1,6
root:/root
forlinx:/home/forlinx
ybtxr:/home/ybtxr
要显示字段的范围,请指定以-分隔的开始字段和结束字段,如下所示:
[email protected]:~# grep '/bin/bash' /etc/passwd|cut -d ':' -f 1-4,6,7
root:x:0:0:/root:/bin/bash
forlinx:x:1000:1000:/home/forlinx:/bin/bash
ybtxr:x:1001:1001:/home/ybtxr:/bin/bash
排除所指定的字段
在下面的代码中,打印所有字段,除了/etc/passwd文件中的第二个字段:
[email protected]:~# grep '/bin/bash' /etc/passwd|cut -d ':' --complement -f 2
root:0:0:root:/root:/bin/bash
forlinx:1000:1000:A40i,,,:/home/forlinx:/bin/bash
ybtxr:1001:1001:,,,:/home/ybtxr:/bin/bash
如何指定一个输出内容的分隔符
要指定输出分隔符,请使用–output-delimiter选项。输入分隔符由-d选项指定,默认情况下输出分隔符与输入分隔符相同。
先看一下没有使用–output-delimiter选项,是什么样子的:
[email protected]:~# cut -d ':' -f1,7 /etc/passwd|sort
avahi-autoipd:/bin/false
avahi:/bin/false
backup:/bin/sh
bin:/bin/sh
colord:/bin/false
现在使用–output-delimiter选项,输出分隔符使用’ ‘空格分隔,看一下是什么样子的:
[email protected]:~# cut -d ':' -f1,7 --output-delimiter=' ' /etc/passwd|sort
avahi-autoipd /bin/false
avahi /bin/false
backup /bin/sh
bin /bin/sh
总结
cut命令的一个限制是它不支持指定多个字符作为分隔符。多个空格被视为多个字段分隔符,必须使用tr命令才能得到所需的输出。
边栏推荐
- Fe3O4/SiO2 Composite Magnetic Nanoparticles Aminated on SiO2-NH2/Fe3O4 Surface (Qiyue Reagent)
- A test engineer with an annual salary of 35W was laid off. Personal experience: advice that you have to listen to
- Use of PlantUML plugin in idea
- 中英文说明书丨TRC 交替醇(Catalogue NumberA575760)
- Simple Factory Pattern
- qt send mail program
- The solution that does not work and does not take effect after VScode installs ESlint
- 一道很简答但是没答对的SQL题
- mmdetection源码解析--ResNet18
- 简单使用Lambda表达式
猜你喜欢
中英文说明书丨CalBioreagents 山羊抗人白蛋白,IgG组分
中英文说明书丨TRC 交替醇(Catalogue NumberA575760)
phpstudy install flarum forum
Teach you how to make the Tanabata meteor shower in C language - elegant and timeless (detailed tutorial)
带头双向循环链表的增删查改(C语言实现)
[GO], arrays and slices
Output method of list string print(*a) print(““.join(str(c) for c in a) )
中英文说明书丨CalBioreagents ACTH N端单克隆抗体
C language implements sequential stack and chain queue
输入框最前面添加放大镜&&background-image和background-color冲突问题
随机推荐
.NET高级技术
中英文说明书丨TRC 交替醇(Catalogue NumberA575760)
深度学习-神经网络原理2
Can Jincang Database Set Transaction Automatic Commit?
ZIP压缩包文件删除密码的方法
Polyamide-amine (PAMAM) dendrimer-bismuth sulfide composite nanoparticles | bismuth sulfide modified Gd‑DTPA‑OA ligand | for scientific research
C# 利用iTextSharp画PDF
shardingsphere数据分片配置项说明和示例
[GO], arrays and slices
IQ Products CMV Brite Turbo试剂盒的原理
The 24th day of the special assault version of the sword offer
Molybdenum disulfide/hafnium dioxide composite nanomaterials (MoS2/HfO2) | tantalum-doped hafnium dioxide nanoparticles (Qi Yue bio)
BeautifulSoup4的介绍与使用
Used to import the data type
2022-08-08: Given an array arr, it represents the height of the missiles that will appear in order from morning to night.When the cannon shoots missiles, once the cannon is set to shoot at a certain h
【Feel】Unity Feel插件中,Camera无法正确显示CameraShake
MYSQL Advanced Chapter - Query Interception Analysis, Lock Mechanism, Master-Slave Replication
Data center project preliminary summary
How to find package information and pin definitions for NXP S32K1xx series microcontrollers
Simple Factory Pattern