Changes

Jump to: navigation, search

Tutorial11: Sed & Awk Utilities

601 bytes added, 12:19, 26 November 2020
INVESTIGATION 2: USING THE AWK UTILITY
:* Use the '''sed''' command to '''manipulate text''' contained in a file.
:* List and understand explain several '''instructions''' associated with the '''sed''' command.
:* Use the '''sed''' command as a '''filter''' with Linux pipeline commands.
:* Use the '''awk''' command to '''manipulate text''' contained in a file.
 
:* List and explain several '''comparison operators''' and variables associated with the '''awk''' command.
:* Use the '''awk''' command as a '''filter''' with Linux pipeline commands.
<br><br>
 
===Tutorial Reference Material===
| style="padding-left:15px;" |Text Manipulation
* [https://www.digitalocean.com/community/tutorials/the-basics-of-using-the-sed-stream-editor-to-manipulate-text-in-linux Purpose of using the sed utility]* [https://www.digitalocean.com/community/tutorials/how-to-use-the-awk-language-to-manipulate-text-in-linux Purpose of using the awk utility]
| style="padding-left:15px;" |Man Pages
'''Usage:'''
<span style="color:blue;font-weight:bold;font-family:courier;">awk options 'selection _criteria -criteria {action }’ file-name</span>
# Issue a command to '''confirm''' you are located in your home directory.<br><br>
# Issue the following linux command ('''copy and paste''' to save time):<br><span style="color:blue;font-weight:bold;font-family:courier;">wget <nowiki>https://ict.senecacollege.ca/~murray.saul/uli101/cars.txt</nowiki></span><br><br>
# Issue the '''more''' command to quickly view the contents of the '''cars.txt''' file.<br>When finished, exit the more command by pressing the letter <span style="color:blue;font-weight:bold;font-family:courier;">q</span><br><br>The "'''print'''" action (command) is the <u>default </u> action of awk to print all selected lines that match a pattern.<br>This action (contained in braces) can provide more options such as printing specific fields of selected lines (or records) from a database.<br><br># Issue the following linux command all to display records in the "cars.txt" database that contain the make "ford":<br><span style="color:blue;font-weight:bold;font-family:courier;">wget awk '/ford/ {print}' cars.txt</span><br><br># Issue the following linux command all to display records in the "cars.txt" database that contain the make "ford":<br><span style="color:blue;font-weight:bold;font-family:courier;">awk '/ford/' cars.txt</span><br><br>What do you notice?<br><br>You can use variables with the "print" action for further processing. We will discuss the following variables in this tutorial:<br><br>'''$0''' - Current record (entire line)<br>'''$1''' - First field in record<br>'''$n''' - nth field in record<br>'''NR''' - Record Number (order in database)<br> '''NF''' - Number of fields in current record<br><br>For a listing of more variables, please consult your course notes.<br><br>The '''tilde character''' '''~''' character is used to search for a pattern or display standard output for a particular field.<br><br>
# Issue the following linux command to display the model, year, quantity and price in the "cars.txt" database for makes of "chevy":<br><span style="color:blue;font-weight:bold;font-family:courier;">awk '/chevy/ {print $2,$3,$4,$5}' cars.txt</span><br><br>Notice that a space " " is the delimiter for the fields that appear as standard output.<br><br>
# Issue the following linux command to display all plymouths (plyms) by model name, price and quantity:<br><span style="color:blue;font-weight:bold;font-family:courier;">awk '/chevy/ {print $2,$3,$4,$5}' cars.txt</span><br><br>You can also use comparison operators to specify conditions for processing with matched patterns when using the awk command. Since they are used WITHIN the awk expression, they are not confused with redirection symbols<br><br> Comparison Operators:<br><br>'''<''' &nbsp;&nbsp;&nbsp;&nbsp;Less than<br>'''<=''' &nbsp;&nbsp;Less than or equal<br>'''>''' &nbsp;&nbsp;&nbsp;&nbsp;Greater than<br>'''>=''' &nbsp;&nbsp;Greater than or equal<br>'''==''' &nbsp;&nbsp;Equal<br>'''!=''' &nbsp;&nbsp;&nbsp;Not equal<br><br>
# <span style="font-family:courier;font-weight:bold">sed -n '3,6 p' ~murray.saul/uli101/stuff.txt</span><br><br># <span style="font-family:courier;font-weight:bold">sed '4 q' ~murray.saul/uli101/stuff.txt</span><br><br># <span style="font-family:courier;font-weight:bold">sed '/the/ d' ~murray.saul/uli101/stuff.txt</span><br><br># <span style="font-family:courier;font-weight:bold">sed 's/line/NUMBER/g' ~murray.saul/uli101/stuff.txt</span>
# <span style="font-family:courier;font-weight:bold">awk ‘NR == 3 {print}’ ~murray.saul/uli101/stuff.txt</span><br><br># <span style="font-family:courier;font-weight:bold">awk ‘NR >= 2 && NR <= 5 {print}’ ~murray.saul/uli101/stuff.txt</span><br><br># <span style="font-family:courier;font-weight:bold">awk ‘$1 ~ /This/ {print $2}’ ~murray.saul/uli101/stuff.txt</span><br><br># <span style="font-family:courier;font-weight:bold">awk ‘$1 ~ /This/ {print $3,$2}’ ~murray.saul/uli101/stuff.txt</span><br><br>
13,420
edits

Navigation menu