0
0
Bash Scriptingscripting~20 mins

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

Choose your learning style9 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.