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

韵味老鸟 2024-09-17 14:10:59

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

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

1. cut 命令简介

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

基本语法bashcut [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:~#

0 阅读:0