250+ TOP MCQs on sed command and Answers

Unix Multiple Choice Questions on “sed command”.

1. sed can also perform the substitution.
a) True
b) False

Answer: a
Clarification: Substitution is one of the most important features of sed as it allows us to replace a pattern in its input with some other pattern. For this purpose, regular expressions are used for enhancing pattern matching capabilities.

2. Which one of the following is the correct syntax for performing substitution using sed?
a) sed [address]s /expr1/ expr2
b) sed [address]s /expr1 expr2
c) sed [address]s /expr1/ expr2/ flags
d) sed [address]s

Answer: c
Clarification: Substitution is one of the most important features of sed as it allows us to replace a pattern in its input with some other pattern. The correct syntax for performing substitution is :

sed [address]s  /expr1/ expr2/ flags

where address represents the line numbers where we want to perform the substitution.
expr 1 and expr2 are expressions in which expr1 is replaced with expr2
flags are used for implementing the global or local replacement policy

3. Which one of the following command is used for replacing | with : globally?
a) sed ‘s/|/:/’ emp.lst
b) sed ‘/|/:/’ emp.lst
c) sed ‘s/|/:/g’ emp.lst
d) sed ‘s/

Answer: c
Clarification: sed can perform substitution i.e. it allows us to replace a pattern in its input with some other pattern. The flag g is used for performing substitution globally.

4. To replace the string ‘director’ in the first five lines of file emp.lst with ‘manager’ we can use _____
a) sed ‘s/director/manager/’ emp.lst
b) sed ‘1-5s/director/manager/’ emp.lst
c) sed ‘1,5s/director/manager/’ emp.lst
d) sed ‘15s

  250+ TOP MCQs on System Calls Basics and Answers

Answer: c
Clarification: Performing substitution using sed is not only limited to a single character; it can be any string. Above command will replace the string ‘director’ with ‘manager’ in the first five lines of file emp.lst.

5. Which of the following characters are used with sed as anchoring characters?
a) $
b) ^
c) %
d) $ and ^

Answer: d
Clarification: We can use the anchoring characters, ^ and & with the same meaning. They are used as sole characters in the source pattern to replace the target pattern at that location. For example,

$ sed ‘s/^/2/’  emp.lst          //add first character with a prefix ‘2’
$ sed ‘s/$/.00/’ emp.lst        // add the last character with a suffix ‘.00’

6. What will be the function of the following command?
$ sed ‘s/ *|/|/g’ emp.lst
a) replace * with |
b) replace / with |
c) compress multiple spaces
d) erroneous output

Answer: c
Clarification: We can delete the trailing spaces from a file using sed. For this purpose the metacharacter (*) is used.

7. Basic regular expressions are divided into ______ categories.
a) 1
b) 3
c) 2
d) 5

Answer: b
Clarification: Basic regular expressions are divided into three categories namely –repeated pattern, Interval regular expressions and tagged regular expressions.

8. The interval regular expression uses the character _______
a) {
b) }
c) { and }
d) ( and )

Answer: c
Clarification: Interval regular expression uses { and } with a single or pair of numbers between them. For example,

$ grep  ‘[0-9]{10}’  emp.lst

Above command selects only those users who have a mobile phone. Here we used IRE to indicate that a numeral can occur 10 times (as phone numbers are of 10 digits).

  250+ TOP MCQs on Editing Text in Vi Editor and Answers

9. Which one of the following command will be used for adding two spaces before every line in emp.lst?
a) sed ‘s/^/ /’ emp.lst
b) sed ‘s / / /’emp.lst
c) sed ‘s /$/ /’emp.lst
d) sed ‘s/$/

Answer: a
Clarification: The ^ symbol is used for prefixing some value at the beginning of any line in the file.

10. Which shortcut does sed offer to replace the string Linux with Red hat Linux?
a) { }
b) ( )
c) ^^
d) &

Answer: d
Clarification: sed supports the use of repeated pattern. For this purpose, & is used. It makes the whole string appear in the target string also. For example, to replace the string Linux with Red hat Linux, use the following command:

$ sed ‘s/ Linux/Red Hat &/’ emp.lst

11. Which command will be used for selecting lines 3 to 10 from emp.lst?
a) sed 3,10 emp.lst
b) sed -n ‘3,10p’ emp.lst
c) sed -n ‘^,10p’ emp.lst
d) sed -n, 10p

Answer: b
Clarification: To select a specific number of lines, we can use sed along with p option. For example,

  $ sed -n ‘1,$p’ emp.lst

will select all the lines from the file emp.lst.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top