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 4, and Part
- -1st: Take Part 5 from 9th to Part 5. Here-5 is short for part 1 to 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.73 m: Klondike
Hara Mikie: female: pictorial model: 19870703: 1.62 m: Oscar Promotion Co., Ltd.
Drew Barrymore: female: actor: 19750222: 1.63 m: Music and Lyrics
Kobe Bryant: male: NBA player: 19780823: 1.98 m: Los Angeles Lakers
Timothy Theodore Duncan: male: NBA player: 19760425: 2.11 m: San Antonio Spurs
Tony Parker: male: NBA player: 19820517: 1.88 m: San Antonio Spurs
Manu Gin Ardo bili: male: NBA player: 19770728: 1.98 m: San Antonio Spurs
LaMarcus Nurae Aldridge: male: NBA player: 19850719: 2.11 m: Portland Trail Blazers
AName: 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.73 m: Klondike
Harle: pictorial model: 19870703: 1.62 m: Oscar Promotion Co., Ltd.
Drefemale: actor: 19750222: 1.63 m: Music and Lyrics
Kobe: NBA player: 19780823: 1.98 m: Los Angeles Lakers
Time Duncan: male: NBA player: 19760425: 2.11 m: San Antonio Spurs
Tone: NBA player: 19820517: 1.88 m: San Antonio Spurs
Manmale: NBA player: 19770728: 1.98 m: San Antonio Spurs
LaMAldridge: male: NBA player: 19850719: 2.11 m: Portland Trail Blazers
ANaProfession: 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
Ginobili
San Antonio
$ Cut-B 3 chinese.txt
��
��
��
��
$ Cut-B 3-n chinese.txt
Section
Luo
Ji
Sheng
$
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
AName
AName
Abbie
Hara
Drew
Kobe
Timot
Tony
Manu
LaMar
AName
$
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 1, 3 Field
AName: aGender: aProfession: aBirthday: aHeight: aEtc.
AName: aGender: aProfession: aBirthday: aHeight: aEtc.
Abbie
Hara model: 19870703: 1.62 m: Oscar
Drew and
Kobe player: 19780823: 1.98 m: Los
Timothy Duncan: male: NBA
Tony player: 19820517: 1.88 m: San
Manu player: 19770728: 1.98 m: San
LaMarcus Aldridge: male: NBA
AName: aGender: aProfession: aBirthday: aHeight: aEtc.
$ Cut-f 1, 3-d''-s data.txt // use the-s option to ignore rows without field separator spaces. You can see that the rows with no space in the result are not output again.
Abbie
Hara model: 19870703: 1.62 m: Oscar
Drew and
Kobe player: 19780823: 1.98 m: Los
Timothy Duncan: male: NBA
Tony player: 19820517: 1.88 m: San
Manu player: 19770728: 1.98 m: San
LaMarcus Aldridge: male: NBA
$ Cut-f 1, 3-d': 'data.txt // specify ':' as the field separator. Print fields 1st and 3.
AName: aProfession
AName: aProfession
Abbie Cornishi: actor
Hara Mikie: pictorial model
Drew Barrymore: actor
Kobe Bryant: NBA player
Timothy Theodore Duncan: NBA player
Tony Parker: NBA player
Manu Gin óbili: NBA player
LaMarcus Nurae Aldridge: NBA player
AName: aProfession
$
Here, the explanation of the cut command is over. If there is any new content, I will try again later. Pai_^
Linux cut command learning notes
Cat for Linux commands
10 useful Linux Command Interview Questions and answers
This article permanently updates the link address: