0% found this document useful (0 votes)
150 views7 pages

Sed Assignment

Iitm.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
150 views7 pages

Sed Assignment

Iitm.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Week 7 - Sed Assignment

Graded Assignment
Problem 1
Which of the following commands will print the count of lines in the input file file1 ? The expected output
is only a number indicating the count of lines.[MSQ]

(1) awk 'END{print NR}' file1


(2) awk '{print NR}' file1
(3) sed '$=' file1

(4) sed -n '$=' file1

Answer
(1) and (4)

Problem 2
Match the following sed action characters to their use.[MCQ]

Action Use

1. d A. Delete the pattern space.

2. c B. Insert above current line.

3. a C. Insert below current line.

4. i D. Replace current line.

(a) 1-A, 2-D, 3-C, 4-B


(b) 1-A, 2-D, 3-B, 4-C
(c) 1-D, 2-A, 3-B, 4-C
(d) 1-D, 2-A, 3-C, 4-B

Answer
(a)

Problem 3
Which of the following sed commands will give the same output as the command cat file1 | head -5 |
tail -2 ? Consider that file1 contains exactly 10 lines of text.[MSQ]
(a) sed -n -e '4p' -e '5p' file1
(b) sed -n '4,5p' file1
(c) sed -n -e '4,5p' file1
(d) sed -n -e '4~5' file1
(e) sed '1,3d; 6,$d' file1
(f) None of the above

Answer
(a), (b), (c) and (e)

Problem 4
Which of the following sed commands will give the same output as the command cat file1 | head -5 |
tail -2 irrespective of the number of lines in the file file1 .[MSQ]

(a) sed -n -e '4p' -e '5p' file1


(b) sed -n '4,5p' file1
(c) sed -n -e '4,5p' file1
(d) sed -n -e '4~5' file1
(e) sed '1,3d; 6,$d' file1
(f) None of the above

Answer
(f)

Problem 5
Which of the following sed commands can be used to delete the lines containing the word ram (case
incensitive) in a file input.txt ? The command should just print the desired output, and not replace the
contents of the original file. [MSQ]

(a) sed -e '/ram/d'


(b) sed -e '/ram/Id'
(c) sed -e 's/FROM//i' input.txt
(d) sed -e 's/FROM//I' input.txt
(e) sed -e '/ram/id'

Answer
(b)

Problem 6
Which of the following sed commands can be used to delete all the occurences of the word ram (case
incensitive) in a file input.txt ? The command should just print the desired output, and not replace the
contents of the original file. [MSQ]
(c) sed -e 's/FROM//i' input.txt
(d) sed -e 's/FROM//I' input.txt
(c) sed -e 's/FROM//ig' input.txt
(d) sed -e 's/FROM//Ig' input.txt
(e) sed -e 's/FROM//g' input.txt

Answer
(c) and (d)

Problem 7
Consider a file twister.txt as below with contents as below.

$ cat twister.txt
How much wood would a woodchuck chuck if a woodchuck could chuck wood?
He would chuck, he would, as much as he could, and chuck as much wood
As a woodchuck would if a woodchuck could chuck wood

Given below some commands and outputs, match the command to their output.

1. sed '1,2 s/wood/WOOD/' twister.txt


2. sed 's/wood/WOOD/g' twister.txt
3. sed '2,3 s/wood/WOOD/2' twister.txt
4. sed 's/wood/WOOD/3g' twister.txt

Output A

How much WOOD would a WOODchuck chuck if a WOODchuck could chuck WOOD?
He would chuck, he would, as much as he could, and chuck as much WOOD
As a WOODchuck would if a WOODchuck could chuck WOOD

Output B

How much wood would a woodchuck chuck if a woodchuck could chuck wood?
He would chuck, he would, as much as he could, and chuck as much wood
As a woodchuck would if a WOODchuck could chuck wood

Output C

How much WOOD would a woodchuck chuck if a woodchuck could chuck wood?
He would chuck, he would, as much as he could, and chuck as much WOOD
As a woodchuck would if a woodchuck could chuck wood

Output D
How much wood would a woodchuck chuck if a WOODchuck could chuck WOOD?
He would chuck, he would, as much as he could, and chuck as much wood
As a woodchuck would if a woodchuck could chuck WOOD

(a) 1-B, 2-A, 3-C, 4-C


(b) 1-C, 2-A, 3-B, 4-D
(c) 1-B, 2-D, 3-C, 4-A
(d) 1-D, 2-C, 3-A, 4-B

Answer
(b)

Problem 8
sed '$s/\(.*\)wood/\1WOOD/g' twister.txt

The above command will print the file twister.txt after:

(1) Replacing all occurrences of "wood" with "WOOD" in every line of the file.

(2) Replacing all occurances of "wood" with "WOOD" in last line of the file.

(3) Replacing last occurance of "wood" with "WOOD" in every line of the file.

(4) Replacing last occurance of "wood" with "WOOD" in last line of the file.

(5) Replacing first occurance of "wood" with "WOOD" in every line of the file.

(6) Replacing first occurance of "wood" with "WOOD" in last line of the file.

Answer
(4)

Problem 9
What is the expected outuput of the following command

echo "hello world" | sed -e "s/\b\(.\)/\U\1/g"

(1) HELLO WORLD

(2) u 1

(3) helloworld

(4) Hello World


Answer
(4) Hello World

Problem 10
What will the following command do?

sed '/[bB]reak$/a #####' file1

(a) Inserts five hashes on a new line before every line that ends with the word "break", the matching will be
case incesitive.

(b) Inserts five hashes on a new line after every line that ends with the word "break", the matching will be
case incesitive.

(c) Inserts five hashes on a new line before every line that ends with the word "break" or "Break".

(d) Inserts five hashes on a new line after every line that ends with the word "break" or "Break".

Answer
(c)

Practice Questions
Problem 1
Which of the following commands will replace the word "teach"(but not change anything in the word
"teaching") with the word "reach"?

(1) echo "teaching you how to teach" | sed -e "s/^teach$/reach/"

(2) echo "teaching you how to teach" | sed -e "s/$teach^/reach/"

(3) echo "teaching you how to teach" | sed -e "s/\bteach\b/reach/"

(4) echo "teaching you how to teach" | sed -e "s/teach\b/reach/"

Answer
(3) echo "teaching you how to teach" | sed -e "s/\bteach\b/reach/"

(4) echo "teaching you how to teach" | sed -e "s/teach\b/reach/"

Problem 2
Match the following sed option with their use
Option Use

1. -n (a) Use extended regular expressions in the script.

2. -r (b) To run/execute sed script stored in a file.

3. -f (c) Add the scripts to the commands to be executed.

4. -e (d) Run in quiet mode, i.e. suppress automatic printing of lines in input.

(a) 1-a, 2-c, 3-d, 4-b


(b) 1-d, 2-c, 3-b, 4-a
(c) 1-a, 2-d, 3-c, 4-b
(d) 1-d, 2-a, 3-b, 4-c

Answer
(d)

Problem 3
The file 'dates.txt' has a list of dates in MM/DD/YYYY format. Which of the following commands can be used
to convert it to YYYY-MM-DD format?

(1) sed 's/\([0-9]{2}\)\/\([0-9]{2}\)\/\([0-9]{4}\)/\3-\1-\2/' dates.txt

(2) sed 's/([0-9]{2})/([0-9]{2})/([0-9]{4})/\3-\1-\2/' dates.txt

(3) sed 's/\([0-9]\{2\}\)\/\([0-9]\{2\}\)\/\([0-9]\{4\}\)/\4-\2-\2/' dates.txt

(4) sed 's/\([0-9]\{2\}\)\/\([0-9]\{2\}\)\/\([0-9]\{4\}\)/\3-\1-\2/' dates.txt

Answer
(4) sed 's/\([0-9]\{2\}\)\/\([0-9]\{2\}\)\/\([0-9]\{4\}\)/\3-\1-\2/' dates.txt

Problem 4
Which of the following are the buffers maintained in sed ? [MSQ]

(a) Input space


(b) Output space
(c) Pattern space
(d) Auxiliary hold space
Answer
(c) and (d)

Problem 5
The varible var contains a string. Which of the following commands is/are equivalent to the command
echo ${var^^} ?[MSQ]

(1) echo $var | sed 's/\(.*\)/\U&/g'

(2) echo $var | sed 's/\(.*\)/\L&/g'

(3) echo $var | sed 's/\(.*\)/\U\1/g'

(4) echo $var | sed 's/\(.*\)/\L\1/g'

Answer
(1) and (3)

You might also like