Bird
Raised Fist0
Bash Scriptingscripting~20 mins

cut and paste in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Cut and Paste Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of cut command with delimiter and field
What is the output of this command when run on the file data.txt containing:
apple,banana,cherry
dog,elephant,fox

Command:
cut -d',' -f2 data.txt
Bash Scripting
cut -d',' -f2 data.txt
A
cherry
fox
B
apple
dog
C
banana
elephant
D
apple,banana,cherry
dog,elephant,fox
Attempts:
2 left
💡 Hint
The -d option sets the delimiter, and -f selects the field number.
💻 Command Output
intermediate
2:00remaining
Paste command combining two files
Given two files:
file1.txt:
1
2
3

file2.txt:
a
b
c

What is the output of:
paste file1.txt file2.txt
Bash Scripting
paste file1.txt file2.txt
A
1
2
3
a
b
c
B
1  a
2  b
3  c
C
1a
2b
3c
D
1	a
2	b
3	c
Attempts:
2 left
💡 Hint
Paste joins lines from files side by side separated by tabs by default.
📝 Syntax
advanced
2:00remaining
Correct cut command to extract characters
Which option correctly extracts characters 3 to 5 from each line of input.txt?
Acut -f3-5 input.txt
Bcut -c3-5 input.txt
Ccut -d3-5 input.txt
Dcut -c3,5 input.txt
Attempts:
2 left
💡 Hint
Use -c to select character positions.
💻 Command Output
advanced
2:00remaining
Output of paste with custom delimiter
Given two files:
names.txt:
John
Jane

ages.txt:
30
25

What is the output of:
paste -d ':' names.txt ages.txt
Bash Scripting
paste -d ':' names.txt ages.txt
A
John:30
Jane:25
B
John 30
Jane 25
C
John:Jane
30:25
D
John
Jane
30
25
Attempts:
2 left
💡 Hint
The -d option sets the delimiter between pasted columns.
🚀 Application
expert
3:00remaining
Combine cut and paste to swap columns
You have a file data.csv with comma-separated values:
red,apple
blue,berry
green,grape

Which command sequence swaps the two columns so output is:
apple,red
berry,blue
grape,green
Apaste -d',' <(cut -d',' -f2 data.csv) <(cut -d',' -f1 data.csv)
Bcut -d',' -f2 data.csv | paste -d',' - <(cut -d',' -f1 data.csv)
Ccut -d',' -f2 data.csv > temp1; cut -d',' -f1 data.csv > temp2; paste -d',' temp1 temp2
Dcut -d',' -f1 data.csv | paste -d',' - <(cut -d',' -f2 data.csv)
Attempts:
2 left
💡 Hint
Use process substitution to cut columns and paste them in swapped order.

Practice

(1/5)
1. What does the cut command do in bash scripting?
easy
A. Deletes lines containing a specific pattern
B. Extracts specific parts of each line from a file or input
C. Joins lines from multiple files side by side
D. Counts the number of lines in a file

Solution

  1. Step 1: Understand the purpose of cut

    The cut command is used to extract sections from each line of input, usually by specifying fields or characters.
  2. Step 2: Compare with other commands

    paste joins lines side by side, grep deletes or filters lines, and wc counts lines. So, only cut extracts parts of lines.
  3. Final Answer:

    Extracts specific parts of each line from a file or input -> Option B
  4. Quick Check:

    cut = Extract parts [OK]
Hint: Remember: cut slices parts, paste sticks lines side by side [OK]
Common Mistakes:
  • Confusing cut with paste
  • Thinking cut deletes lines
  • Assuming cut counts lines
2. Which of the following is the correct syntax to extract the 2nd field from a colon-separated file using cut?
easy
A. cut -f 2 -d ':' filename
B. cut -d 2 -f ':' filename
C. cut -c 2 filename
D. cut -f ':' -d 2 filename

Solution

  1. Step 1: Identify correct option flags for cut

    The -f flag specifies the field number, and -d specifies the delimiter character.
  2. Step 2: Match syntax to extract 2nd field with ':' delimiter

    cut -f 2 -d ':' filename uses -f 2 and -d ':', which is the correct order and syntax.
  3. Final Answer:

    cut -f 2 -d ':' filename -> Option A
  4. Quick Check:

    Fields (-f) then delimiter (-d) [OK]
Hint: Use -f for field, -d for delimiter in cut [OK]
Common Mistakes:
  • Swapping -f and -d flags
  • Using -c instead of -f for fields
  • Putting delimiter as field number
3. Given two files, file1.txt contains:
apple banana cherry
and file2.txt contains:
red yellow red
What is the output of paste file1.txt file2.txt?
medium
A. appleyellow bananared cherryred
B. apple banana cherry red yellow red
C. apple red banana yellow cherry red
D. apple,red banana,yellow cherry,red

Solution

  1. Step 1: Understand paste command behavior

    paste joins lines from two files side by side separated by a tab by default.
  2. Step 2: Combine lines from both files line by line

    Line 1: apple + tab + red
    Line 2: banana + tab + yellow
    Line 3: cherry + tab + red
  3. Final Answer:

    apple red banana yellow cherry red -> Option C
  4. Quick Check:

    paste joins lines side by side [OK]
Hint: paste joins lines horizontally, not vertically [OK]
Common Mistakes:
  • Thinking paste stacks lines vertically
  • Assuming paste adds commas
  • Ignoring tab separator default
4. What is wrong with this command to extract the first 3 characters of each line?
cut -c 1-3 file.txt
It returns an error. Why?
medium
A. The command is correct; error likely from missing file
B. The range should be specified as 1,3 not 1-3
C. The -c option does not accept ranges
D. The syntax is correct; error is from file permissions

Solution

  1. Step 1: Check cut syntax for character ranges

    cut -c 1-3 is the correct syntax to extract characters 1 to 3.
  2. Step 2: Consider other reasons for error

    If an error occurs, it is likely due to the file not existing or permission issues, not syntax.
  3. Final Answer:

    The command is correct; error likely from missing file -> Option A
  4. Quick Check:

    cut -c 1-3 is valid syntax [OK]
Hint: Check file existence before blaming cut syntax [OK]
Common Mistakes:
  • Thinking cut -c doesn't accept ranges
  • Using commas instead of dash for ranges
  • Ignoring file existence or permissions
5. You have two files:
names.txt with:
John Jane Doe
and ages.txt with:
25 30
You want to create a file combined.txt where each line has name and age separated by a comma. Missing ages should show as 'N/A'. Which command sequence achieves this?
hard
A. paste -d ',' names.txt ages.txt > combined.txt
B. paste names.txt ages.txt | sed 's/$/,N\/A/' > combined.txt
C. cut -d ',' -f 1 names.txt > combined.txt
D. paste -d ',' names.txt ages.txt | sed 's/,$/,N\/A/' > combined.txt

Solution

  1. Step 1: Use paste with comma delimiter

    paste -d ',' names.txt ages.txt joins lines with a comma.
  2. Step 2: Handle missing ages with sed

    Lines missing age end with a comma only, so sed 's/,$/,N\/A/' replaces trailing comma with ',N/A'.
  3. Step 3: Redirect output to combined.txt

    Using > combined.txt saves the result to the file.
  4. Final Answer:

    paste -d ',' names.txt ages.txt | sed 's/,$/,N\/A/' > combined.txt -> Option D
  5. Quick Check:

    paste + sed fixes missing fields [OK]
Hint: Use paste with -d and sed to fix missing fields [OK]
Common Mistakes:
  • Not handling missing age lines
  • Using cut instead of paste for joining
  • Forgetting to redirect output to file
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