Full contact with cut commands in UNIX

Source: Internet
Author: User

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

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.