Use the cut command in Linux

Source: Internet
Author: User

Use the cut command in Linux

There are many command lines for text processing in Linux. Here we will introduce the cut command.

The cut command can cut lines in text files by byte, character, and field, and then write the specified part to the standard output. If no file name is specified, the cut command reads the standard input by default.

1. cut command syntax Summary

Cut {-B list [-n] |-c list |-f list [-s] [-d character]} [file ..]

From the above format, it is not difficult to see that there are three methods to use the cut command, corresponding to three different parameter forms, which have been marked with different colors. The red part corresponds to cut by byte, the green part corresponds to cut by character, and the brown part corresponds to cut by field. The list parameter is used in the three methods. The following describes several forms of the list parameter:

1, 3, 5 take part 1, 3, and 5 (the part here refers to the byte, character, or field) 2-7th take part 2 to Part 4-Part 5 and Part 5-5 take part 5 to Part 5. Here-5 is short for part 1-5. 5-take part 1 and all parts afterwards

The following describes how to cut by byte, character, and field.

2. Cut by byte

As described in the preceding syntax, the-B option should be used for byte cutting. Pay attention to the use of the-n option:-n option and-B option are used together. If a character (here multi-byte characters) if the last byte falls within the range specified by the list, the character is printed. If no, the character is not printed. If no-n option is available, if the range specified by list is separated by one multi-byte character, garbled characters are printed. The following are examples. The document used here is a user name that can be generated with the name data.txt. The content is as follows:

$ cat data.txt aName:aGender:aProfession:aBirthday:aHeight:aEtc.aName:aGender:aProfession:aBirthday:aHeight:aEtc.Abbie Cornishi:female:actor:19820807:1.73m:KlondikeHara Mikie:female:pictorial model:19870703:1.62m:Oscar Promotion Co., Ltd.Drew Barrymore:female:actor:19750222:1.63m:Music and LyricsKobe Bryant:male:NBA player:19780823:1.98m:Los Angeles LakersTimothy Theodore Duncan:male:NBA player:19760425:2.11m:San Antonio SpursTony Parker:male:NBA player:19820517:1.88m:San Antonio SpursManu Ginóbili:male:NBA player:19770728:1.98m:San Antonio SpursLaMarcus Nurae Aldridge:male:NBA player:19850719:2.11m:Portland Trail BlazersaName:aGender:aProfession:aBirthday:aHeight:aEtc.$ 
Cut the file in bytes as follows:

$ cut -b 1-3,16- data.txt aNaProfession:aBirthday:aHeight:aEtc.aNaProfession:aBirthday:aHeight:aEtc.Abbfemale:actor:19820807:1.73m:KlondikeHarle:pictorial model:19870703:1.62m:Oscar Promotion Co., Ltd.Drefemale:actor:19750222:1.63m:Music and LyricsKobe:NBA player:19780823:1.98m:Los Angeles LakersTime Duncan:male:NBA player:19760425:2.11m:San Antonio SpursTone:NBA player:19820517:1.88m:San Antonio SpursManmale:NBA player:19770728:1.98m:San Antonio SpursLaMAldridge:male:NBA player:19850719:2.11m:Portland Trail BlazersaNaProfession:aBirthday:aHeight:aEtc.
I would like to demonstrate the use of the-n option. I don't print garbled characters on Ubuntu machines, and it may be related to the settings of my terminal. For a centos machine, the following is an example:

$ Cat chinese.txt Kobe Bryant Los Angeles Lakers Ginobili San Antonio Spurs $ cut-B 3 chinese.txt ???? $ Cut-B 3-n chinese.txt corogies $
After this experiment, you know that a Chinese character occupies about several bytes.

3. Cut by characters

It is relatively simple to cut by character, that is, the-c option. The following is an example.

$ cut -b 1-4,5 data.txt aNameaNameAbbieHara Drew Kobe TimotTony Manu LaMaraName$ 
4. Cut by field

The option to cut by field is-f (field ). When using the cut by field, pay attention to the following two options:

-D character is used with the-f option. The specified character is used as the field separator (the default field separator is a tab ). Note: escape characters with special shell meanings. For example, if a space is specified as a field separator, quotation marks (-d '') must be added ''). Note that character can only be a single character as a field separator. -S cancels rows that do not contain field delimiters.

The following are examples:

$ Cut-f 1, 3-d' data.txt // specify the space as the field separator and print the aName: aGender: aProfession: aBirthday: aHeight: aEtc. aName: aGender: aProfession: aBirthday: aHeight: aEtc. abbieHara model: 19870703: 1.62 m: OscarDrew andKobe player: 19780823: 1.98 m: LosTimothy Duncan: male: NBATony player: 19820517: 1.88 m: SanManu player: 19770728: 1.98 m: sanLaMarcus Aldridge: male: NBAaName: aGender: aProfession: aBirthday: aHeight: aEtc. $ cut-f 1, 3-d'-s data.txt // use the-s option to ignore rows without field separator spaces, we can see that the rows without spaces in the result are no longer output. AbbieHara model: 19870703: 1.62 m: OscarDrew andKobe player: 19780823: 1.98 m: LosTimothy Duncan: male: NBATony player: 19820517: 1.88 m: SanManu player: 19770728: 1.98 m: SanLaMarcus Aldridge: male: NBA $ cut-f 1, 3-d ': 'data.txt // specify': 'as the field separator, print the aName: Audience: aProfessionAbbie Cornishi: actorHara Mikie: pictorial modelDrew Barrymore: actorKobe Bryant: NBA playerTimothy Theodore Duncan: NBA playerTony Parker: NBA playermangin óbili: NBA playerLaMarcus Nurae Aldridge: NBA playeraName: aProfession $
Here, the explanation of the cut command is over. If there is any new content, I will try again later. Pai_^

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.