Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Use the cut command of linux to connect the file and print it to the standard output device

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the use of linux cut command to connect the file and print to the standard output device, the article is very detailed, has a certain reference value, interested friends must read it!

The cut command has two main functions: the first is to display the contents of the file, and the second is to connect multiple or more files.

If you do not specify the file parameter, the command reads standard input. One of the-b,-c, or-f flags must be specified.

Syntax format: cut [parameters] [file]

Commonly used parameters: coach

-b is divided in bytes, showing only the contents of the specified direct range in the line-c is divided in characters, and only the character-d custom delimiter in the specified range in the line is displayed By default, the tab "TAB"-f displays the contents of the specified field, using-n with-d to unsplit multibyte characters-- complement complements the selected byte, character, or field-- out-delimiter specifies the field separator that the output is.

Reference example

Suppose you have a student report information that contains No, Name, Mark, Percent:

[root@linuxcool ~] # cat student.txt

No Name Mark Percent

01 tom 69 91

02 jack 71 87

03 alex 68 98

Use the-f option to extract the specified field (the f parameter here can be simply remembered as the abbreviation of-- fields):

[root@linuxcool] # cut-f 2 student.txt

Name

Tom

Jack

Alex

-- the complement option extracts columns other than the specified field (prints columns other than the second column):

[root@linuxcool] # cut-f2-- complement student.txt

No Mark Percent

01 69 91

02 71 87

03 68 98

Use the-d option to specify the field delimiter:

[root@linuxcool ~] # cat student2.txt

No;Name;Mark;Percent

01 / 10 / 69 / 91

02tjackter71Ten 87

03positionalexter68 [root@linuxcool] # cut-f2-d ";" student2.txt "

Name

Tom

Jack

Alex [root@linuxcool ~] # cat test.txt

Abcdefghijklmnopqrstuvwxyz

Abcdefghijklmnopqrstuvwxyz

Abcdefghijklmnopqrstuvwxyz

Abcdefghijklmnopqrstuvwxyz

Abcdefghijklmnopqrstuvwxyz

Print the 1st to 3rd characters:

[root@linuxcool] # cut-C1-3 test.txt

Abc

Abc

Abc

Abc

Abc

Note:-b for bytes,-c for characters, and-f for definition fields.

N -: from the Nth byte, character, field to the end; NMurM: from the Nth byte, character, field to the M (including M) byte, character, segment;-M: from the 1st byte, character, field to the M (including M) byte, character, field.

Print the first 2 characters:

[root@linuxcool] # cut-Cmuri 2 test.txt

Ab

Ab

Ab

Ab

Ab

Print from the fifth character to the end:

[root@linuxcool] # cut-c5-test.txt

Efghijklmnopqrstuvwxyz

Efghijklmnopqrstuvwxyz

Efghijklmnopqrstuvwxyz

Efghijklmnopqrstuvwxyz

Efghijklmnopqrstuvwxyz above is using linux's cut command to connect the file and print it to the standard output device, thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report