UNIX Cut command

Source: Internet
Author: User

Cut command
Use
Writes out the selected bytes, characters, or fields from each line of the file.
Grammar
Cut {

-B List [-n] |  -C List | -F List [-S] [-D Character]} [File ...]
Describe
The cut command cuts bytes, characters, and fields from each line of the file and writes those bytes, characters, and fields to standard output. If you do not specify a File parameter, the Cut command reads standard input.
One of the-B,-C, or-f flags must be specified. The list parameter is a comma-delimited, space-delimited, or hyphen-delimited listing of integers (in sequential increments). The hyphen delimiter represents the range. The following entry is an example of a List parameter that can be used to refer to a byte, character, or field:
1,4,7
1-3,8
-5,10
3-
Where 5 is the shorthand form from the first to the fifth, 3-is the shorthand form from the third to the last.
If the cut command is used for a field, the length of the field specified by the List parameter can vary from field to field, from row to row. The position of the field delimiter character (such as a tab) to determine the length of the field.
You can also use the grep command to cut a file horizontally, and use the Paste command to restore the file. To change the order of the columns in the file, use the cut and Paste commands.
Sign
-B List Specifies the byte position. These byte locations will ignore multibyte character boundaries unless the-n flag is also specified.

-C List Specifies the character position. For example, if you specify the-C 1-72,cut command to write out the first 72 characters of each line of a file.
-D Character uses the character specified by the Character variable as the field delimiter when specifying the-f flag. You must enclose a character (such as a space character) that has special meaning to the shell.
The-f list Specifies the lists of fields separated by delimiters (by default, tabs) in the file. For example, if you specify the-F 1,7,cut command, only the first and seventh fields of each row will be written out. If the rows do not contain field delimiters, the cut command will pass them without any action (useful for the subtitle of the table) unless the-s flag is specified.

-N cancels the splitting of multibyte characters. Used only with the-B flag. If the last byte of a character falls within the range indicated by the List parameter of the-B flag, the character is written out; otherwise, the character is excluded.
-S cancels rows that do not contain delimiters. Used only with the-f flag.

Exit Status
This command returns the following exit values:
0  All input files are successfully exported.
>0  An error occurred.
Example
1.  to display several fields for each row of a file, enter:
cut -F 1,5-d:/etc/passwd
This displays the login name and the full User name field for the system password file. This is the first and fifth fields (-f 1,5), separated by a colon (-D:).
For example, if the/etc/passwd file looks like this:
Su:*:0:0:user with special privileges:/:/usr/bin/sh
Daemon:*:1:1::/etc:
bin:* : 2:2::/usr/bin:
sys:*:3:3::/usr/src:
Adm:*:4:4:system administrator:/var/adm:/usr/bin/sh
pierre:* : 200:200:pierre harper:/home/pierre:/usr/bin/sh
Joan:*:202:200:joan brown:/home/joan:/usr/bin/sh
Cut The command produces the following result:
Su:user with special privileges
Daemon:
Bin:
Sys:
adm:system Administrator
Pierre: Pierre Harper
Joan:joan Brown
2.  to use a space-delimited list to display fields, enter:
Cut-f "1 2 3"-D:/etc/passwd
The Cut command will produce the following result:
su:*:0
daemon:*:1
bin:*:2
sys:*:3
adm:*:4
pierre:*:200
joan:*:202
File
/usr/bin/cut& nbsp Include the Cut command

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.