Use the Cut command in Linux to extract text usage guidelines

Source: Internet
Author: User

Sometimes we often encounter such a problem: there is a page of the telephone number is thin, in order to write the rules of people's names, home addresses, telephone, notes, etc., at this time we just want to get everyone's name and their corresponding phone number, you have several ways to achieve it?

It is true that this vertical positioning is difficult to achieve by conventional means, at which point cut will be able to work.
What ' s cut?
Sub Yue: The Cut command extracts a text column from a text file or from a text stream.

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 to make field (field) delimiters
-F,--fields=list
Output these fields
-N
Ignored
-S,--only-delimited
Do not show lines without delimiters
--output-delimiter=string
Using STRING as the output delimiter, the default (output delimiter) is the input separator
--help
Display Help information, and then end
--version
Display version information, and then end

Use and use only one of the options in-B,-C, or-F. The LIST consists of multiple ranges separated by a range (range) or comma. The scope is one of the following forms:

N
Nth Byte, character or field, counting from 1
N-
From Nth Byte, character or field up to end of line
N-m
From Nth to M (and including m) byte, character or field
-M
From 1th to M (and including m) byte, character or field

Read data from standard input if you do not specify file files, or.

Example:

The code is as follows:

[Root@localhost ~]# cat/etc/passwd | Cut-b 1 |head-5 #输出文件的第一个字节
R
B
D
A
L

The code is as follows:

[Root@localhost ~]# cat/etc/passwd | Cut-c 1-4 |head-5 #输出文件的前四个字符
Root
Bin
Daem
Adm:
Lp:x

The code is as follows:

[Root@localhost ~]# cat/etc/passwd | Cut-f1-d ': ' |head-5 #以: Split file, output first field
Root
Bin
Daemon
cd/
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,3-d $ ' t ' #1, 3 columns
SSSs dddd
RRR ADFA


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.