13,420
edits
Changes
→INVESTIGATION 2: USING THE AWK UTILITY
# Issue the following linux pipeline command to display the '''model''', '''year''', '''quantity''' and price<br>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 | tee awk-2.txt</span><br><br>Notice that a '''space''' is the delimiter for the fields that appear as standard output.<br><br>The '''tilde character''' '''~''' is used to search for a pattern or display standard output for a particular field.<br><br>
# Issue the following linux pipeline command to display all '''plymouths''' ('''plyms''')<br>by '''model name''', '''price''' and '''quantity''':<br><span style="color:blue;font-weight:bold;font-family:courier;">awk '$1 ~ /plyms/ {print $2,$3,$4,$5}' cars.txt | tee awk-3.txt</span><br><br>You can also use '''comparison operators''' to specify conditions for processing with matched patterns<br>when using the awk command. Since they are used WITHIN the awk expression,<br>they are not confused with redirection symbols<br><br>'''<''' Less than<br>'''<=''' Less than or equal<br>'''>''' Greater than<br>[[Image:awk-3.png|thumb|right|400px|Using the awk command to display results based on '''comparison operators'''.]]'''>=''' Greater than or equal<br>'''==''' Equal<br>'''!=''' Not equal<br><br>
# Issue the following linux pipeline command to display display the '''car make''',<br>'''model''', '''quantity''' and '''price''' of all vehicles<br>whose '''prices are less than $5,000''':<br><span style="color:blue;font-weight:bold;font-family:courier;">awk '$5 < 5000 {print $1,$2,$4,$5}' cars.txt | tee awk-4.txt</span><br><br>What do you notice?<br><br>
# Issue the following linux pipeline command to display display the car make, model number, quantity and price of all vehicles that are prices less than $5,000:<br><span style="color:blue;font-weight:bold;font-family:courier;">awk '$5 < 5000 {print $1,$2,$4,$5}' cars.txt | tee awk-5.txt</span><br><br>
# Issue the following linux pipeline command to display the car make, year and quantity of all car makes that begin with the letter 'f':<br><span style="color:blue;font-weight:bold;font-family:courier;">awk '$1 ~ /^f/ {print $1,$2,$4}' cars.txt | tee awk-6.txt</span><br><br>Compound criteria symbols can be used to join search statements together<br><br>Compound Operators:<br><br>'''&&''' (and)<br>'''||''' (or)<br><br>