Bird
Raised Fist0
Linux CLIscripting~10 mins

cut (extract columns) in Linux CLI - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - cut (extract columns)
Input text or file
↓
Specify delimiter or default tab
↓
Specify fields or character positions
↓
cut extracts specified columns
↓
Output extracted columns
The cut command reads input, uses a delimiter to split columns, extracts specified columns, and outputs them.
Execution Sample
Linux CLI
echo "apple,banana,cherry" | cut -d',' -f2
Extracts the second column (banana) from a comma-separated string.
Execution Table
StepInputDelimiterFieldsActionOutput
1"apple,banana,cherry"','2Split input by ','["apple", "banana", "cherry"]
2["apple", "banana", "cherry"]','2Extract field 2"banana"
3"banana"N/AN/APrint outputbanana
💡 All specified fields extracted and printed; command ends.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
input_line"apple,banana,cherry""apple,banana,cherry""apple,banana,cherry""apple,banana,cherry"
delimitertab (default)','','','
fieldsnone222
split_fieldsnone["apple", "banana", "cherry"]["apple", "banana", "cherry"]["apple", "banana", "cherry"]
outputnonenone"banana""banana"
Key Moments - 3 Insights
Why does cut use a delimiter and what happens if I don't specify one?
cut uses the delimiter to know where to split the input into columns. If you don't specify one, it uses the tab character by default (see execution_table step 1).
What does the -f2 option mean in the cut command?
-f2 tells cut to extract the second field (column) after splitting by the delimiter, as shown in execution_table step 2.
Can cut extract multiple columns at once?
Yes, you can specify multiple fields like -f1,3 to extract first and third columns. This example shows a single field for simplicity.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output after step 2?
A"banana"
B"apple"
C"cherry"
D"apple,banana,cherry"
💡 Hint
Check the 'Output' column in execution_table row for step 2.
At which step does cut split the input string into fields?
AStep 2
BStep 1
CStep 3
DNo splitting occurs
💡 Hint
Look at the 'Action' column in execution_table to find when splitting happens.
If the delimiter was not specified, what delimiter would cut use by default?
AComma ','
BSpace ' '
CTab '\t'
DSemicolon ';'
💡 Hint
Check variable_tracker for the initial delimiter value.
Concept Snapshot
cut command extracts columns from text.
Use -d to set delimiter (default is tab).
Use -f to specify which columns to extract.
Example: cut -d',' -f2 extracts second column from comma-separated input.
Outputs only the selected columns.
Full Transcript
The cut command takes input text or a file and splits it into columns using a delimiter. By default, the delimiter is a tab character. You specify which columns to extract using the -f option. For example, echo "apple,banana,cherry" | cut -d',' -f2 splits the input by commas and extracts the second column, which is "banana". The command outputs only the selected columns. This process involves reading the input, splitting it by the delimiter, extracting the requested fields, and printing them. If no delimiter is specified, cut assumes tab. You can extract multiple columns by listing them with commas in the -f option.

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

  1. Step 1: Understand the purpose of cut

    The cut command is designed to extract sections from each line of input, usually columns or fields.
  2. Step 2: Compare options

    Options A, C, and D describe other commands like rm, cp, and chmod, not cut.
  3. Final Answer:

    Extracts specific columns or fields from each line of a file or input -> Option D
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    cut -d ',' -f 2,4 filename -> Option A
  4. 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:
apple,banana,cherry,date
red,yellow,red,brown
sweet,sweet,tart,sweet

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

  1. Step 1: Identify the delimiter and field number

    The delimiter is a comma, and the command extracts the 3rd field from each line.
  2. Step 2: Extract the 3rd field from each line

    Line 1: 'cherry'
    Line 2: 'red'
    Line 3: 'tart'
  3. Final Answer:

    cherry red tart -> Option C
  4. 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

  1. Step 1: Check possible causes for no output

    cut produces no output if there are no lines to process.
  2. 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.
  3. Final Answer:

    The file is empty -> Option A
  4. 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?
hard
A. cut -f 1,3 records.txt | grep -v '^\t$' | cut -f 3
B. awk -F '\t' '$3 != "" {print $1, $3}' records.txt
C. cut -f 1,3 records.txt | grep -v '^\t$'
D. cut -f 1,3 records.txt | grep '^\t$'

Solution

  1. Step 1: Understand cut limitation

    cut extracts columns but cannot filter lines based on content.
  2. Step 2: Use a tool that filters lines by field content

    awk can check if the 3rd field is not empty and print the 1st and 3rd fields.
  3. Step 3: Analyze options

    awk -F '\t' '$3 != "" {print $1, $3}' records.txt uses awk with tab delimiter and condition $3 != "", printing fields 1 and 3 correctly.
  4. Final Answer:

    awk -F '\t' '$3 != "" {print $1, $3}' records.txt -> Option B
  5. Quick Check:

    Use awk to filter and extract columns [OK]
Hint: Use awk for filtering and extracting columns together [OK]
Common Mistakes:
  • Trying to filter lines using cut alone
  • Using grep incorrectly to filter empty fields
  • Not specifying correct delimiter in awk
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard