MCQs on Text Processing Commands | Shell Scripting

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

  1. 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
  2. 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
  3. 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
  4. Which option with the head command allows you to display a specific number of lines?
    a) -n
    b) -l
    c) -f
    d) -b
  5. 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
  6. To continuously monitor the end of a file, which tail option would you use?
    a) -f
    b) -n
    c) -r
    d) -s
  7. 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
  8. Which command would you use to reverse the contents of a file?
    a) tac
    b) cat
    c) tail
    d) head
  9. 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
  10. 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

  1. 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
  2. Which option with cut command allows you to select specific columns?
    a) -c
    b) -f
    c) -d
    d) -n
  3. 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
  4. 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
  5. 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
  6. What is the default sorting order of the sort command?
    a) Descending
    b) Random
    c) Ascending
    d) Unsorted
  7. 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
  8. 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
  9. Which option with the sort command will sort the contents in descending order?
    a) -n
    b) -r
    c) -d
    d) -f
  10. Which sort option should you use to sort data numerically?
    a) -n
    b) -r
    c) -d
    d) -f

wc, nl, awk, sed (Basic Usage)

  1. 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
  2. Which option with wc command counts the lines in a file?
    a) -l
    b) -w
    c) -c
    d) -s
  3. 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
  4. 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
  5. 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
  6. 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
  7. Which option with sed is used for replacing text in a file?
    a) -d
    b) -r
    c) -i
    d) -e
  8. 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
  9. 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
  10. 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

QnoAnswer (Option with Text)
1a) Concatenates and displays the content of files
2d) Displays the content of files in reverse order
3b) head -n 10 filename
4a) -n
5a) It displays the last lines of a file
6a) -f
7a) Displays the first 20 lines of a file
8a) tac
9a) tail -n 10 filename
10a) head -n 10 filename && tail -n 10 filename
11a) To extract columns from a file
12b) -f
13b) Merges lines from files side by side
14a) paste file1.txt file2.txt
15a) To arrange lines in alphabetical or numerical order
16c) Ascending
17a) Removes duplicate lines from a file
18a) sort filename
19b) -r
20a) -n
21a) Counts the number of lines, words, and characters in a file
22a) -l
23a) To number the lines of a file
24a) Extracts and processes data from files
25a) awk ‘{print $1}’ filename
26a) Streams text and modifies it in place
27c) -i
28a) sed ‘s/apple/orange/’ filename
29a) awk ‘$2 > 10’ filename
30a) sed ‘1d’ filename

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top