📖 Scenario: You have a text file containing a list of employees with their details separated by commas. You want to extract only specific columns to see just the names and departments.
🎯 Goal: Learn how to use the cut command to extract specific columns from a comma-separated file.
📋 What You'll Learn
Create a file named employees.txt with exact content
Use a variable to store the delimiter
Use the cut command with the delimiter variable to extract columns
Display the extracted columns output
💡 Why This Matters
🌍 Real World
Extracting specific fields from CSV or text files is common in data processing and system administration.
💼 Career
Knowing how to use cut helps in log analysis, report generation, and quick data extraction tasks in many IT roles.
Progress0 / 4 steps
1
Create the employees.txt file
Create a file named employees.txt with the following exact content: John,Doe,Accounting,50000 Jane,Smith,Marketing,60000 Emily,Jones,IT,55000
Linux CLI
Hint
Use echo -e with newline characters \n to create multiple lines in the file.
2
Set the delimiter variable
Create a variable called DELIM and set it to a comma , to use as the delimiter.
Linux CLI
Hint
Use DELIM=, to assign the comma character to the variable.
3
Extract first and third columns using cut
Use the cut command with the delimiter stored in DELIM to extract the first and third columns from employees.txt. Use -d "$DELIM" and -f 1,3 options.
Linux CLI
Hint
Use cut -d "$DELIM" -f 1,3 employees.txt to extract the first and third columns.
4
Display the extracted columns output
Run the script to display the output of the cut command showing only the first and third columns.
Linux CLI
Hint
The output should show only the first and third columns separated by commas.
Practice
(1/5)
1. What does the cut command do in Linux?
easy
A. Deletes files from the system
B. Changes file permissions
C. Copies files from one directory to another
D. Extracts specific columns or fields from each line of a file or input
Solution
Step 1: Understand the purpose of cut
The cut command is designed to extract sections from each line of input, usually columns or fields.
Step 2: Compare options
Options A, C, and D describe other commands like rm, cp, and chmod, not cut.
Final Answer:
Extracts specific columns or fields from each line of a file or input -> Option D
Quick Check:
cut extracts columns [OK]
Hint: Remember: cut slices columns, not files [OK]
Common Mistakes:
Confusing cut with copy or delete commands
Thinking cut changes file content permanently
Assuming cut works on whole files, not lines
2. Which of the following is the correct syntax to extract the 2nd and 4th columns from a comma-separated file using cut?
easy
A. cut -d ',' -f 2,4 filename
B. cut -d ',' -f 2 4 filename
C. cut -f 2 4 -d ',' filename
D. cut -f 2-4 -d ',' filename
Solution
Step 1: Understand cut syntax for fields and delimiter
The option -f specifies fields, and multiple fields must be comma-separated without spaces. The option -d sets the delimiter.
Step 2: Identify correct order and format
The correct syntax places -d ',' before -f 2,4 or vice versa, but fields must be comma-separated. cut -d ',' -f 2,4 filename correctly uses -d ',' -f 2,4.
Final Answer:
cut -d ',' -f 2,4 filename -> Option A
Quick Check:
Fields comma-separated, delimiter set correctly [OK]
Hint: Use commas between fields and set delimiter with -d [OK]
Common Mistakes:
Separating fields with spaces instead of commas
Mixing order of options incorrectly
Omitting the delimiter option for non-tab files
3. Given a file data.txt with the following content:
What is the output of the command cut -d ',' -f 3 data.txt?
medium
A. date
brown
sweet
B. banana
yellow
sweet
C. cherry
red
tart
D. apple
red
sweet
Solution
Step 1: Identify the delimiter and field number
The delimiter is a comma, and the command extracts the 3rd field from each line.
Step 2: Extract the 3rd field from each line
Line 1: 'cherry' Line 2: 'red' Line 3: 'tart'
Final Answer:
cherry
red
tart -> Option C
Quick Check:
3rd column extracted correctly [OK]
Hint: Count fields separated by delimiter, pick correct number [OK]
Common Mistakes:
Counting fields starting at zero instead of one
Using wrong delimiter
Extracting wrong field number
4. You run the command cut -f 1,3 -d ':' file.txt but get no output. What is the most likely reason?
medium
A. The file is empty
B. The file uses spaces instead of ':' as delimiter
C. The fields 1 and 3 do not exist in the file
D. The cut command does not support multiple fields
Solution
Step 1: Check possible causes for no output
cut produces no output if there are no lines to process.
Step 2: Evaluate the most likely cause
A wrong delimiter produces modified output (whole lines with tabs), not empty. Fields 1 always exists, multiple fields supported. Empty file causes truly no output.
Final Answer:
The file is empty -> Option A
Quick Check:
Empty file -> no output [OK]
Hint: Verify input file has content before debugging [OK]
Common Mistakes:
Assuming delimiter mismatch causes no output
Thinking cut fails if fields missing
Believing cut can't handle multiple fields
5. You have a file records.txt with tab-separated values. You want to extract the 1st and 3rd columns but only if the 3rd column is not empty. Which command correctly does this?