当前位置: 首页 > article >正文

linux 操作系统下的cut命令介绍和使用案例

linux 操作系统下的cut命令介绍和使用案例

在 Linux 操作系统中,cut 命令是一个用于文本处理的命令行工具,允许用户从文件或管道数据中提取特定部分并将结果输出到标准输出

1. cut 命令简介

cut 命令主要用于从文本文件中提取特定的字段、字节或字符。它常用于处理结构化数据文件,如 CSV 或 TSV 文件,能够根据分隔符(如逗号或制表符)提取列。

基本语法

bash
cut [OPTIONS] [FILE]
  • OPTIONS: 用于指定如何切割文本的选项。
  • FILE: 要处理的文件名。如果未指定文件名,cut 将从标准输入读取数据。

2. cut 命令选项

选项

描述

-f 或 --fields=LIST

根据指定字段(列)选择数据。

-b 或 --bytes=LIST

根据指定字节选择数据。

-c 或 --characters=LIST

根据指定字符选择数据。

-d 或 --delimiter

指定分隔符,默认为制表符(TAB)。

-s 或 --only-delimited

只输出包含分隔符的行。

--complement

显示所有字节、字符或字段,除了选定的部分。

--output-delimiter

指定输出的分隔符,默认为输入的分隔符。

命令介绍:

root@meng:~# which cut

/usr/bin/cut

root@meng:~# cut --help

Usage: cut OPTION... [FILE]...

Print selected parts of lines from each FILE to standard output.

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

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 (ignored)

--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

-z, --zero-terminated line delimiter is NUL, not newline

--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

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>

Report any translation bugs to <https://translationproject.org/team/>

Full documentation <https://www.gnu.org/software/coreutils/cut>

or available locally via: info '(coreutils) cut invocation'

root@meng:~# cut

cut: you must specify a list of bytes, characters, or fields

Try 'cut --help' for more information.

root@meng:~#

命令案例:

root@meng:~# more s2.txt

his is a test

HloWorld

Hlo World

Hlo World

g

g

hlo men

hlo meng

root@meng:~# cut -b 1-3 s2.txt

Hlo

Hlo

Hlo

g

g

hlo

hlo

root@meng:~# cut -d ' ' -f 2 s2.txt

is

is World

World

World

g

g

men

meng

root@meng:~#


http://www.kler.cn/news/306136.html

相关文章:

  • JavaScript控制语句和函数的使用
  • Python Numpy布尔数组在数据分析中的应用
  • 思维商业篇(3)—三大竞争战略
  • 【安全系列--处理挖矿】
  • Centos 执行yum安装 出现Failed connect to mirrors.163.com:80; 拒绝连接
  • Golang | Leetcode Golang题解之第409题最长回文串
  • Java中的服务端点响应缓存:Spring Cache抽象
  • ★ C++基础篇 ★ string类的实现
  • Python实现pdf转图片、转文字、去水印
  • 房产销售系统开发:SpringBoot技术要点
  • 避免 PyCharm 将该 Python 脚本作为测试运行
  • 串口数据波形显示工具对比
  • k8s service如何实现流量转发
  • Python 课程10-单元测试
  • 基于 TDMQ for Apache Pulsar 的跨地域复制实践
  • 2024.9.14 Python与图像处理新国大EE5731课程大作业,马尔可夫随机场和二值图割,校正立体图像的深度
  • 攻击者如何在日常网络资源中隐藏恶意软件
  • rust + bevy 实现小游戏 打包成wasm放在浏览器环境运行
  • Vue3使用Websocket进行跨页面通信
  • 2024年金九银十最新版Java面试题及答案整理(持续更新)
  • 从单体到微服务:FastAPI ‘挂载’子应用程序的转变
  • AI在医学领域:医学AI的安全与隐私全面概述
  • 【iOS】ViewController的生命周期
  • Linux06(vi/vim)
  • 【Python机器学习】循环神经网络(RNN)——超参数
  • 什么是Java 语法糖?
  • ARM驱动学习之8 动态申请字符类设备号
  • sqli-labs Basic Challenge Less_1 通关指南
  • 【AI学习】陶哲轩在 2024 年第 65 届国际数学奥林匹克(IMO)的演讲:AI 与数学
  • 基于YOLOV8+Pyqt5光伏太阳能电池板目标检测系统