Unix Questions and Answers – sed command – 2

This set of Unix Multiple Choice Questions & Answers (MCQs) focuses on “sed command – 2”.

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

Answer: a
Explanation: 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
View Answer

Answer: c
Explanation: 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

advertisement
advertisement

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/
View Answer

Answer: c
Explanation: 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.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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
View Answer

Answer: c
Explanation: 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 ^
View Answer

Answer: d
Explanation: 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,

advertisement
$ 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’

advertisement

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
View Answer

Answer: c
Explanation: 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
View Answer

Answer: b
Explanation: 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 )
View Answer

Answer: c
Explanation: 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).

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/$/
View Answer

Answer: a
Explanation: 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) &
View Answer

Answer: d
Explanation: 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
View Answer

Answer: b
Explanation: 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.

Sanfoundry Global Education & Learning Series – Unix.

To practice all areas of Unix, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.