
The Linux cut
command lets you extract portions of text from files or data streams. It’s especially useful for working with delimited data, such as CSV files. Here’s what you need to know.
The cut Command
The cut
command is a veteran of the Unix world, making its debut in 1982 as part of AT&T System III UNIX. Its purpose in life is to snip out sections of text from files or streams, according to the criteria that you set. Its syntax is as simple as its purpose, but it is this joint simplicity that makes it so useful.
RELATEDWhat's the Difference Between Linux and BSD?
In the time-honored UNIX way, by combining cut
with other utilities such as grep you can create elegant and powerful solutions to challenging problems. While there are different versions of cut
, we’re going to discuss the standard GNU/Linux version. Be aware that other versions, notably the cut
found in BSD variants, don’t include all the options described here.
You can check which version is installed on your computer by issuing this command:
cut --version
If you see “GNU coreutils” in the output you’re on the version we’re going to describe in this article. All versions of cut
have some of this functionality, but the Linux version has had enhancements added to it.
First Steps With cut
Whether we’re piping information into cut
or using cut
to read a file, the commands we use are the same. Anything you can do to a stream of input with cut
can be done on a line of text from a file, andvice versa. We can tell cut
to work with bytes, characters, or delimited fields.
To select a single byte, we use the -b
(byte) option and tell cut
which byte or bytes we want. In this case, it is byte five. We’re sending the string “how-to geek” into the cut
command with a pipe, “|”, from echo
.
echo 'how-to geek' | cut -b 5
The fifth byte in that string is “t”, so cut
responds by printing “t” in the terminal window.
To specify arangewe use a hyphen. To extract bytes 5 through to—and including—11, we’d issue this command:
echo 'how-to geek' | cut -b 5-11
You can supply multiple single bytes or ranges by separating them with commas. To extract byte 5 and byte 11, use this command:
echo 'how-to geek' | cut -b 5,11
To get the first letter of each word we can use this command:
echo 'how-to geek' | cut -b 1,5,8
If you use the hyphen without afirstnumber, cut
returns everything from position 1 up to the number. If you use the hyphen without asecondnumber, cut
returns everything from the first number to the end of the stream or line.
echo 'how-to geek' | cut -b -6
echo 'how-to geek' | cut -b 8-
Using cut With Characters
Using cut
with characters is pretty much the same as using it with bytes. In both cases, special care must be taken with complex characters. By using the -c
(character) option, we tell cut
to work in terms of characters, not bytes.
echo 'how-to geek' | cut -c 1,5,8
echo 'how-to geek' | cut -c 8-11
These work exactly as you’d expect. But take a look at this example. It’s a six-letter word, so asking cut
to return the characters from one to six should return the entire word. But it doesn’t. It’s one character short. To see the whole word we have to ask for the characters from one to seven.
echo 'piñata' | cut -c 1-6
echo 'piñata' | cut -c 1-7
The issue is the character “ñ” is actually made up out of two bytes. We can see this quite easily. We’ve got a short text file containing this line of text:
cat unicode.txt
We’ll examine that file with the hexdump
utility. Using the -C
(canonical) option gives us a table of hexadecimal digits with the ASCII equivalent on the right. In the ASCII table, the “ñ” isn’t shown, instead, there are dots representing two non-printable characters. These are the bytes highlighted in the hexadecimal table.
hexdump -C unicode.txt
These two bytes are used by the displaying program—in this case, the Bash shell—to identify the “ñ.” Many Unicode characters use three or more bytes to represent a single character.
If we ask for character 3 or character 4 we’re shown the symbol for a non-printing character. If we ask for bytes 3 and 4, the shell interprets them as “ñ.”
echo 'piñata' | cut -c 3
echo 'piñata' | cut -c 4
echo 'piñata' | cut -c 3-4
Using cut With Delimited Data
We can ask cut
to split lines of text using a specified delimiter. By default, cut uses a tab character but it is easy to tell it to use whatever we want. The fields in the “/etc/passwd” file are separated by colons “:”, so we’ll use that as our delimiter and extract some text.
The portions of text between the delimiters are calledfields, and are referenced just like bytes or characters, but they’re preceded by the -f
(fields) option. You can leave a space between the “f” and the digit, or not.
The first command uses the -d
(delimiter) option to tell cut to use “:” as the delimiter. It’s going to pull the first field out of each line in the “/etc/passwd” file. That’ll be a long list so we’re using head
with the -n
(number) option to show the first five responses only. The second command does the same thing but uses tail
to show us the last five responses.
cut -d':' -f1 /etc/passwd | head -n 5
cut -d':' -f2 /etc/passwd | tail -n 5
To extract a selection of fields, list them as a comma-separated list. This command will extract fields one to three, five, and six.
cut -d':' -f1-3,5,6 /etc/passwd | tail -n 5
By including grep
in the command, we can look for lines that include “/bin/bash.” The means we can list only those entries that have Bash as their default shell. That’ll usually be the “normal” user accounts. We’ll ask for fields from one to six because the seventh field is the default shell field and we already know what that is—we’re searching for it.
grep "/bin/bash" /etc/passwd | cut -d':' -f1-6
Another way to include all fields apart from one is to use the --complement
option. This inverts the field selection and shows everything thathasn’tbeen requested. Let’s repeat the last command but only ask for field seven. Then we’ll run that command again with the --complement
option.
grep "/bin/bash" /etc/passwd | cut -d':' -f7
grep "/bin/bash" /etc/passwd | cut -d':' -f7 --complement
The first command finds a list of entries, but field seven gives us nothing to distinguish between them, so we don’t know who the entries refer to. In the second command, by adding the --complement
option we get everything except field seven.
Piping cut Into cut
Sticking with the “/etc/passwd” file, let’s extract field five. This is the actual name of the user who owns the user account.
grep "/bin/bash" /etc/passwd | cut -d':' -f5
The fifth field has subfields separated by commas. They’re rarely populated so they show up as a line of commas.
We can remove the commas by piping the output of the previous command into another invocation of cut
. The second instance of cut
uses the comma “,” as its delimiter. The -s
(only delimited) option tells cut
to suppress results that don’t have the delimiter in them at all.
grep "/bin/bash" /etc/passwd | cut -d':' -s -f5 | cut -d',' -s -f1
Because the root entry doesn’t have comma subfields in the fifth field it is suppressed, and we get the results we’re after—a list of the names of the “real” users configured on this computer.
RELATED: How Do Linux File Permissions Work?
The Output Delimiter
We’ve got a small file with some Comma Separated Values in them. The fields in this dummy data are:
- ID: A database ID number
- First: The first name of the subject.
- Last: The last name of the subject.
- email: Their email address.
- IP Address: Their IP Address.
- Brand: The brand of motor vehicle they drive.
- Model: The model of motor vehicle they drive.
- Year: The year their motor vehicle was built.
cat small.csv
If we tell cut to use the comma as the delimiter we can extract fields just like we did before. Sometimes you’ll have a requirement to extract data from a file, but you don’t want to have the field delimiter included in the results. Using the --output-delimiter
we can tell cut what character—or in fact, charactersequence—to use instead of the actual delimiter.
cut -d ',' -f 2,3 small.csv
cut -d ',' -f 2,3 small.csv --output-delimiter=' '
The second command tells cut
to replace the commas with spaces.
We can take this further and use this feature to convert the output to a vertical list. This command uses a new line character as the output delimiter. Note the “$” that we need to include to have the newline character acted upon, and not interpreted as a literal sequence of two characters.
We’ll use grep
to filter out the entry for Morgana Renwick, and ask cut
to print all fields from field two to the end of the record, and to use a newline character as the output delimiter.
grep 'renwick' small.csv | cut -d ',' -f2- --output-delimiter=$''
An Oldie but Goldie
At the time of writing, the little cut command is approaching its 40th birthday, and we’re still using it and writing about it today. I suppose cutting up text today is the same as it was 40 years ago. That is, a lot easier when you have the right tool to hand.
RELATED: 37 Important Linux Commands You Should Know
READ NEXT
- › How to Manipulate Strings in Bash on Linux
- › How to Parse CSV Data in Bash
- › How to Use the wc Command in Linux
- › How to List Users in Linux
- › This 55-Inch TCL Roku TV Is a Steal at $370
- › Save Time in Outlook With These 6 Custom Quick Step Examples
- › iOS 16.3 Lets You Lock Your Apple ID With a Physical Key
- › How to Count Characters in Word
FAQs
How is cut command used in Linux? ›
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.
How do you use cut command? ›Use the cut command to write selected bytes, characters, or fields from each line of a file to standard output. This displays the login name and full user name fields of the system password file. These are the first and fifth fields ( -f1,5 ) separated by colons ( -d: ).
How to cut a file in Linux? ›Each line of the file can be sliced and the required data can be retrieved easily by using `cut` command. It can also be used to cut text by delimiter or character or byte position. This command has many options to cut data from the text or file in different ways.
How do I cut a word in Linux? ›-c (column): To cut by character use the -c option.
This selects the characters given to the -c option. This can be a list of numbers separated comma or a range of numbers separated by hyphen(-).
For cut(move and rename)
The correct commands are mv and cp. You can create alias if you want and make copy respond to cp for example.
To split a file into pieces, you simply use the split command. By default, the split command uses a very simple naming scheme. The file chunks will be named xaa, xab, xac, etc., and, presumably, if you break up a file that is sufficiently large, you might even get chunks named xza and xzz.
How do I cut a directory in Linux? ›linux you would use cp (copy) mv (move which acts like cut and paste).
How do I cut a specific column in Linux? ›You must specify either the -c option to cut by column or -f to cut by fields. (Fields are separated by tabs unless you specify a different field separator with -d. Use quotes (Section 27.12) if you want a space or other special character as the delimiter.)
How do you cut and paste in Unix? ›The command yy (yank yank) is used to copy a line. Move the cursor to the line you want to copy and then press yy. The p command paste a copied or cut content after the current line.
How do I cut a string in Linux? ›To cut by character use the -c option. This selects the characters given to the -c option. This can be a list of comma separated numbers, a range of numbers or a single number. Where your input stream is character based -c can be a better option than selecting by bytes as often characters are more than one byte.
How do I cut a string in bash? ›
In bash, a string can also be divided without using $IFS variable. The 'readarray' command with -d option is used to split the string data. The -d option is applied to define the separator character in the command like $IFS. Moreover, the bash loop is used to print the string in split form.
How to split a file in Linux? ›To split a file into small pieces and print what is being done, we use the --verbose option with the split command in the Linux system. To check more information and option with descriptions about the split command, we use the --help option with the split command as shown below.
How do I make text cuts? ›- Cut. Select Cut. or press Ctrl + X.
- Paste. Select Paste. or press Ctrl + V. Note: Paste only uses your most recently copied or cut item.
- Copy. Select Copy. or press Ctrl + C.
Copy: Ctrl+C. Cut: Ctrl+X. Paste: Ctrl+V.
How do I cut a file into parts? ›- Open the Zip file.
- Open the Tools tab.
- Click the Split Size dropdown button and select the appropriate size for each of the parts of the split Zip file. ...
- Now click Split Zip File (to the left of Split Size).
Move the cursor over the first character of the text you want to remove, and then press Ctrl-^ (the caret, Ctrl-Shift-6 ) to set the mark. Use the arrow keys to highlight the text you wish to cut, and then press Ctrl-k to cut the text. Be sure you got all of the text you wanted, including the last character.
How do you cut a word in terminal? ›ctrl-w: cuts/kills the text from the current cursor location through the beginning of the word. alt-d: cuts/kills the text from the current cursor location through the end of the word.
How do I split a line in a string? ›Split String at Newline
Split a string at a newline character. When the literal \n represents a newline character, convert it to an actual newline using the compose function. Then use splitlines to split the string at the newline character. Create a string in which two lines of text are separated by \n .
To break a single statement into multiple lines
Use the line-continuation character, which is an underscore ( _ ), at the point at which you want the line to break.
As in normal makefile syntax, a single logical recipe line can be split into multiple physical lines in the makefile by placing a backslash before each newline.
How do I cut a line in vi? ›
- Press CTRL+V to enter visual block mode.
- Move your cursor to the end of where you want to copy or cut.
- Press y to copy. Press d to cut.
- Move the cursor to where you want to paste your selection.
- Press P (uppercase) to paste before your cursor.
You should be using the command substitution syntax $(command) when you want to execute a command in script/command. name=$(echo "$filename" | cut -f 1 -d '. ')
How do you cut a delimiter in UNIX? ›Unix Cut by a delimiter
The tab character is the default delimiter for cut command. and "-f" option is used to cut by a delimiter. You can override delimiter by providing the "-d" option.
Use SED to display specific lines
The powerful sed command provides several ways of printing specific lines. The -n suppresses the output while the p command prints specific lines.
The cut command removes the selected data from its original position, while the copy command creates a duplicate; in both cases the selected data is kept in temporary storage (the clipboard). The data from the clipboard is later inserted wherever a paste command is issued.
What is cut and give examples? ›To cut something is to use a sharp tool to chop, sever, slice, or divide something. Cut has several different specific senses depending on the tool being used. For example, when you use scissors to cut hair, you are snipping pieces of hair off.
How do you cut and copy in Linux? ›You can copy files by right-clicking on the file and selecting "Copy", then going to a different directory and selecting "Paste". For my terminal friends, you can also perform file copy-paste operations without leaving the terminal. In a Linux-based terminal, you do this using the cp command.
What control is used for cut? ›Cut (Ctrl + X)
Ctrl + X lets you remove text in one area and paste that same text multiple times in other areas.
Cut: in the case of items inside a file, Cut deletes the content from the screen, but keeps it in memory (see below). Delete: in the case of items inside a file, Delete deletes an item from the screen without storing it in memory.
What is the example of cut? ›Example Sentences
The saw easily cuts through metal. She cut into the melon with a knife. I cut myself while shaving. I had a cut finger.
What is cut explain? ›
to break the surface of something, or to divide or make something smaller, using a sharp tool, especially a knife: to cut a slice of bread. I cut myself/my hand on that glass/with that knife. Cut the meat up into small pieces.
What is cut work? ›If you say that you will have your work cut out to do something, you mean that it will be a very difficult task. The new administration has its work cut out for it. [ + for]
How do I cut a folder in Linux? ›linux you would use cp (copy) mv (move which acts like cut and paste).
What are the commands for cut and paste? ›Select the text you want to copy and press Ctrl+C. Place your cursor where you want to paste the copied text and press Ctrl+V.
How do I delete and cut the current line? ›In normal mode, d is the key for cutting (deleting) text. Move the cursor to the desired position and press the d key, followed by the movement command. Here are some helpful deleting commands: dd - Delete (cut) the current line, including the newline character.