Enhance your knowledge of Text Processing Commands in Shell Scripting with these 30 MCQs. Topics include cat, tac, head, tail, cut, paste, sort, uniq, and awk, sed basics.
MCQs on Text Processing Commands in Shell Scripting
cat, tac, head, tail
What does the cat command do in shell scripting? a) Concatenates and displays the content of files b) Deletes content from files c) Creates a new file d) Appends content to a file
What is the function of the tac command in shell scripting? a) Concatenates files in reverse order b) Displays the content of files c) Reverses the contents of a file d) Displays the content of files in reverse order
Which of the following commands will display the first 10 lines of a file? a) tail -n 10 filename b) head -n 10 filename c) cat filename | head d) head filename
Which option with the head command allows you to display a specific number of lines? a) -n b) -l c) -f d) -b
How does the tail command differ from head? a) It displays the last lines of a file b) It displays the first lines of a file c) It sorts the contents of a file d) It concatenates the content of files
To continuously monitor the end of a file, which tail option would you use? a) -f b) -n c) -r d) -s
What does the head -n 20 command do? a) Displays the first 20 lines of a file b) Displays the last 20 lines of a file c) Displays the first 20 characters of a file d) Displays the entire file content
Which command would you use to reverse the contents of a file? a) tac b) cat c) tail d) head
How can you display the last 10 lines of a file? a) tail -n 10 filename b) head -n 10 filename c) cat filename | tail d) tail filename
How can you display both the first and last 10 lines of a file in one command? a) head -n 10 filename && tail -n 10 filename b) cat filename | head -n 10 && tail -n 10 c) head filename | tail filename d) head -n 10 filename | tail -n 10
cut, paste, sort, uniq
What is the function of the cut command in shell scripting? a) To extract columns from a file b) To concatenate files c) To display lines matching a pattern d) To sort the lines of a file
Which option with cut command allows you to select specific columns? a) -c b) -f c) -d d) -n
What does the paste command do in shell scripting? a) Concatenates files b) Merges lines from files side by side c) Sorts the contents of a file d) Extracts data from a file
How do you use paste to join two files column-wise? a) paste file1.txt file2.txt b) paste file1.txt – file2.txt c) paste -s file1.txt file2.txt d) paste file1.txt | file2.txt
What is the sort command used for in shell scripting? a) To arrange lines in alphabetical or numerical order b) To remove duplicate lines c) To display specific lines d) To display the first and last lines
What is the default sorting order of the sort command? a) Descending b) Random c) Ascending d) Unsorted
What does the uniq command do in shell scripting? a) Removes duplicate lines from a file b) Sorts the contents of a file c) Joins two files together d) Displays unique lines of a file
Which of the following commands will remove consecutive duplicate lines from a file? a) sort filename | uniq b) uniq filename c) sort filename | grep -v duplicate d) cut filename | sort
Which option with the sort command will sort the contents in descending order? a) -n b) -r c) -d d) -f
Which sort option should you use to sort data numerically? a) -n b) -r c) -d d) -f
wc, nl, awk, sed (Basic Usage)
What does the wc command do in shell scripting? a) Counts the number of lines, words, and characters in a file b) Counts the number of occurrences of a word c) Sorts the contents of a file d) Displays the number of files in a directory
Which option with wc command counts the lines in a file? a) -l b) -w c) -c d) -s
What is the purpose of the nl command in shell scripting? a) To number the lines of a file b) To sort the lines of a file c) To display the contents of a file d) To concatenate files
What does awk do in shell scripting? a) Extracts and processes data from files b) Sorts files in reverse order c) Deletes duplicate lines d) Extracts the first line of a file
Which of the following is a basic awk command for printing the first column of a file? a) awk ‘{print $1}’ filename b) awk ‘{print $2}’ filename c) awk ‘{print $0}’ filename d) awk ‘{print $3}’ filename
What does the sed command do in shell scripting? a) Streams text and modifies it in place b) Sorts text files c) Joins multiple lines into one d) Counts words in a file
Which option with sed is used for replacing text in a file? a) -d b) -r c) -i d) -e
Which of the following commands will replace “apple” with “orange” in a file using sed? a) sed ‘s/apple/orange/’ filename b) sed ‘s/apple orange/’ filename c) sed ‘s/apple=orange/’ filename d) sed ‘replace apple with orange’ filename
Which awk command will print only the lines where the value in the second column is greater than 10? a) awk ‘$2 > 10’ filename b) awk ‘$1 > 10’ filename c) awk ‘$2 = 10’ filename d) awk ‘2 > 10’ filename
How can you use sed to delete the first line of a file? a) sed ‘1d’ filename b) sed ‘d1’ filename c) sed ‘delete 1’ filename d) sed ‘remove 1’ filename
Answer Key
Qno
Answer (Option with Text)
1
a) Concatenates and displays the content of files
2
d) Displays the content of files in reverse order
3
b) head -n 10 filename
4
a) -n
5
a) It displays the last lines of a file
6
a) -f
7
a) Displays the first 20 lines of a file
8
a) tac
9
a) tail -n 10 filename
10
a) head -n 10 filename && tail -n 10 filename
11
a) To extract columns from a file
12
b) -f
13
b) Merges lines from files side by side
14
a) paste file1.txt file2.txt
15
a) To arrange lines in alphabetical or numerical order
16
c) Ascending
17
a) Removes duplicate lines from a file
18
a) sort filename
19
b) -r
20
a) -n
21
a) Counts the number of lines, words, and characters in a file