Bash Cut Command with Examples – Linux Hint

Linux provides us with many commands and utilities that allow us to cut out pieces and parts of specific files and process and filter those files. “cut” is also a command line utility in Linux that allows us to cut specific parts and sections of files and display the data as standard output by printing it. We can use it to cut the parts of the files by delimiter, field, bytes, position and characters.

Cut breaks a line and pulls out the text it contains. It is mandatory to provide the options when typing the command; otherwise, it will throw an error. If we operate on multiple files by providing more than one file name, then the data in both files will not be preceded by their name. This command comes with a list of options which are as follows:

-f (-fields= LIST): Select using a field specified by the user. -b(-byte= LIST): Select using a

user-specified byte

. -c(-

character= LIST): Select

using a character specified by the user. -d(-delimiter):

Uses a default “TAB” delimiter but if we specify the delimiter using the option, it will use the specified delimiter

. –

complement: Instructs the “cut” command to execute all characters, bytes or fields instead of the selected part.

-s

(-only-delimited

): By default, lines that contain anchors are not printed. When you specify -s together with the cut command, you are instructed not to print lines that do not have a delimiter.

output-delimiter: It is specified to use the string as output. By default, the string acts as input.

-help: Used to view the help manual.

-version: Used to verify the version.

These are the options we already discussed above.

If we want to cut from a specific file, we will have to specify the name of the file at the end of our command. We can cut one or several bytes depending on our requirements.

Syntax:

The syntax for using the cut command is shown below, where any option can be any flag that is used to perform additional operations on the content to be cut from the file. We can use multiple options at once. The [File] parameter takes the name of the file from which we want to extract. If we do not specify the name of the file to our “cut” command, it will be read from the standard input and start working according to it. In that case

, this command will filter the “pipeline”

Example No. 1:

In this example, we’ll use the cut command to get a better understanding of how the cut command works. Let’s first check the version of the cut we have right now. For that, we will execute the command mentioned below.

To check the version of the cut, we will simply run the cut command along with the “version” indicator that we have studied above. As shown in the excerpt, in our case it is version 8.30.

Suppose we have a file in our home directory that stores the data of five students along with their “names

,” “grades,” and “grades.”

<img src

=”https://linuxhint.com/wp-content/uploads/2021/07/word-image-109831-2.png”

alt=”” />

Printing some bytes of fileNow, we will perform some actions on this file using the cut command. To print the bytes of the file, we will simply execute the command shown below.

When we try to execute the above command, it will give the output as you can see below in which the number of bytes starting from 1 to 6, which are the names of the students, was listed.

Printing

the byte range

As above, we can also print the multiple byte ranges. Suppose we want to print the name along with the students’ marks. For that, we will go from 1 to 6 and then from 14 to 24.

After running the above command, we will get the output shown below in which the name and total grades of the students are displayed.

Viewing content from the start or end position

To print the content from the specified starting point, we will simply run a single command mentioned below. Suppose we want to print the index character “6”.

As shown below, the characters from index 7 to the last index have been displayed in our terminal. This included students’ grades and grades.

Like the previous one, we can tell the cut command to print the content at the limited index. For that, we will execute the command mentioned in the next line.

When we execute this command, we will get the content from the beginning of index “1” until index “13” is reached. As shown below, in the output, name and grades are only displayed where student grades are excluded.

Using the Cut by Fields command

Now, we will try another file to cut the content using fields. For that, we will use the file “/etc/passwd”. Let’s check the details of the user who can access this system, for this we will run the cat command along with the file name as indicated below.

Below are the details of the users who can access the system.

Now, it contains all the details of the users. But we only want the names of the users, for that we will execute the following command.

After executing the previous command in which, we specify the cut command to print the first field of the file using the “:” delimiter.

<img src

=”https://linuxhint.com/wp-content/uploads/2021/07/word-image-109831-8.png” alt=”” />

Conclusion

Today we introduce you to the Linux command “cut” that is used to cut some part of the specified part of any file. After giving your overview, we explain the syntax and perform some operations of cutting the parts of the files to make it easier for you to understand the concept of the cut command.