Description
Cut press the column to split the file and you can specify the delimiters for each column. In cut terms, each column is a field, sometimes the first column, which may be described as the first field.
Actual combat:
Suppose there is a file data.txt, the format is as follows
NO Name Mark Percent
1 Sarath 45 90
2 Alex 49 98
3 Anu 45 90
Delimiter (delimiter) as tab tab
If I want to get the second column, everyone's name, is there any good way? At this point cut should be a skill.
1. Get 2, 3 column that is name, and Mark
$cut –f 2,3 Data.txt
2. Acquisition of all columns except column 3rd
$cut –f3–complement Data.txt
3. The use of other delimiters can be changed with-D.
Copy Code code as follows:
Suppose Data_comma.txt:
No,name,mark,percent
1,sarath,45,90
2,alex,49,98
3,anu,45,90
The
$cut –f2-d "," Data_comma.txt
Interpreting-help
Usage: cut [options] ... [File] ...
Outputs the specified portion from each file to the standard output.
Parameters that must be used for long options are also required for short options.
-B, the--bytes= list selects only those bytes specified
-C, the--characters= list selects only those characters specified
-D, the--delimiter= delimiter replaces the tab character with the specified delimiter as a regional demarcation
-F, the--fields= list selects only those fields that are specified, and prints all those that do not contain a delimiter.
Row, unless the-s option is specified
-N (ignored)
--complement the selected byte, character, or field
-S,--only-delimited does not print lines that do not contain a delimiter
The--output-delimiter= string uses the specified string as the output delimiter, with the default input
The delimiters
--HELP displays this help information and exits
--version display version information and exit
Use only one of the f-b,-C, or-F. Each list is made specifically for one category, or you can use a comma-separated
Open the different categories you want to display at the same time. Your input order will be in the reading order, and each can be entered only once.
Each parameter format represents the following range:
n bytes, characters, or fields from the 1th starting number
n all characters, Bytes, or fields from the beginning of Nth to the end of the line
N-m all characters, Bytes, or fields from the beginning of Nth to the first m (including the first m)
-m all characters, Bytes, or fields from the beginning of the 1th to the first m (including the first m)
Read from standard input when there is no file parameter, or if the file does not exist
Interpretation:
1. Get a column of characters or bytes can be used with-C,-B, etc.
2. A range can be obtained
1 and 2 Comprehensive examples:
There are documents Range_data.txt, the contents are as follows
Copy Code code as follows:
Abcdefghijklmnopqrstuvwxyz
Abcdefghijklmnopqrstuvwxyz
Abcdefghijklmnopqrstuvwxyz
Abcdefghijklmnopqrstuvwxyz
Run: $cut range_data.txt-c-2
Print character 1-2 columns
A little more complicated:
$cut range_data.txt-c1-3,6-9--output-delimiter ","
Print 1-3 lines, 6-9 lines, separated by commas.
The command is simpler, the main reference is the book "Linux shell Script", from the attachment will see a row of bad letters, try to use-s filter off him.