{"id":6840,"date":"2020-02-11T09:08:31","date_gmt":"2020-02-11T15:08:31","guid":{"rendered":"https:\/\/www.poweradmin.com\/blog\/?p=6840"},"modified":"2019-12-06T16:21:39","modified_gmt":"2019-12-06T22:21:39","slug":"how-to-maximize-on-your-usage-of-awk","status":"publish","type":"post","link":"https:\/\/www.poweradmin.com\/blog\/how-to-maximize-on-your-usage-of-awk\/","title":{"rendered":"How To Maximize On Your Usage Of Awk"},"content":{"rendered":"<p><a href=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2020\/02\/using-awk.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-6849 size-full alignleft\" src=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2020\/02\/using-awk.jpg\" alt=\"\" width=\"350\" height=\"233\" srcset=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2020\/02\/using-awk.jpg 350w, https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2020\/02\/using-awk-300x200.jpg 300w\" sizes=\"auto, (max-width: 350px) 100vw, 350px\"><\/a><\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">Abbreviated from the names of its developers \u2013 Aho, Weinberger, and Kernighan \u2013 Awk is a\u00a0 scripting language used in Unix or Linux environments for manipulating data and generating reports.<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">The awk command programming language requires no compiling, and allows the user to employ variables, numeric functions, string functions, and logical operators. In this article, we\u2019ll be looking at some examples demonstrating its many and diverse uses.<\/span><\/p>\n<h2><span style=\"font-family: verdana, geneva, sans-serif;\">Some Awk Basics<\/span><\/h2>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\"><a href=\"https:\/\/www.geeksforgeeks.org\/awk-command-unixlinux-examples\/\" rel=\"nofollow\" target=\"_blank\">In essence<img class=\"extlink-icon\" src=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/plugins\/external-links-nofollow-open-in-new-tab-favicon\/images\/extlink.png\"><\/a>, the awk command scans a file line by line, splits each input line into fields, compares input lines or fields to a specified pattern, then performs one or more actions on the matched lines.<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">Awk operations are especially useful in transforming data files, and producing formatted reports.<\/span><\/p>\n<h2><span style=\"font-family: verdana, geneva, sans-serif;\">Selecting Specific Fields<\/span><\/h2>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">The easiest and most common <a href=\"https:\/\/www.networkworld.com\/article\/3454979\/the-many-faces-of-awk.html\" rel=\"nofollow\" target=\"_blank\">usage of awk<img class=\"extlink-icon\" src=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/plugins\/external-links-nofollow-open-in-new-tab-favicon\/images\/extlink.png\"><\/a> is selecting specific fields from files, or from data that is fed into the awk command line. You use white space (any sequence of blanks and tabs) as a field separator by default, and a simple example at the Linux $ prompt would be:<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">$ echo one two three four five | awk \u2018{print $4}\u2019<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">four<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">$ who | awk \u2018{print $1}\u2019<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">jdoe<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">fhenry<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">In the example above, awk is extracting just the fourth and first fields from the data provided.<\/span><\/p>\n<h2><span style=\"font-family: verdana, geneva, sans-serif;\">Using Awk To Pull Text From Files<\/span><\/h2>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">By adding the name of the file after the awk command, you can use the mechanism to pull specific text from files. For example, to pick out the first, fifth and last words in the single line of text from the file \u201cHelenKellerQuote\u201d:<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">$ awk \u2018{print $1,$5,$NF}\u2019 HelenKellerQuote<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">The beautiful heart.<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">Specifying $NF in the command pulls out the last piece of text on each line. Here, NF represents the number of fields in the line (23) while $NF represents the value of that field (\u201cheart.\u201d). The period is included because it\u2019s part of the final text string.<\/span><\/p>\n<h2><span style=\"font-family: verdana, geneva, sans-serif;\">Using Awk To Print Text From Linux Files<\/span><\/h2>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">The awk command may be used to print fields from a file in any specified order. In the example below, awk is rearranging the fields in a date command output:<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">$ date | awk \u2018{print $4,$3,$2}\u2019<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">2019 Nov 22<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">Note that if you leave out the commas between the field designators in an awk command, the output will be pushed into a single string.<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">$ date | awk \u2018{print $4 $3 $2}\u2019<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">2019Nov21<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">If you happen to replace the commas with hyphens, awk will attempt to subtract one field from another:<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">$ date | awk \u2018{print $4-$3-$2}\u2019<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">1997<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">Here, awk is subtracting 22 (the day of the month) from the year (2019) and simply ignoring \u201cNov\u201d.<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">If you want the output to be separated by something other than white space, you can specify an output separator with the OFS (output field separator) modifier like this:<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">$ date | awk \u2018{OFS=\u201d-\u201c; print $4,$3,$2}\u2019<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">2019-Nov-22<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">As part of a script, you can use awk to simply display some relevant text. In this example, we\u2019re using awk to display a line of text to label a set of data so that you can identify what you\u2019re looking at:<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">$ who | awk \u2018BEGIN {print \u201cCurrent logins:\u201d} {print $1}\u2019<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">Current logins:<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">shs<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">nemo<\/span><\/p>\n<h2><span style=\"font-family: verdana, geneva, sans-serif;\">Specifying Field Separators<\/span><\/h2>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">If your text is separated by some character other than white space (commas, colons, semicolons, etc.), you can inform awk by using the -F (input separator) option as shown here:<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">$ cat testfile<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">a:b:c,d:e<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">$ awk -F : \u2018{print $2,$3}\u2019 testfile<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">b c,d<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">To specify a separator and pull a field from the colon-separated \/etc\/passwd file, you could use a syntax like this:<\/span><\/p>\n<p>\u00a0<\/p>\n<p><a href=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2020\/02\/awk-1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6842\" src=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2020\/02\/awk-1.jpg\" alt=\"\" width=\"416\" height=\"248\" srcset=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2020\/02\/awk-1.jpg 416w, https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2020\/02\/awk-1-300x179.jpg 300w\" sizes=\"auto, (max-width: 416px) 100vw, 416px\"><\/a><\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">(Image source: <a href=\"https:\/\/www.networkworld.com\/article\/3454979\/the-many-faces-of-awk.html\" rel=\"nofollow\" target=\"_blank\">NetworkWorld.com<img class=\"extlink-icon\" src=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/plugins\/external-links-nofollow-open-in-new-tab-favicon\/images\/extlink.png\"><\/a>)<\/span><\/p>\n<h2><span style=\"font-family: verdana, geneva, sans-serif;\">Evaluating Fields Using Awk<\/span><\/h2>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">You can also use the awk command to take a deeper look into the contents of a data file. If for example, you want to list only the user accounts in \/etc\/passwd, you can include a test for the 3rd field. In the screenshot we\u2019re only interested in any UIDs that are 1000 and above:<\/span><\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n<p><a href=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2020\/02\/aws-2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6844\" src=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2020\/02\/aws-2.jpg\" alt=\"\" width=\"622\" height=\"130\" srcset=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2020\/02\/aws-2.jpg 622w, https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2020\/02\/aws-2-300x63.jpg 300w\" sizes=\"auto, (max-width: 622px) 100vw, 622px\"><\/a><\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">(Image source: <a href=\"https:\/\/www.networkworld.com\/article\/3454979\/the-many-faces-of-awk.html\" rel=\"nofollow\" target=\"_blank\">NetworkWorld.com<img class=\"extlink-icon\" src=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/plugins\/external-links-nofollow-open-in-new-tab-favicon\/images\/extlink.png\"><\/a>)<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">To add a title for the listing, you can add a BEGIN clause:<\/span><\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n<p><a href=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2020\/02\/awk-3.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6845\" src=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2020\/02\/awk-3.jpg\" alt=\"\" width=\"622\" height=\"134\" srcset=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2020\/02\/awk-3.jpg 622w, https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2020\/02\/awk-3-300x65.jpg 300w\" sizes=\"auto, (max-width: 622px) 100vw, 622px\"><\/a><\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">(Image source: <a href=\"https:\/\/www.networkworld.com\/article\/3454979\/the-many-faces-of-awk.html\" rel=\"nofollow\" target=\"_blank\">NetworkWorld.com<img class=\"extlink-icon\" src=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/plugins\/external-links-nofollow-open-in-new-tab-favicon\/images\/extlink.png\"><\/a>)<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">You can use the \u201c\\n\u201d (newline characters) modifier to separate your intended output lines, if you want more than one line in your title:<\/span><\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n<p><a href=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2020\/02\/awk-4.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6846\" src=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2020\/02\/awk-4.jpg\" alt=\"\" width=\"622\" height=\"168\" srcset=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2020\/02\/awk-4.jpg 622w, https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2020\/02\/awk-4-300x81.jpg 300w\" sizes=\"auto, (max-width: 622px) 100vw, 622px\"><\/a><\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">(Image source: <a href=\"https:\/\/www.networkworld.com\/article\/3454979\/the-many-faces-of-awk.html\" rel=\"nofollow\" target=\"_blank\">NetworkWorld.com<img class=\"extlink-icon\" src=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/plugins\/external-links-nofollow-open-in-new-tab-favicon\/images\/extlink.png\"><\/a>)<\/span><\/p>\n<h2><span style=\"font-family: verdana, geneva, sans-serif;\">Using Awk For Math Calculations<\/span><\/h2>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">The awk command includes several mathematical capabilities, and is able to calculate square roots, logs, tangents, and other functions. So for example:<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">$ awk \u2018BEGIN {print sqrt(2019)}\u2019<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">44.9333<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">This returns the square root of 2019. The syntax below calculates the logarithm of 2019:<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">$ awk \u2018BEGIN {print log(2019)}\u2019<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">7.61036<\/span><\/p>\n<p>\u00a0<\/p>\n<h2><span style=\"font-family: verdana, geneva, sans-serif;\">Creating Scripts With Awk<\/span><\/h2>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">It\u2019s possible to use awk for creating standalone scripts. The example below builds on the user accounts case given earlier, but also counts the number of users with accounts on the system.<\/span><\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n<p><a href=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2020\/02\/awk-5.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6847\" src=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2020\/02\/awk-5.jpg\" alt=\"\" width=\"622\" height=\"455\" srcset=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2020\/02\/awk-5.jpg 622w, https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2020\/02\/awk-5-300x219.jpg 300w\" sizes=\"auto, (max-width: 622px) 100vw, 622px\"><\/a><\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">(Image source: <a href=\"https:\/\/www.networkworld.com\/article\/3454979\/the-many-faces-of-awk.html\" rel=\"nofollow\" target=\"_blank\">NetworkWorld.com<img class=\"extlink-icon\" src=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/plugins\/external-links-nofollow-open-in-new-tab-favicon\/images\/extlink.png\"><\/a>)<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">The BEGIN section is run only when the script starts, and provides a heading, dictates the field separator, and sets up a counter to start with 0. The script also includes an END section which only runs after all the lines in the text provided to the script have been processed. It displays the final count of lines which meet the specification in the middle section.<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">The awk command has a long tradition in Unix, and as we\u2019ve demonstrated, it still provides a range of uses and functionality for Linux environments.<\/span><\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Abbreviated from the names of its developers \u2013 Aho, Weinberger, and Kernighan \u2013 Awk is a\u00a0 scripting language used in Unix or Linux environments for manipulating data and generating reports. \u00a0 The awk command programming language requires no compiling, and allows the user to employ variables, numeric functions, string functions, and logical operators. In this [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":6849,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[447,6,9],"tags":[],"class_list":["post-6840","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","category-tech","category-technical"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.poweradmin.com\/blog\/wp-json\/wp\/v2\/posts\/6840","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.poweradmin.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.poweradmin.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.poweradmin.com\/blog\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/www.poweradmin.com\/blog\/wp-json\/wp\/v2\/comments?post=6840"}],"version-history":[{"count":5,"href":"https:\/\/www.poweradmin.com\/blog\/wp-json\/wp\/v2\/posts\/6840\/revisions"}],"predecessor-version":[{"id":6843,"href":"https:\/\/www.poweradmin.com\/blog\/wp-json\/wp\/v2\/posts\/6840\/revisions\/6843"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.poweradmin.com\/blog\/wp-json\/wp\/v2\/media\/6849"}],"wp:attachment":[{"href":"https:\/\/www.poweradmin.com\/blog\/wp-json\/wp\/v2\/media?parent=6840"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.poweradmin.com\/blog\/wp-json\/wp\/v2\/categories?post=6840"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.poweradmin.com\/blog\/wp-json\/wp\/v2\/tags?post=6840"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}