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. The cut command allows us to cut out the pieces we need so we can pipe them along into the intended commands.
The cut command, 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. The cut command 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 of ps.
How to use cut command in Linux?
The general syntax of cut is as follows.
cut OPTION... [FILE]...
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.
cut -f 1,5 -d ':'/etc/passwd
This command will pull columns one and five, the user ID and user description fields, from the /etc/passwd file. Here we’ve used the -f flag to indicate the fields we want. This option accepts comma separated values or ranges in the form of x-y, where x is the lower bound and y the upper.
Find your dream job
Additionally, you can combine these options as done below. The -d option 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.
cut -f 1-3,7 -d ':'/etc/passwd
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 cause cut to behave unexpectedly. An example of this is using spaces for padding instead of tab characters. This is not uncommon. You can sometimes deal with this by piping the command output through tr before cut.
tr -s ''''
The tr command is the transposition command. It lets you easily swap characters in a file, much like sed, but not nearly as flexible. The tr command’s -s (or –squeeze) option squeezes all white space into a single space character. This is very effective when used in combination with cut because 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-delimiter option is used for.
cut -f 1,5 -d ':' --output-delimiter=$'\t' /etc/passwd
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 \t is the standard escape sequence for a tab character.
And those are the basics of using cut. 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