Open main menu

CDOT Wiki β

Changes

Tutorial 11 - SED & AWK

3,888 bytes added, 14:01, 14 November 2021
no edit summary
= LINUX PRACTICE QUESTIONS =
 
The purpose of this section is to obtain '''extra practice''' to help with '''quizzes''', your '''midterm''', and your '''final exam'''.
 
Here is a link to the [https://matrix.senecacollege.ca/~osl640/questions/osl640_week11_practice.docx MS Word Document of ALL of the questions] displayed below but with extra room to answer on the document to
simulate a quiz:
 
Your instructor may take-up these questions during class. It is up to the student to attend classes in order to obtain the answers to the following questions. Your instructor will NOT provide these answers in any other form (eg. e-mail, etc).
 
 
'''Review Questions:'''
 
'''Part A: Display Results from Using the sed Utility'''
 
Note the contents from the following tab-delimited file called '''~osl640/stuff.txt''':
(this file pathname exists for checking your work)
 
<pre>
Line one.
This is the second line.
This is the third.
This is line four.
Five.
Line six follows
Followed by 7
Now line 8
and line nine
Finally, line 10
</pre>
 
 
Write the results of each of the following Linux commands for the above-mentioned file:
 
 
# <span style="font-family:courier;font-weight:bold">sed -n '3,6 p' ~osl640/stuff.txt</span><br><br>
# <span style="font-family:courier;font-weight:bold">sed '4 q' ~osl640/stuff.txt</span><br><br>
# <span style="font-family:courier;font-weight:bold">sed '/the/ d' ~osl640/stuff.txt</span><br><br>
# <span style="font-family:courier;font-weight:bold">sed 's/line/NUMBER/g' ~osl640/stuff.txt</span>
 
 
'''Part B: Writing Linux Commands Using the sed Utility'''
 
Write a single Linux command to perform the specified tasks for each of the following questions.
 
 
# Write a Linux sed command to display only lines 5 to 9 for the file: '''~osl640/stuff.txt'''<br><br>
# Write a Linux sed command to display only lines the begin the pattern “and” for the file: '''~osl640/stuff.txt'''<br><br>
# Write a Linux sed command to display only lines that end with a digit for the file: '''~osl640/stuff.txt'''<br><br>
# Write a Linux sed command to save lines that match the pattern “line” (upper or lowercase) for the file: '''~osl640/stuff.txt''' and save results (overwriting previous contents) to: '''~/results.txt'''<br><br>
 
 
'''Part C: Writing Linux Commands Using the awk Utility'''
 
Note the contents from the following tab-delimited file called '''~osl640/stuff.txt''':
(this file pathname exists for checking your work)
 
<pre>
Line one.
This is the second line.
This is the third.
This is line four.
Five.
Line six follows
Followed by 7
Now line 8
and line nine
Finally, line 10
</pre>
 
 
'''Write the results of each of the following Linux commands for the above-mentioned file:'''
 
 
# <span style="font-family:courier;font-weight:bold">awk ‘NR == 3 {print}’ ~osl640/stuff.txt</span><br><br>
# <span style="font-family:courier;font-weight:bold">awk ‘NR >= 2 && NR <= 5 {print}’ ~osl640/stuff.txt</span><br><br>
# <span style="font-family:courier;font-weight:bold">awk ‘$1 ~ /This/ {print $2}’ ~osl640/stuff.txt</span><br><br>
# <span style="font-family:courier;font-weight:bold">awk ‘$1 ~ /This/ {print $3,$2}’ ~osl640/stuff.txt</span><br><br>
 
 
'''Part D: Writing Linux Commands Using the awk Utility'''
 
 
Write a single Linux command to perform the specified tasks for each of the following questions.
 
 
# Write a Linux awk command to display all records for the file: '''~/cars''' whose fifth field is greater than 10000.<br><br>
# Write a Linux awk command to display the first and fourth fields for the file: '''~/cars''' whose fifth field begins with a number.<br><br>
# Write a Linux awk command to display the second and third fields for the file: '''~/cars''' for records that match the pattern “chevy”.<br><br>
# Write a Linux awk command to display the first and second fields for all the records contained in the file: '''~/cars'''<br><br>
 
 
[[Category:OSL640]]