Linux and other UNIX-like operating systems have many tools for processing text on the command line. Without these tools (find complete Linux command list here), we would be forced to write down the output of one command and type it into the next so we could isolate the bits we actually need and not pipe the entire output into an unexpecting command. Thecutcommand allows us to cut out the pieces we need so we can pipe them along into the intended commands.

Thecutcommand, also part of the POSIX compliance requirements, is very useful for a few different cases. It’s not as robust as some of the other commands, but it’s very useful in its own right. Thecutcommand is used for, as the name suggests, cutting text. Most commonly, I believe, it is used for cutting information out of textual tables, such as a CSV file or the output ofps.

6 Year Lifecycle linux kernel lts cycle

How to use cut command in Linux?

The general syntax ofcutis as follows.

It’s not super helpful in that form, but we’ll cover a couple of the options that are commonly used.

First, we’ll take a look at cutting out text from a table using tabs between the columns.

This command will pull columns one and five, the user ID and user description fields, from the/etc/passwdfile. Here we’ve used the-fflag to indicate the fields we want. This option accepts comma separated values or ranges in the form ofx-y, wherexis the lower bound andythe upper.

Additionally, you can combine these options as done below. The-doption is for indicating the delimiter, or the separator, value. Different files use different values for this. The/etc/passwd file uses a colon, but a csv file uses commas, so it’s very useful to be able to specify a delimiter other than the default of tab.

This will provide fields one through three, as well as the seventh.

NOTE: The output of some programs isn’t always uniform and will sometimes have blank fields which will causecutto behave unexpectedly. An example of this is using spaces for padding instead of tab characters. This is not uncommon. you’re able to sometimes deal with this by piping the command output throughtrbeforecut.

Thetrcommand is the transposition command. It lets you easily swap characters in a file, much likesed, but not nearly as flexible. Thetrcommand’s-s (or –squeeze)option squeezes all white space into a single space character. This is very effective when used in combination withcutbecause it provides much more consistent delimiting of tabular data.

Sometimes you want to be able to change the delimiter from that used in the input file, and that’s what the–output-delimiteroption is used for.

Here we have specified tab as the delimiter. Because we can’t easily pass a tab character(pressing tab typically tells bash to attempt autocompletion), we use$’\t’to pass a tab character. This sequence tells bash to escape what is in the quotations and\tis the standard escape sequence for a tab character.

And those are the basics of usingcut. There are some more features, but you’re not as likely to use them in day-to-day work.

Let us know if there are any tools you would like covered or if there are any you would like covered in further depth in particular.

Also Read:The Ultimate A To Z List of Linux Commands