0
0
Linux CLIscripting~5 mins

cut (extract columns) in Linux CLI - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you have a file or output with many columns, and you only want to see or use a few specific columns. The cut command helps you quickly extract those columns by specifying their positions or delimiters.
When you want to see only the usernames from a list of users with many details.
When you have a CSV file and want to extract just the email addresses column.
When you want to grab the first few characters from each line of a log file.
When you want to split a line of text by a specific character and get one part.
When you want to quickly filter out unnecessary columns from command output.
Commands
This command takes a line of text with fruits separated by commas, then extracts the second fruit using comma as the delimiter.
Terminal
echo "apple,banana,cherry" | cut -d',' -f2
Expected OutputExpected
banana
-d - Specifies the delimiter character to split the input line.
-f - Selects the field (column) number(s) to extract.
This extracts the first 5 characters from each line of the /etc/passwd file and shows only the first 3 lines to keep output short.
Terminal
cut -c1-5 /etc/passwd | head -3
Expected OutputExpected
root: daemo bin:
-c - Selects characters by position instead of fields.
This extracts the 1st and 3rd tab-separated fields from the /etc/passwd file and shows the first 3 lines.
Terminal
cut -f1,3 -d$'\t' /etc/passwd | head -3
Expected OutputExpected
root:0 daemon:1 bin:2
-f - Selects multiple fields separated by commas.
-d - Specifies the delimiter, here a tab character.
Key Concept

If you remember nothing else from cut, remember: use -d to set the delimiter and -f to pick the columns you want.

Common Mistakes
Not specifying the delimiter when the input is not tab-separated.
cut defaults to tab delimiter, so it won't split correctly if your data uses commas or other characters.
Always use -d with the correct delimiter character matching your input.
Using -f with character positions instead of fields.
-f works on fields separated by delimiter, not on character positions, so it won't extract the right parts.
Use -c to select characters by position, and -f to select fields by delimiter.
Trying to extract fields from input that has no delimiters.
Without delimiters, cut treats the whole line as one field, so -f won't split anything.
Make sure your input has the delimiter you specify, or use -c for character positions.
Summary
Use cut with -d to set the delimiter and -f to select columns (fields).
Use cut with -c to select characters by position instead of fields.
Always check your input format to choose the right delimiter and flags.