1. Write commands to carry out the following commands: a) Print all lines contai
ID: 3869690 • Letter: 1
Question
1. Write commands to carry out the following commands:
a) Print all lines containing the string John.
b) Print all lines where a word starts with J.
c) Print all lines ending with word stopped.
d) Print all lines that don't contain was.
e) Print lines where a word begins with K or k.
2. Write a bash script that copies all files of the current directory into a new sub-directory, and then deletes the created sub-directory along with its contents and displays a message that indicates completion of the task.
3. Write a bash script that reads two numbers x and y and performs addition, subtraction, multiplication, and division of the two numbers (x+y, x-y, x*y, x/y) and prints out each of these values along with some text to indicate what these numbers are.
Explanation / Answer
1. Write commands to carry out the following commands:
The grep command searches the given file for the patterns specified. Therefore, grep command
will be used to achieve all the requirements here.
a) Print all lines containing the string John.
>grep 'John' fileName
will print all the lines containing the string John.
b) Print all lines where a word starts with J.
>grep '^J' fileName
will print all the lines start with J.
c) Print all lines ending with word stopped.
>grep 'stopped$' fileName
will print all the lines that end with the word stopped
d) Print all lines that don't contain was.
>grep -v -e 'was' fileName
will list all the lines that doesn't contain the word was.
e) Print lines where a word begins with K or k.
>grep -E '^(k|K)' temp.txt
will list all words beginning with either k or K.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.