The cut command is used to read content from a file or standard input, extract specific parts of each row, and send them to standard output.
There are three ways to intercept, one is by character location, the other is by byte location, the third is to use a separator to split a line into multiple fields, and extract the specified fields.
The cut command has five parameters, including-C,-B, and-F, which indicate the truncation methods of "character", "Byte", and "field" respectively. To intercept in field mode, you must use the "-d" parameter to specify a separator. The delimiter can only be a single character. There is also a "-s", suppress, indicating that if the line does not provide the delimiter, the line is not output (by default, if there is no separator, the line will not be output)
The following are examples:
- Truncate by character: Echo Hello, world | cut-C 8-12 then output "world" (The truncate string contains 12 characters from 8th to 12th)
- Intercept by separator: Echo Hello, world | cut-F 2-D "", output "world" (intercept the second part separated by space)
- Echo Long, long ago | cut-F 2, 3-D "", the output is "long, Ago" (section 2nd and three separated by spaces, note that the output results are also separated by the separator specified by-D)
- Use "-s" to quietly ignore the line without a given separator: Echo Hello | cut-d "! "-F 1-S, nothing is output (because the row does not exist "! "Character)
If the command is successfully executed, 0 is returned. If an error occurs, a number greater than 0 is returned.
Among them, "-c" and "-B" are easy to understand. I will understand the "-F" parameter that I have understood below. I will experiment several times and output several times.
# Echo Long, long ago, ddddddd | cut-F 2-D,
Long ago
# Echo Long, long ago, ddddddd | cut-F 2-D,
Long ago, ddddddd
# Echo Long, long ago, ddddddd, hhhhhhhhhhh | cut-F 2-D,
Long ago, ddddddd, hhhhhhhhhhh
The above three comparisons will be clear.
# Echo Long, long ago, ddddddd | cut-F 2, 3-D,
Long ago, ddddddd
# Echo Long, long ago, ddddddd | cut-F 1, 3-D,
Long, ddddddd
# Echo Long, long ago, ddddddd | cut-F 1, 2-D,
Long, long ago
# Echo Long, long ago ddddddd | cut-F 2-D,
Long ago ddddddd
# Echo Long, long ago ddddddd | cut-F 2, 3-D,
Long ago ddddddd