Problem 9 (12 point. Answer the following questions assuming that the file “temp
ID: 3588846 • Letter: P
Question
Problem 9 (12 point. Answer the following questions assuming that the file “temp.txt" contains the following 10 lines of content: temp.txt This is a test filk a) Give the command for obtaining just the first line of the file “temp.txt" b) Give the command for obtaining the lines starting at (and including) "A" and going to the end of the fil c) Give the command for obtaining just the lines "A" and "BB" (lines 4-5) d) Give the command for quickly displaying the entire contents of the file “temp.txt" to the console/terminal e) Give the command to open the file “temp.txt" for reading, starting at the line containing "DDDD" (do not use the line number) f Give the command to open the file "temp.txt" for reading, starting at ine 3.Explanation / Answer
NOTE: there is no programming language mentioned in your question. Please do that going forward. Given that the questions indicate commands i am guessing the commands has to be written in Unix. Let me know otherwise i will change the answers in the required programming language. If the answers are expected to be in Unix please check the solutions and let me know if you have any questions. I will acknowledge back with a response within 24 hours. Thanks for your patience.
Input file:
Unix Terminal> cat temp.txt
This is a test
file
================
A
BB
CCC
DDDD
EEE
FF
G
Unix Terminal>
a)
We can use head -1 <filename> command to obtain the first line in a given file. Head command displays the first n number of lines in a file. Below is the required command where head -1 represents the first line in the file temp.txt
Command output:
Unix Terminal> head -1 temp.txt
This is a test
Unix Terminal>
b)
To get all the lines starting from lines beginning with 'A' to end of the file we can use awk command along with its range operator. The range operator comma acts like a start and stop pattern where the command will print lines from the pattern matching start to stop pattern. In the below awk command it prints the lines from pattern starting with 'A' to end of the line which in this case stands for EOF.
Command output:
Unix Terminal> awk '/^A/,EOF' temp.txt
A
BB
CCC
DDDD
EEE
FF
G
Unix Terminal>
c)
To obtain just the lines matcing A and BB we can write a command in multiple ways. Following are some of them.
The egrep command matches for multiple patterns and prints those lines. In the below case, it looks for lines containing A and BB then prints them. If we want only the lines starting with A or BB then print them. We can also do that by using ^ symbol and following is another snapshot.
Command output:
Unix Terminal> egrep 'A|BB' temp.txt
A
BB
Unix Terminal>
Unix Terminal> egrep '^A|^BB' temp.txt
A
BB
Unix Terminal>
We can also use awk command to print the lines matching A and BB by using | symbol. In the below awk command it looks for lines matching either A or BB and prints those lines.
Command output:
Unix Terminal> awk '/A|BB/' temp.txt
A
BB
Unix Terminal>
d)
To display the entire contents of file temp.txt quickly we can use cat command.
Command output:
Unix Terminal> cat temp.txt
This is a test
file
================
A
BB
CCC
DDDD
EEE
FF
G
Unix Terminal>
e)
To read the lines starting from lines containing DDDD we can use the below command. The awk command will produce lines containing DDDD to end of the file. The read command will start reading lines from DDDD to end of the line using while loop.
Command output:
Unix Terminal> awk '/DDDD/, EOF' temp.txt|while read line; do echo $line; done
DDDD
EEE
FF
G
Unix Terminal>
f)
To read lines starting from 3 line to end of the file we can use the below command. The sed command generates lines starting from line number 3 to end of the file. The read command in while loop starts reading lines in a file from line 3 to end of the file.
Command output:
Unix Terminal> sed -n '3,$p' temp.txt|while read line; do echo $line; done
================
A
BB
CCC
DDDD
EEE
FF
G
Unix Terminal>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.