Cut Command in Linux with Examples

Posted in

Cut Command in Linux with Examples
sangeeta.gulia

Sangeeta Gulia
Last updated on December 3, 2024

    When dealing with Linux, filtering content from a file may be a typical task. It's possible that you won't need to display the full file's content but rather custom content with varying sizes. This is a difficult task to complete manually. In this article, we'll go over some of the options for the cut command in Linux, which can help you with a variety of tasks involving content cutting and filtering.

    What is the cut command?

    Many programs for processing and filtering text files are available on Linux and Unix platforms. In UNIX, the cut command is used to clip parts off each line of a file and write the result to standard output. It may be used to break lines into chunks based on the delimiter, byte location, and character.

    The cut command basically cuts a line and extracts the content. If you don't supply an option with the command, you'll get an error. Data from each file is not preceded by its file name if more than one file name is given. The syntax is:

    $ cut OPTION... [FILE]...

    1. –version

    This option displays the version of cut that is presently installed on your computer.

    $ cut --version

    Output

    cut (GNU coreutils) 8.26
    
    Packaged by Cygwin (8.26-2)
    
    Copyright (C) 2016 Free Software Foundation, Inc.
    
    License GPLv3+: GNU GPL version 3 or later .
    
    This is free software: you are free to change and redistribute it.
    
    There is NO WARRANTY, to the extent permitted by law.
    
    Written by David M. Ihnat, David MacKenzie, and Jim Meyering.

    2. -b(byte)

    You must use the - b option with a list of byte numbers separated by a comma to extract individual bytes. The hyphen can also be used to specify a range of bytes (-). It is required to supply a list of byte numbers; else, an error will occur. Backspaces and tabs are considered one-byte characters. Below is an example of the option

    $ cut -b 2,3,4 demo.txt

    3. -c (column)

    Use the -c option to cut by character. The characters specified by the -c option are selected. This can be a comma-separated list of numbers or a hyphen-separated range of integers (-). Backspaces and tabs are considered as characters. If you don't supply a list of character numbers, this option will return an error. The following is the syntax of the command:

    $ cut -c 3,6,8 demo.txt

    4. -f (field)

    For fixed-length lines, the - c option is beneficial. There are no fixed-length lines in most Unix files. You must cut by fields rather than columns to obtain the important information. The number of fields supplied must be separated by a comma. The -f option does not describe ranges. cut defaults to tab as a field delimiter, but with the -d option, it can also operate with other delimiters.

    $ cut -d "delimiter" -f (field number) demo.txt

    5. –complement

    It complements the output, as the name implies. This option can be used with either -f or -c in conjunction with other options.

    $ cut --complement -d " " -f 1 demo.txt

    6. –output-delimiter

    The output delimiter is set to the same as the input delimiter in the cut with -d option by default. Use the –output-delimiter ="value" option to alter the output delimiter.

    $ cut -d " " -f 1,2 demo.txt --output-delimiter='$'

    Conclusion

    In this article, we discussed various examples and options related to the cut command which is used to display and filter the content using various options. However, the cut command does not allow multiple delimiters.

    People are also reading:

    Leave a Comment on this Post

    0 Comments