当前位置:网站首页>Example of using the cut command
Example of using the cut command
2022-08-09 06:33:00 【qq_36412526】
cut command is used for text processing.You can use this command to extract a portion of text from a file by selecting a column
Options for the cut command
Cut basic syntax:
cut OPTION... [FILE]...
Options:
-f : By specifying which field to extract.The cut command uses "TAB" as the default field separator.
-d : "TAB" is the default delimiter, use this option to change to other delimiters.
--complement : This option is used to exclude the specified field.
--output-delimiter : Change the delimiter for output content.
How to split
The most common options for cut are the combination of -d and -f.It basically extracts content based on a specific delimiter and the fields listed.
The following code uses only delimiters: prints the first field of each line in the /etc/passwd file.
[email protected]:~# cut -d ':' -f 1 /etc/passwdrootdaemonbinsyssyncgamesmanlpmailnewsuucpproxywww-databackuplistircgnats
The following code extracts the first and sixth fields from the /etc/passwd file:
[email protected]:~# grep '/bin/bash' /etc/passwd| cut -d ':' -f 1,6root:/rootforlinx:/home/forlinxybtxr:/home/ybtxr
To display the range of fields, specify the start and end fields separated by - as follows:
[email protected]:~# grep '/bin/bash' /etc/passwd|cut -d ':' -f 1-4,6,7root:x:0:0:/root:/bin/bashforlinx:x:1000:1000:/home/forlinx:/bin/bashybtxr:x:1001:1001:/home/ybtxr:/bin/bash
Exclude specified fields
In the code below, print all fields except the second field in the /etc/passwd file:
[email protected]:~# grep '/bin/bash' /etc/passwd|cut -d ':' --complement-f 2root:0:0:root:/root:/bin/bashforlinx:1000:1000:A40i,,,:/home/forlinx:/bin/bashybtxr:1001:1001:,,,:/home/ybtxr:/bin/bash
How to specify a delimiter for output content
To specify the output delimiter, use the --output-delimiter option.The input delimiter is specified by the -d option, by default the output delimiter is the same as the input delimiter.
Let's take a look at what it looks like without the --output-delimiter option:
[email protected]:~# cut -d ':' -f1,7 /etc/passwd|sortavahi-autoipd:/bin/falseavahi:/bin/falsebackup:/bin/shbin:/bin/shcolord:/bin/false
Now use the --output-delimiter option, the output delimiter is separated by ' ' space, and see what it looks like:
[email protected]:~# cut -d ':' -f1,7 --output-delimiter=' ' /etc/passwd|sortavahi-autoipd /bin/falseavahi /bin/falsebackup /bin/shbin /bin/sh
Summary
A limitation of thecut command is that it does not support specifying multiple characters as delimiters.Multiple spaces are treated as multiple field separators and the tr command must be used to get the desired output.
边栏推荐
- io.lettuce.core.RedisCommandTimeoutException Command timed out
- [HNOI2002]营业额统计
- 程序性能分析 —— 复杂度分析
- VS2019 common shortcut keys
- Likou Brush Question 180
- 我入职阿里后,才知道原来简历这么写
- 治疗消化性溃疡—Toronto Research Chemicals 甘氨酸铝
- 报错jinja2.exceptions.UndefinedError: ‘form‘ is undefined
- Error: flask: TypeError: 'function' object is not iterable
- 移远EC20 4G模块拨号相关
猜你喜欢
Flask failed to create database without error
pycharm环境包导入到另外一个环境
sql problem solving statement to create a table
分布式id 生成器实现
pdf加密、找回密码
Built-in macros in C language (define log macros)
idea中PlantUML插件使用
Adds, deletes, searches, and changes the leading doubly circular linked list (implemented in C language)
install flask
e-learning summary
随机推荐
P7阿里面试题2020.07 之滑动窗算法(阿里云面试)
leetcode 之 零移位
Service
6 states of a thread
字节跳动笔试题2020 (抖音电商)
【Wwise】ArgumentException: The specified path is not of a legal form (empty). About the path reading error in WwiseGlobal
阿里巴巴官方技术号
Common Oracle Commands
TCP segment of a reassembled PDU
代码目录结构
C语言的内置宏(定义日志宏)
抗菌药物丨Toronto Research Chemicals 天冬酰胺D
VS2019常用快捷键
Use baidu EasyDL intelligent bin
APP product source data interface (taobao, jingdong/spelling/suning/trill platform details a lot data analysis interface) code and docking tutorial
Introduction of convenient functions and convenient shortcut keys of vs tomato assistant
ByteDance Interview Questions: Mirror Binary Tree 2020
分布式id 生成器实现
移远EC20 4G模块拨号相关
使用百度EasyDL实现智能垃圾箱