Javatpoint Logo
Javatpoint Logo

Linux cut Command

Linux cut command is useful for selecting a specific column of a file. It is used to cut a specific sections by byte position, character, and field and writes them to standard output. It cuts a line and extracts the text data. It is necessary to pass an argument with it; otherwise, it will throw an error message.

To cut a specific section, it is necessary to specify the delimiter. A delimiter will decide how the sections are separated in a text file. Delimiters can be a space (' '), a hyphen (-), a slash (/), or anything else. After '-f' option, the column number is mentioned.

Syntax:

Options:

The following command line options are used by the cut command to make it more specific:

-b, --bytes=LIST: It is used to cut a specific section by bytes.

-c, --characters=LIST: It is used to select the specified characters.

-d, --delimiter=DELIM: It is used to cut a specific section by a delimiter.

-f, --fields=LIST: It is used to select the specific fields. It also prints any line that does not contain any delimiter character, unless the -s option is specified.

-n: It is used to ignore any option.

--complement: It is used to complement the set of selected bytes, characters or fields

-s, --only-delimited: It is used to not print lines that do not have delimiters.

--output-delimiter=STRING: This option is specified to use a STRING as an output delimiter; The default is to use "input delimiter".

-z, --zero-terminated: It is used if line delimiter is NUL, not newline.

--help: It is used to display the help manual.

--version: It is used to display the version information.

Examples of the cut command

Let's see the following examples of the cut command:

Using Hyphen (-) As Delimiter

To cut by using the hyphen (-) as the delimiter, execute the below command:

Consider the following commands:

from the above commands, the output will be trimmed from hyphen (-). Consider the below output:

Linux Cut Filter

As we can see from the above output, our delimiter is the hyphen (-); hence we have used (-) after (-d). Command "cut -d- -f1 marks.txt" displays column 1 and command "cut -d- -f2 marks.txt" displays column 2.

Using Space As Delimiter

If we want to use space as a delimiter, then we have to quote the space (' ') with the cut command. To cut the output by using space as delimiter, execute the command as follows:

Consider the following commands:

From the above commands, the output will be trimmed after space for the specified column. The above commands will produce the output as follows:

Linux Cut Filter2

From the above output, our delimiter is space; hence we have used (' ') after (-d). Command "cut -d ' ' -f2 exm.txt" displays column 2, command "cut -d ' ' -f5 exm.txt" displays column 5.

Cut by byte

The '-b' option is used to cut a section of line by byte. To cut a file by its byte position, execute the command as follows:

Consider the below command:

The above command will cut the line by a specified byte position. Consider the below output:

Linux Cut Filter

Cut by Character

The '-c' option is used to cut a specific section by character. However, these character arguments can be a number or a range of numbers, a list of comma-separated numbers, or any other character.

To cut by specified character, execute the command as follows:

Consider the below commands:

The above commands will cut the line by the specified characters. Consider the below output:

Linux Cut Filter

From the above output, we can see the first command is cutting the first and sixth character from each line, and the second command is cutting the first to the third character from each line.

Cut by Complement Pattern

The '--complement' option is used to cut by the complement. This option is supported by the BSD version of the cut. To cut by the complement pattern, execute the command as follows:

The compliment pattern can be a list of bytes, characters, or fields.

Consider the below command:

The above command will cut the file by the first character. Consider the below output:

Linux Cut Filter
Next TopicLinux grep





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA