When you are working with large text files, server logs, or CSV data in the Linux terminal, you rarely need to look at every single piece of information on a line. For example, if you have a log file containing timestamps, IP addresses, and error codes separated by spaces, you might only want to extract the IP addresses. Instead of manually reading through the file, you can use the cut command to instantly slice the file vertically, extracting only the specific columns or characters you need and discarding the rest.
Extracting Data by Delimiter (Columns)
The most common and powerful way to use cut is by identifying a “delimiter”—a specific character that separates the data into columns. This could be a comma (in a CSV file), a colon (in system files like /etc/passwd), or a space.
To use this method, you combine the -d (delimiter) flag with the -f (field) flag.
For example, to read a standard comma-separated values (CSV) file and extract only the third column of data:
cut -d ',' -f 3 data.csv
In this command, -d ',' tells cut that commas separate the columns, and -f 3 tells it to print only the third column to the screen.
If you need to extract the first and the fourth column simultaneously, you can list multiple fields separated by a comma:
cut -d ',' -f 1,4 data.csv
Extracting Data by Character Position
Sometimes, data is not separated by a clean delimiter; instead, it is formatted with a fixed width, where the information you need always starts at a specific character count. In these cases, you use the -c (character) flag.
To extract only the first 10 characters of every line in a file:
cut -c 1-10 document.txt
You can also use an open-ended range. If you want to strip off the first 5 characters of a line and print everything else until the end of the line, you leave the second number blank:
cut -c 6- document.txt
Piping cut with Other Commands
cut is exceptionally useful when chained together with other commands using the pipe (|) operator. For example, if you run the ls -l command to list files in a directory, it outputs a lot of information (permissions, owners, sizes, dates, names). If you want to extract just the file sizes (which is usually the 5th column separated by spaces), you can pipe the output directly into cut.
Note: Since ls -l uses a variable number of spaces to align columns, you often have to use the tr command to squeeze multiple spaces into a single space before cutting.
ls -l | tr -s ' ' | cut -d ' ' -f 5
This pipeline takes the raw directory listing, normalizes the spaces, and then extracts exactly the column you requested.