Get the App
SLTechnology News&Howtos  ›  Servers  › 

The use of cut commands to extract text in Linux

Shulou Source: shulou.com Published: 2022-06-01 12:52:30 09月22日 Update

This article mainly explains the usage of using cut command to extract text in Linux. Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "the use of cut command to extract text in Linux".

Command usage:

Cut-b list [- n] [file...]

Cut-c list [file...]

Cut-f list [- d delim] [- s] [file...]

Parameters:

-b,-- bytes=LIST

Output these bytes

-c,-- characters=LIST

Output these characters

-d,-- delimiter=DELIM

Use DELIM instead of TAB as the field delimiter

-f,-- fields=LIST

Output these fields

-n

(ignore)

-s-- only-delimited

Lines without delimiters are not displayed

-- output-delimiter=STRING

Use STRING as the output delimiter and the default (output delimiter) as the input delimiter

-- help

Display help information and then end

-- version

Display version information, and then end

Use and use only one of-b,-c, or-f. A LIST consists of a range (range) or multiple ranges separated by commas. Scope is one of the following forms:

N

Nth byte, character, or field, counting from 1

N-

From the Nth byte, character or field to the end of the line

Nmurm

Bytes, characters, or fields from N to M (and including M)

-M

Bytes, characters, or fields from 1st to M (and including M)

If no file FILE is specified, or if FILE is -, the data is read from standard input.

Example:

The code is as follows:

[root@localhost ~] # cat / etc/passwd | cut-b 1 | first byte of head-5 # output file

R

B

D

A

L

The code is as follows:

[root@localhost ~] # cat / etc/passwd | cut-c 1-4 | the first four characters of the head-5 # output file

Root

Bin:

Daem

Adm:

Lp:x

The code is as follows:

[root@localhost ~] # cat / etc/passwd | cut-F1-d':'| head-5 # to split the file and output the first field

Root

Bin

Daemon

Adm

Lp

The code is as follows:

# cat a.txt

Ssss affff dddd fe fsc

Rrr f adfa eer ddd

The code is as follows:

# cat a.txt | cut-F1 Magazine 3-d $'\ t'# 1pare 3 columns

Ssss dddd

Rrr adfa

At this point, I believe you have a deeper understanding of "the use of cut commands to extract text in Linux". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Output fields characters bytes code delimiters commands files text ranges information content learning input utility deeper interest parameters multiple utility Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Docker NVidia Redmi macOS