{"id":13612,"date":"2019-04-03T14:51:02","date_gmt":"2019-04-03T14:51:02","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=13612"},"modified":"2019-04-03T14:58:25","modified_gmt":"2019-04-03T14:58:25","slug":"awk","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/03\/awk\/","title":{"rendered":"AWK (Alfred V. Aho &#8211; Peter J. Weinberger &#8211; Brian W. Kernighan)"},"content":{"rendered":"<h1 class=\"post-title\">Ebook: Introducing the Awk Getting Started Guide for Beginners<\/h1>\n<p>As a Linux system administrator, many times, you will get into situations where you need to manipulate and reformat the output from different commands, to simply display part of a output by filtering out a few lines. This process can be referred to as text filtering, using a collection of Linux programs known as filters.<\/p>\n<p>There are several Linux utilities for text filtering and some of the well known filters include\u00a0<strong>head<\/strong>,\u00a0<strong>tail<\/strong>,\u00a0<strong>grep<\/strong>,\u00a0<strong>tr<\/strong>,\u00a0<strong>fmt<\/strong>,\u00a0<strong>sort<\/strong>,\u00a0<strong>uniq<\/strong>,\u00a0<strong>pr<\/strong>\u00a0and more advanced and powerful tools such as\u00a0<strong>Awk<\/strong>\u00a0and\u00a0<strong>Sed<\/strong>.<\/p>\n<div id=\"attachment_22347\" class=\"wp-caption aligncenter\">\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-22347\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/08\/Linux-Awk-Book.png\" alt=\"Introducing the Awk Getting Started Guide for Beginners\" width=\"320\" height=\"400\" aria-describedby=\"caption-attachment-22347\" data-lazy-loaded=\"true\" \/><\/p>\n<p id=\"caption-attachment-22347\" class=\"wp-caption-text\">Introducing the Awk Getting Started Guide for Beginners<\/p>\n<\/div>\n<p>Unlike\u00a0<strong>Sed<\/strong>,\u00a0<strong>Awk<\/strong>\u00a0is more than just a text filtering tool, it is a comprehensive and flexible text pattern scanning and processing language.<\/p>\n<p><strong>Awk<\/strong>\u00a0is a strongly recommended text filtering tool for Linux, it can be utilized directly from the command line together with several other commands, within shell scripts or in independent Awk scripts. It searches through input data or a single or multiple files for user defined patterns and modifies the input or file(s) based on certain conditions.<\/p>\n<p>Since\u00a0<strong>Awk<\/strong>\u00a0is a sophisticated programming language, learning it requires a lot of time and dedication just as any other programming language out there. However, mastering a few basic concepts of this powerful text filtering language can enable you to understand how it actually works and sets you on track to learn more advanced Awk programming techniques.<\/p>\n<p>After carefully and critically revising our 13 articles in the Awk programming series, with high consideration of the vital feedback from our followers and readers over the last 5 months, we have managed to organize the Introduction to Awk programming language eBook.<\/p>\n<p>Therefore, if you are ready to start learning Awk programming language from the basic concepts, with simple and easy-to-understand, well explained examples, then you may consider reading this concise and precise eBook.<\/p>\n<h3>What\u2019s Inside this eBook?<\/h3>\n<p>This book contains 13 chapters with a total of 41 pages, which covers all Awk basic and advance usage with practical examples:<\/p>\n<ol>\n<li><strong>Chapter 1<\/strong>: Awk Regular Expressions to Filter Text in Files<\/li>\n<li><strong>Chapter 2<\/strong>: Use Awk to Print Fields and Columns in File<\/li>\n<li><strong>Chapter 3<\/strong>: Use Awk to Filter Text Using Pattern Specific Actions<\/li>\n<li><strong>Chapter 4<\/strong>: Learn Comparison Operators with Awk<\/li>\n<li><strong>Chapter 5<\/strong>: Learn Compound Expressions with Awk<\/li>\n<li><strong>Chapter 6<\/strong>: Learn \u2018next\u2019 Command with Awk<\/li>\n<li><strong>Chapter 7<\/strong>: Read Awk Input from STDIN in Linux<\/li>\n<li><strong>Chapter 8<\/strong>: Learn Awk Variables, Numeric Expressions and Assignment Operators<\/li>\n<li><strong>Chapter 9<\/strong>: Learn Awk Special Patterns \u2018BEGIN and END\u2019<\/li>\n<li><strong>Chapter 10<\/strong>: Learn Awk Built-in Variables<\/li>\n<li><strong>Chapter 11<\/strong>: Learn Awk to Use Shell Variables<\/li>\n<li><strong>Chapter 12<\/strong>: Learn Flow Control Statements in Awk<\/li>\n<li><strong>Chapter 13<\/strong>: Write Scripts Using Awk Programming Language<\/li>\n<\/ol>\n<h1 class=\"post-title\">How to Use Awk and Regular Expressions to Filter Text or String in Files<\/h1>\n<p>When we run certain commands in Unix\/Linux to read or edit text from a string or file, we most times try to filter output to a given section of interest. This is where using regular expressions comes in handy.<\/p>\n<p><b>Read Also:<\/b>\u00a0<a href=\"https:\/\/www.tecmint.com\/chaining-operators-in-linux-with-practical-examples\/\" target=\"_blank\" rel=\"noopener\">10 Useful Linux Chaining Operators with Practical Examples<\/a><\/p>\n<h4>What are Regular Expressions?<\/h4>\n<p>A regular expression can be defined as a strings that represent several sequence of characters. One of the most important things about regular expressions is that they allow you to filter the output of a command or file, edit a section of a text or configuration file and so on.<\/p>\n<h4>Features of Regular Expression<\/h4>\n<p>Regular expressions are made of:<\/p>\n<ol>\n<li><strong>Ordinary characters<\/strong>\u00a0such as space, underscore(_), A-Z, a-z, 0-9.<\/li>\n<li><strong>Meta characters<\/strong>\u00a0that are expanded to ordinary characters, they include:\n<ol>\n<li><code>(.)<\/code>\u00a0it matches any single character except a newline.<\/li>\n<li><code>(*)<\/code>\u00a0it matches zero or more existences of the immediate character preceding it.<\/li>\n<li><code>[ character(s) ]<\/code>\u00a0it matches any one of the characters specified in character(s), one can also use a hyphen\u00a0<code>(-)<\/code>\u00a0to mean a range of characters such as\u00a0<code>[a-f]<\/code>,\u00a0<code>[1-5]<\/code>, and so on.<\/li>\n<li><code>^<\/code>\u00a0it matches the beginning of a line in a file.<\/li>\n<li><code>$<\/code>\u00a0matches the end of line in a file.<\/li>\n<li><code>\\<\/code>\u00a0it is an escape character.<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p>In order to filter text, one has to use a text filtering tool such as\u00a0<strong>awk<\/strong>. You can think of\u00a0<strong>awk<\/strong>\u00a0as a programming language of its own. But for the scope of this guide to using\u00a0<strong>awk<\/strong>, we shall cover it as a simple command line filtering tool.<\/p>\n<p>The general syntax of awk is:<\/p>\n<pre># awk 'script' filename\r\n<\/pre>\n<p>Where\u00a0<code>'script'<\/code>\u00a0is a set of commands that are understood by\u00a0<strong>awk<\/strong>\u00a0and are execute on file, filename.<\/p>\n<p>It works by reading a given line in the file, makes a copy of the line and then executes the script on the line. This is repeated on all the lines in the file.<\/p>\n<p>The\u00a0<code>'script'<\/code>\u00a0is in the form\u00a0<code>'\/pattern\/ action'<\/code>\u00a0where\u00a0<strong>pattern<\/strong>\u00a0is a regular expression and the\u00a0<strong>action<\/strong>\u00a0is what awk will do when it finds the given pattern in a line.<\/p>\n<h3>How to Use Awk Filtering Tool in Linux<\/h3>\n<p>In the following examples, we shall focus on the meta characters that we discussed above under the features of awk.<\/p>\n<h4>A simple example of using awk:<\/h4>\n<p>The example below prints all the lines in the file\u00a0<strong>\/etc\/hosts<\/strong>\u00a0since no pattern is given.<\/p>\n<pre># awk '<strong>\/\/<\/strong>{print}'\/etc\/hosts\r\n<\/pre>\n<div id=\"attachment_19810\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/04\/Awk-Command-Example.gif\" rel=\"attachment wp-att-19810\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-19810\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/04\/Awk-Command-Example.gif\" alt=\"Awk Prints all Lines in a File \" width=\"670\" height=\"314\" aria-describedby=\"caption-attachment-19810\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-19810\" class=\"wp-caption-text\">Awk Prints all Lines in a File<\/p>\n<\/div>\n<h4>Use Awk with Pattern:<\/h4>\n<p>I the example below, a pattern\u00a0<code>localhost<\/code>\u00a0has been given, so awk will match line having\u00a0<strong>localhost<\/strong>\u00a0in the\u00a0<code>\/etc\/hosts<\/code>\u00a0file.<\/p>\n<pre># awk '<strong>\/localhost\/<\/strong>{print}' \/etc\/hosts \r\n<\/pre>\n<div id=\"attachment_19811\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/04\/Use-Awk-Command-with-Pattern.gif\" rel=\"attachment wp-att-19811\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-19811\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/04\/Use-Awk-Command-with-Pattern.gif\" alt=\"Awk Print Given Matching Line in a File\" width=\"673\" height=\"129\" aria-describedby=\"caption-attachment-19811\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-19811\" class=\"wp-caption-text\">Awk Print Given Matching Line in a File<\/p>\n<\/div>\n<h4>Using Awk with (.) wild card in a Pattern<\/h4>\n<p>The\u00a0<code>(.)<\/code>\u00a0will match strings containing\u00a0<strong>loc<\/strong>,\u00a0<strong>localhost<\/strong>,\u00a0<strong>localnet<\/strong>\u00a0in the example below.<\/p>\n<p>That is to say\u00a0<strong>* l some_single_character c *<\/strong>.<\/p>\n<pre># awk '<strong>\/l.c\/<\/strong>{print}' \/etc\/hosts\r\n<\/pre>\n<div id=\"attachment_19812\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/04\/Use-Awk-with-Wild-Cards.gif\" rel=\"attachment wp-att-19812\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-19812\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/04\/Use-Awk-with-Wild-Cards.gif\" alt=\"Use Awk to Print Matching Strings in a File\" width=\"675\" height=\"155\" aria-describedby=\"caption-attachment-19812\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-19812\" class=\"wp-caption-text\">Use Awk to Print Matching Strings in a File<\/p>\n<\/div>\n<h4>Using Awk with (*) Character in a Pattern<\/h4>\n<p>It will match strings containing\u00a0<strong>localhost<\/strong>,\u00a0<strong>localnet<\/strong>,\u00a0<strong>lines<\/strong>,\u00a0<strong>capable<\/strong>, as in the example below:<\/p>\n<pre># awk '<strong>\/l*c\/<\/strong>{print}' \/etc\/localhost\r\n<\/pre>\n<div id=\"attachment_19813\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/04\/Use-Awk-to-Match-Strings-in-File.gif\" rel=\"attachment wp-att-19813\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-19813\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/04\/Use-Awk-to-Match-Strings-in-File.gif\" alt=\"Use Awk to Match Strings in File\" width=\"725\" height=\"250\" aria-describedby=\"caption-attachment-19813\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-19813\" class=\"wp-caption-text\">Use Awk to Match Strings in File<\/p>\n<\/div>\n<p>You will also realize that\u00a0<code>(*)<\/code>\u00a0tries to a get you the longest match possible it can detect.<\/p>\n<p>Let look at a case that demonstrates this, take the regular expression\u00a0<code>t*t<\/code>\u00a0which means match strings that start with letter\u00a0<code>t<\/code>\u00a0and end with\u00a0<code>t<\/code>\u00a0in the line below:<\/p>\n<pre>this is tecmint, where you get the best good tutorials, how to's, guides, tecmint. \r\n<\/pre>\n<p>You will get the following possibilities when you use the pattern\u00a0<code>\/t*t\/<\/code>:<\/p>\n<pre>this is t\r\nthis is tecmint\r\nthis is tecmint, where you get t\r\nthis is tecmint, where you get the best good t\r\nthis is tecmint, where you get the best good tutorials, how t\r\nthis is tecmint, where you get the best good tutorials, how tos, guides, t\r\nthis is tecmint, where you get the best good tutorials, how tos, guides, tecmint\r\n<\/pre>\n<p>And\u00a0<code>(*)<\/code>\u00a0in\u00a0<code>\/t*t\/<\/code>\u00a0wild card character allows awk to choose the the last option:<\/p>\n<pre>this is tecmint, where you get the best good tutorials, how to's, guides, tecmint\r\n<\/pre>\n<h4>Using Awk with set [ character(s) ]<\/h4>\n<p>Take for example the set\u00a0<code>[al1]<\/code>, here awk will match all strings containing character\u00a0<code>a<\/code>\u00a0or\u00a0<code>l<\/code>\u00a0or\u00a0<code>1<\/code>\u00a0in a line in the file\u00a0<strong>\/etc\/hosts<\/strong>.<\/p>\n<pre># awk '<strong>\/[al1]\/<\/strong>{print}' \/etc\/hosts\r\n<\/pre>\n<div id=\"attachment_19814\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/04\/Use-Awk-to-Print-Matching-Character.gif\" rel=\"attachment wp-att-19814\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-19814\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/04\/Use-Awk-to-Print-Matching-Character.gif\" alt=\"Use-Awk to Print Matching Character in File\" width=\"674\" height=\"288\" aria-describedby=\"caption-attachment-19814\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-19814\" class=\"wp-caption-text\">Use-Awk to Print Matching Character in File<\/p>\n<\/div>\n<p>The next example matches strings starting with either\u00a0<code>K<\/code>\u00a0or\u00a0<code>k<\/code>\u00a0followed by\u00a0<code>T<\/code>:<\/p>\n<pre># awk '<strong>\/[Kk]T\/<\/strong>{print}' \/etc\/hosts \r\n<\/pre>\n<div id=\"attachment_19815\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/04\/Use-Awk-to-Print-Matched-String-in-File.gif\" rel=\"attachment wp-att-19815\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-19815\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/04\/Use-Awk-to-Print-Matched-String-in-File.gif\" alt=\"Use Awk to Print Matched String in File\" width=\"592\" height=\"73\" aria-describedby=\"caption-attachment-19815\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-19815\" class=\"wp-caption-text\">Use Awk to Print Matched String in File<\/p>\n<\/div>\n<h4>Specifying Characters in a Range<\/h4>\n<p>Understand characters with awk:<\/p>\n<ol>\n<li><code>[0-9]<\/code>\u00a0means a single number<\/li>\n<li><code>[a-z]<\/code>\u00a0means match a single lower case letter<\/li>\n<li><code>[A-Z]<\/code>\u00a0means match a single upper case letter<\/li>\n<li><code>[a-zA-Z]<\/code>\u00a0means match a single letter<\/li>\n<li><code>[a-zA-Z 0-9]<\/code>\u00a0means match a single letter or number<\/li>\n<\/ol>\n<p>Lets look at an example below:<\/p>\n<pre># awk '<strong>\/[0-9]\/<\/strong>{print}' \/etc\/hosts \r\n<\/pre>\n<div id=\"attachment_19816\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/04\/Use-Awk-To-Print-Matching-Numbers-in-File.gif\" rel=\"attachment wp-att-19816\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-19816\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/04\/Use-Awk-To-Print-Matching-Numbers-in-File.gif\" alt=\"Use Awk To Print Matching Numbers in File\" width=\"676\" height=\"310\" aria-describedby=\"caption-attachment-19816\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-19816\" class=\"wp-caption-text\">Use Awk To Print Matching Numbers in File<\/p>\n<\/div>\n<p>All the line from the file\u00a0<strong>\/etc\/hosts<\/strong>\u00a0contain at least a single number\u00a0<code>[0-9]<\/code>\u00a0in the above example.<\/p>\n<h4>Use Awk with (^) Meta Character<\/h4>\n<p>It matches all the lines that start with the pattern provided as in the example below:<\/p>\n<pre># awk '<strong>\/^fe\/<\/strong>{print}' \/etc\/hosts\r\n# awk '<strong>\/^ff\/<\/strong>{print}' \/etc\/hosts\r\n<\/pre>\n<div id=\"attachment_19817\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/04\/Use-Awk-to-Print-All-Matching-Lines-with-Pattern.gif\" rel=\"attachment wp-att-19817\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-19817\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/04\/Use-Awk-to-Print-All-Matching-Lines-with-Pattern.gif\" alt=\"Use Awk to Print All Matching Lines with Pattern\" width=\"573\" height=\"174\" aria-describedby=\"caption-attachment-19817\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-19817\" class=\"wp-caption-text\">Use Awk to Print All Matching Lines with Pattern<\/p>\n<\/div>\n<h4>Use Awk with ($) Meta Character<\/h4>\n<p>It matches all the lines that end with the pattern provided:<\/p>\n<pre># awk '<strong>\/ab$\/<\/strong>{print}' \/etc\/hosts\r\n# awk '<strong>\/ost$\/<\/strong>{print}' \/etc\/hosts\r\n# awk '<strong>\/rs$\/<\/strong>{print}' \/etc\/hosts\r\n<\/pre>\n<div id=\"attachment_19818\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/04\/Use-Awk-to-Print-Given-Pattern-String.gif\" rel=\"attachment wp-att-19818\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-19818\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/04\/Use-Awk-to-Print-Given-Pattern-String.gif\" alt=\"Use Awk to Print Given Pattern String\" width=\"586\" height=\"185\" aria-describedby=\"caption-attachment-19818\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-19818\" class=\"wp-caption-text\">Use Awk to Print Given Pattern String<\/p>\n<\/div>\n<h4>Use Awk with (\\) Escape Character<\/h4>\n<p>It allows you to take the character following it as a literal that is to say consider it just as it is.<\/p>\n<p>In the example below, the first command prints out all line in the file, the second command prints out nothing because I want to match a line that has\u00a0<strong>$25.00<\/strong>, but no escape character is used.<\/p>\n<p>The third command is correct since a an escape character has been used to read\u00a0<strong>$<\/strong>\u00a0as it is.<\/p>\n<pre># awk '<strong>\/\/<\/strong>{print}' deals.txt\r\n# awk '<strong>\/$25.00\/<\/strong>{print}' deals.txt\r\n# awk '<strong>\/\\.00\/<\/strong>{print}' deals.txt\r\n<\/pre>\n<div id=\"attachment_19819\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/04\/Use-Awk-with-Escape-Character.gif\" rel=\"attachment wp-att-19819\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-19819\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/04\/Use-Awk-with-Escape-Character.gif\" alt=\"Use Awk with Escape Character\" width=\"619\" height=\"211\" aria-describedby=\"caption-attachment-19819\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-19819\" class=\"wp-caption-text\">Use Awk with Escape Character<\/p>\n<\/div>\n<h3>Summary<\/h3>\n<p>That is not all with the\u00a0<strong>awk<\/strong>\u00a0command line filtering tool, the examples above a the basic operations of awk. In the next parts we shall be advancing on how to use complex features of awk. Thanks for reading through and for any additions or clarifications, post a comment in the comments section.<\/p>\n<h1 class=\"post-title\">How to Use Awk to Print Fields and Columns in File<\/h1>\n<p>In this part of our\u00a0<a href=\"https:\/\/www.tecmint.com\/tag\/awk-command\/\" target=\"_blank\" rel=\"noopener\">Linux Awk command series<\/a>, we shall have a look at one of the most important features of\u00a0<strong>Awk<\/strong>, which is field editing.<\/p>\n<p>It is good to know that\u00a0<strong>Awk<\/strong>\u00a0automatically divides input lines provided to it into fields, and a field can be defined as a set of characters that are separated from other fields by an internal field separator.<\/p>\n<div id=\"attachment_20064\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/04\/Awk-Print-Fields-and-Columns.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-20064\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/04\/Awk-Print-Fields-and-Columns.png\" alt=\"Awk Print Fields and Columns\" width=\"720\" height=\"345\" aria-describedby=\"caption-attachment-20064\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-20064\" class=\"wp-caption-text\">Awk Print Fields and Columns<\/p>\n<\/div>\n<p>If you are familiar with the Unix\/Linux or do\u00a0<a href=\"https:\/\/www.tecmint.com\/category\/bash-shell\/\" target=\"_blank\" rel=\"noopener\">bash shell programming<\/a>, then you should know what internal field separator (<strong>IFS<\/strong>) variable is. The default IFS in Awk are tab and space.<\/p>\n<p>This is how the idea of field separation works in\u00a0<strong>Awk<\/strong>: when it encounters an input line, according to the IFS defined, the first set of characters is field one, which is accessed using\u00a0<strong>$1<\/strong>, the second set of characters is field two, which is accessed using\u00a0<strong>$2<\/strong>, the third set of characters is field three, which is accessed using\u00a0<strong>$3<\/strong>\u00a0and so forth till the last set of character(s).<\/p>\n<p>To understand this Awk field editing better, let us take a look at the examples below:<\/p>\n<p><strong>Example 1:<\/strong>\u00a0I have created a text file called\u00a0<strong>tecmintinfo.txt<\/strong>.<\/p>\n<pre># vi tecmintinfo.txt\r\n# cat tecmintinfo.txt\r\n<\/pre>\n<div id=\"attachment_20060\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/04\/Create-File-in-Linux.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-20060\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/04\/Create-File-in-Linux.png\" alt=\"Create File in Linux\" width=\"602\" height=\"86\" aria-describedby=\"caption-attachment-20060\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-20060\" class=\"wp-caption-text\">Create File in Linux<\/p>\n<\/div>\n<p>Then from the command line, I try to print the\u00a0<strong>first<\/strong>,\u00a0<strong>second<\/strong>\u00a0and\u00a0<strong>third<\/strong>\u00a0fields from the file\u00a0<strong>tecmintinfo.txt<\/strong>\u00a0using the command below:<\/p>\n<pre><strong>$ awk '\/\/{print $1 $2 $3 }' tecmintinfo.txt<\/strong>\r\n\r\nTecMint.comisthe\r\n<\/pre>\n<p>From the output above, you can see that the characters from the first three fields are printed based on the\u00a0<strong>IFS<\/strong>defined which is space:<\/p>\n<ol>\n<li>Field one which is\u00a0<strong>\u201cTecMint.com\u201d<\/strong>\u00a0is accessed using\u00a0<code>$1<\/code>.<\/li>\n<li>Field two which is\u00a0<strong>\u201cis\u201d<\/strong>\u00a0is accessed using\u00a0<code>$2<\/code>.<\/li>\n<li>Field three which is\u00a0<strong>\u201cthe\u201d<\/strong>\u00a0is accessed using\u00a0<code>$3<\/code>.<\/li>\n<\/ol>\n<p>If you have noticed in the printed output, the field values are not separated and this is how print behaves by default.<\/p>\n<p>To view the output clearly with space between the field values, you need to add\u00a0<code>(,)<\/code>\u00a0operator as follows:<\/p>\n<pre><strong>$ awk '\/\/{print $1, $2, $3; }' tecmintinfo.txt<\/strong>\r\n\r\nTecMint.com is the\r\n<\/pre>\n<p>One important thing to note and always remember is that the use of\u00a0<code>($)<\/code>\u00a0in\u00a0<strong>Awk<\/strong>\u00a0is different from its use in shell scripting.<\/p>\n<p>Under shell scripting\u00a0<code>($)<\/code>\u00a0is used to access the value of variables while in\u00a0<strong>Awk<\/strong>\u00a0<code>($)<\/code>\u00a0it is used only when accessing the contents of a field but not for accessing the value of variables.<\/p>\n<p><strong>Example 2:<\/strong>\u00a0Let us take a look at one other example using a file which contains multiple lines called\u00a0<strong>my_shoping.list<\/strong>.<\/p>\n<pre>No\tItem_Name\t\tUnit_Price\tQuantity\tPrice\r\n1\tMouse\t\t\t#20,000\t\t   1\t\t#20,000\r\n2 \tMonitor\t\t\t#500,000\t   1\t\t#500,000\r\n3\tRAM_Chips\t\t#150,000\t   2\t\t#300,000\r\n4\tEthernet_Cables\t        #30,000\t\t   4\t\t#120,000\t\t\r\n<\/pre>\n<p>Say you wanted to only print\u00a0<code>Unit_Price<\/code>\u00a0of each item on the shopping list, you will need to run the command below:<\/p>\n<pre><strong>$ awk '\/\/{print $2, $3 }' my_shopping.txt <\/strong>\r\n\r\nItem_Name Unit_Price\r\nMouse #20,000\r\nMonitor #500,000\r\nRAM_Chips #150,000\r\nEthernet_Cables #30,000\r\n<\/pre>\n<p><strong>Awk<\/strong>\u00a0also has a\u00a0<code>printf<\/code>\u00a0command that helps you to format your output is a nice way as you can see the above output is not clear enough.<\/p>\n<p>Using\u00a0<code>printf<\/code>\u00a0to format output of the\u00a0<strong>Item_Name<\/strong>\u00a0and\u00a0<strong>Unit_Price<\/strong>:<\/p>\n<pre><strong>$ awk '\/\/{printf \"%-10s %s\\n\",$2, $3 }' my_shopping.txt<\/strong> \r\n\r\nItem_Name  Unit_Price\r\nMouse      #20,000\r\nMonitor    #500,000\r\nRAM_Chips  #150,000\r\nEthernet_Cables #30,000\r\n<\/pre>\n<h3>Summary<\/h3>\n<p>Field editing is very important when using\u00a0<strong>Awk<\/strong>\u00a0to filter text or strings, it helps you get particular data in columns in a list. And always remember that the use of\u00a0<code>($)<\/code>\u00a0operator in\u00a0<strong>Awk<\/strong>\u00a0is different from that in shell scripting.<\/p>\n<p>I hope the article was helpful to you and for any additional information required or questions, you can post a comment in the comment section.<\/p>\n<h1 class=\"post-title\">How to Use Awk to Filter Text or Strings Using Pattern Specific Actions<\/h1>\n<p>In the third part of the\u00a0<a href=\"https:\/\/www.tecmint.com\/tag\/awk-command\/\" target=\"_blank\" rel=\"noopener\">Awk command series<\/a>, we shall take a look at filtering text or strings based on specific patterns that a user can define.<\/p>\n<p>Sometimes, when filtering text, you want to indicate certain lines from an input file or lines of strings based on a given condition or using a specific pattern that can be matched. Doing this with\u00a0<strong>Awk<\/strong>\u00a0is very easy, it is one of the great features of\u00a0<strong>Awk<\/strong>\u00a0that you will find helpful.<\/p>\n<p>Let us take a look at an example below, say you have a shopping list for food items that you want to buy, called\u00a0<strong>food_prices.list<\/strong>. It has the following list of food items and their prices.<\/p>\n<pre><strong>$ cat food_prices.list <\/strong>\r\nNo\tItem_Name\t\tQuantity\tPrice\r\n1\tMangoes\t\t\t   10\t\t$2.45\r\n2\tApples\t\t\t   20\t\t$1.50\r\n3\tBananas\t\t\t   5\t\t$0.90\r\n4\tPineapples\t\t   10\t\t$3.46\r\n5\tOranges\t\t\t   10\t\t$0.78\r\n6\tTomatoes\t\t   5\t\t$0.55\r\n7\tOnions\t\t\t   5            $0.45\r\n<\/pre>\n<p>And then, you want to indicate a\u00a0<code>(*)<\/code>\u00a0sign on food items whose price is greater than\u00a0<code>$2<\/code>, this can be done by running the following command:<\/p>\n<pre>$ awk '\/ *$[2-9]\\.[0-9][0-9] *\/ { print $1, $2, $3, $4, \"*\" ; } \/ *$[0-1]\\.[0-9][0-9] *\/ { print ; }' food_prices.list\r\n<\/pre>\n<div id=\"attachment_20349\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/04\/Filter-and-Print-Text-Using-Awk.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-20349\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/04\/Filter-and-Print-Text-Using-Awk.gif\" sizes=\"auto, (max-width: 812px) 100vw, 812px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/04\/Filter-and-Print-Text-Using-Awk.gif 812w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/04\/Filter-and-Print-Text-Using-Awk-768x185.gif 768w\" alt=\"Print Items Whose Price is Greater Than $2\" width=\"812\" height=\"196\" aria-describedby=\"caption-attachment-20349\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-20349\" class=\"wp-caption-text\">Print Items Whose Price is Greater Than $2<\/p>\n<\/div>\n<p>From the output above, you can see that the there is a\u00a0<code>(*)<\/code>\u00a0sign at the end of the lines having food items,\u00a0<strong>mangoes<\/strong>\u00a0and\u00a0<strong>pineapples<\/strong>. If you check their prices, they are above\u00a0<strong>$2<\/strong>.<\/p>\n<p>In this example, we have used used two patterns:<\/p>\n<ol>\n<li>the first:\u00a0<code>\/ *\\$[2-9]\\.[0-9][0-9] *\/<\/code>\u00a0gets the lines that have food item price greater than\u00a0<strong>$2<\/strong>\u00a0and<\/li>\n<li>the second:\u00a0<code>\/*\\$[0-1]\\.[0-9][0-9] *\/<\/code>\u00a0looks for lines with food item price less than\u00a0<strong>$2<\/strong>.<\/li>\n<\/ol>\n<p>This is what happens, there are four fields in the file, when pattern one encounters a line with food item price greater than\u00a0<strong>$2<\/strong>, it prints all the four fields and a\u00a0<code>(*)<\/code>\u00a0sign at the end of the line as a flag.<\/p>\n<p>The second pattern simply prints the other lines with food price less than\u00a0<strong>$2<\/strong>\u00a0as they appear in the input file,\u00a0<strong>food_prices.list<\/strong>.<\/p>\n<p>This way you can use pattern specific actions to filter out food items that are priced above\u00a0<strong>$2<\/strong>, though there is a problem with the output, the lines that have the\u00a0<code>(*)<\/code>\u00a0sign are not formatted out like the rest of the lines making the output not clear enough.<\/p>\n<p>We saw the same problem in\u00a0<a href=\"https:\/\/www.tecmint.com\/awk-print-fields-columns-with-space-separator\/\" target=\"_blank\" rel=\"noopener\">Part 2 of the awk series<\/a>, but we can solve it in two ways:<\/p>\n<p><strong>1.<\/strong>\u00a0Using\u00a0<strong>printf<\/strong>\u00a0command which is a long and boring way using the command below:<\/p>\n<pre>$ awk '\/ *$[2-9]\\.[0-9][0-9] *\/ { printf \"%-10s %-10s %-10s %-10s\\n\", $1, $2, $3, $4 \"*\" ; } \/ *$[0-1]\\.[0-9][0-9] *\/ { printf \"%-10s %-10s %-10s %-10s\\n\", $1, $2, $3, $4; }' food_prices.list \r\n<\/pre>\n<div id=\"attachment_20350\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/04\/Filter-and-Print-Items-Using-Awk-and-Printf.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-20350\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/04\/Filter-and-Print-Items-Using-Awk-and-Printf.gif\" sizes=\"auto, (max-width: 812px) 100vw, 812px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/04\/Filter-and-Print-Items-Using-Awk-and-Printf.gif 812w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/04\/Filter-and-Print-Items-Using-Awk-and-Printf-768x206.gif 768w\" alt=\"Filter and Print Items Using Awk and Printf\" width=\"812\" height=\"218\" aria-describedby=\"caption-attachment-20350\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-20350\" class=\"wp-caption-text\">Filter and Print Items Using Awk and Printf<\/p>\n<\/div>\n<p><strong>2.<\/strong>\u00a0Using\u00a0<strong>$0<\/strong>\u00a0field.\u00a0<strong>Awk<\/strong>\u00a0uses the variable\u00a0<strong>0<\/strong>\u00a0to store the whole input line. This is handy for solving the problem above and it is simple and fast as follows:<\/p>\n<pre>$ awk '\/ *$[2-9]\\.[0-9][0-9] *\/ { print $0 \"*\" ; } \/ *$[0-1]\\.[0-9][0-9] *\/ { print ; }' food_prices.list \r\n<\/pre>\n<div id=\"attachment_20351\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/04\/Filter-and-Print-Items-Using-Awk-and-Variable.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-20351\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/04\/Filter-and-Print-Items-Using-Awk-and-Variable.gif\" sizes=\"auto, (max-width: 812px) 100vw, 812px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/04\/Filter-and-Print-Items-Using-Awk-and-Variable.gif 812w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/04\/Filter-and-Print-Items-Using-Awk-and-Variable-768x196.gif 768w\" alt=\"Filter and Print Items Using Awk and Variable\" width=\"812\" height=\"207\" aria-describedby=\"caption-attachment-20351\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-20351\" class=\"wp-caption-text\">Filter and Print Items Using Awk and Variable<\/p>\n<\/div>\n<h3>Conclusion<\/h3>\n<p>That\u2019s it for now and these are simple ways of filtering text using pattern specific action that can help in flagging lines of text or strings in a file using\u00a0<strong>Awk<\/strong>\u00a0command.<\/p>\n<p>Hope you find this article helpful and remember to read the next part of the series which will focus on using comparison operators using awk tool.<\/p>\n<h1 class=\"post-title\">How to Use Comparison Operators with Awk in Linux \u2013 Part 4<\/h1>\n<p>When dealing with numerical or string values in a line of text, filtering text or strings using comparison operators comes in handy for\u00a0<strong>Awk<\/strong>\u00a0command users.<\/p>\n<p>In this part of the\u00a0<a href=\"https:\/\/www.tecmint.com\/use-linux-awk-command-to-filter-text-string-in-files\/\" target=\"_blank\" rel=\"noopener\">Awk series<\/a>, we shall take a look at how you can filter text or strings using comparison operators. If you are a programmer then you must already be familiar with comparison operators but those who are not, let me explain in the section below.<\/p>\n<h4>What are Comparison operators in Awk?<\/h4>\n<p>Comparison operators in Awk are used to compare the value of numbers or strings and they include the following:<\/p>\n<ol>\n<li><code>&gt;<\/code>\u00a0\u2013 greater than<\/li>\n<li><code>&lt;<\/code>\u00a0\u2013 less than<\/li>\n<li><code>&gt;=<\/code>\u00a0\u2013 greater than or equal to<\/li>\n<li><code>&lt;=<\/code>\u00a0\u2013 less than or equal to<\/li>\n<li><code>==<\/code>\u00a0\u2013 equal to<\/li>\n<li><code>!=<\/code>\u00a0\u2013 not equal to<\/li>\n<li><code>some_value ~ \/ pattern\/<\/code>\u00a0\u2013 true if some_value matches pattern<\/li>\n<li><code>some_value !~ \/ pattern\/<\/code>\u00a0\u2013 true if some_value does not match pattern<\/li>\n<\/ol>\n<p>Now that we have looked at the various comparison operators in Awk, let us understand them better using an example.<\/p>\n<p>In this example, we have a file named\u00a0<strong>food_list.txt<\/strong>\u00a0which is a shopping list for different food items and I would like to flag food items whose quantity is less than or equal 20 by adding\u00a0<code>(**)<\/code>\u00a0at the end of each line.<\/p>\n<div class=\"code-label\" title=\"File - food_list.txt\">File \u2013 food_list.txt<\/div>\n<pre>No      Item_Name               Quantity        Price\r\n1       Mangoes                    45           $3.45\r\n2       Apples                     25           $2.45\r\n3       Pineapples                 5            $4.45\r\n4       Tomatoes                   25           $3.45\r\n5       Onions                     15           $1.45\r\n6       Bananas                    30           $3.45\r\n<\/pre>\n<p>The general syntax for using comparison operators in Awk is:<\/p>\n<pre># expression { actions; }\r\n<\/pre>\n<p>To achieve the above goal, I will have to run the command below:<\/p>\n<pre><strong># awk '$3 &lt;= 30 { printf \"%s\\t%s\\n\", $0,\"**\" ; } $3 &gt; 30 { print $0 ;}' food_list.txt<\/strong>\r\n\r\nNo\tItem_Name`\t\tQuantity\tPrice\r\n1\tMangoes\t      \t\t   45\t\t$3.45\r\n2\tApples\t\t\t   25\t\t$2.45\t**\r\n3\tPineapples\t\t   5\t\t$4.45\t**\r\n4\tTomatoes\t\t   25\t\t$3.45\t**\r\n5\tOnions\t\t\t   15           $1.45\t**\r\n6\tBananas\t\t\t   30           $3.45\t**\r\n<\/pre>\n<p>In the above example, there are two important things that happen:<\/p>\n<ol>\n<li>The first expression\u00a0<strong>{ action ; }<\/strong>\u00a0combination,\u00a0<strong>$3 &lt;= 30<\/strong>\u00a0<strong>{ printf \u201c%s\\t%s\\n\u201d, $0,\u201d**\u201d ; }<\/strong>\u00a0prints out lines with quantity less than or equal to\u00a0<strong>30<\/strong>\u00a0and adds a\u00a0<strong>(**)<\/strong>\u00a0at the end of each line. The value of quantity is accessed using\u00a0<strong>$3<\/strong>\u00a0field variable.<\/li>\n<li>The second expression\u00a0<strong>{ action ; }<\/strong>\u00a0combination,\u00a0<strong>$3 &gt; 30<\/strong>\u00a0<strong>{ print $0 ;}<\/strong>\u00a0prints out lines unchanged since their quantity is greater then\u00a0<strong>30<\/strong>.<\/li>\n<\/ol>\n<p>One more example:<\/p>\n<pre><strong># awk '$3 &lt;= 20 { printf \"%s\\t%s\\n\", $0,\"TRUE\" ; } $3 &gt; 20  { print $0 ;} ' food_list.txt <\/strong>\r\n\r\nNo\tItem_Name\t\tQuantity\tPrice\r\n1\tMangoes\t\t\t   45\t\t$3.45\r\n2\tApples\t\t\t   25\t\t$2.45\r\n3\tPineapples\t\t   5\t\t$4.45\tTRUE\r\n4\tTomatoes\t\t   25\t\t$3.45\r\n5\tOnions\t\t\t   15           $1.45\tTRUE\r\n6       Bananas\t                   30           $3.45\r\n<\/pre>\n<p>In this example, we want to indicate lines with quantity less or equal to\u00a0<strong>20<\/strong>\u00a0with the word\u00a0<strong>(TRUE)<\/strong>\u00a0at the end.<\/p>\n<h3>Summary<\/h3>\n<p>This is an introductory tutorial to comparison operators in\u00a0<strong>Awk<\/strong>, therefore you need to try out many other options and discover more.<\/p>\n<p>In case of any problems you face or any additions that you have in mind, then drop a comment in the comment section below. Remember to read the next part of the Awk series where I will take you through compound expressions.<\/p>\n<h1 class=\"post-title\">How to Use Compound Expressions with Awk in Linux \u2013 Part 5<\/h1>\n<p>All along, we have been looking at simple expressions when checking whether a condition has been meet or not. What if you want to use more then one expression to check for a particular condition in?<\/p>\n<p>In this article, we shall take a look at the how you can combine multiple expressions referred to as compound expressions to check for a condition when filtering text or strings.<\/p>\n<p>In\u00a0<strong>Awk<\/strong>,\u00a0<strong>compound expressions<\/strong>\u00a0are built using the\u00a0<code>&amp;&amp;<\/code>\u00a0referred to as\u00a0<code>(and)<\/code>\u00a0and the\u00a0<code>||<\/code>\u00a0referred to as\u00a0<code>(or)<\/code>compound operators.<\/p>\n<p>The general syntax for compound expressions is:<\/p>\n<pre>( first_expression ) &amp;&amp; ( second_expression )\r\n<\/pre>\n<p>Here,\u00a0<code>first_expression<\/code>\u00a0and\u00a0<code>second_expression<\/code>\u00a0must be true to make the whole expression true.<\/p>\n<pre>( first_expression ) || ( second_expression) \r\n<\/pre>\n<p>Here, one of the expressions either\u00a0<code>first_expression<\/code>\u00a0or\u00a0<code>second_expression<\/code>\u00a0must be true for the whole expression to be true.<\/p>\n<p><strong>Caution<\/strong>: Remember to always include the parenthesis.<\/p>\n<p>The expressions can be built using the comparison operators that we looked at in\u00a0<a href=\"https:\/\/www.tecmint.com\/comparison-operators-in-awk\" target=\"_blank\" rel=\"noopener\">Part 4 of the awk series<\/a>.<\/p>\n<p>Let us now get a clear understanding using an example below:<\/p>\n<p>In this example, a have a text file named\u00a0<code>tecmint_deals.txt<\/code>, which contains a list of some amazing random Tecmint deals, it includes the name of the deal, the price and type.<\/p>\n<div class=\"code-label\" title=\"TecMint Deal List\">TecMint Deal List<\/div>\n<pre>No      Name                                    Price           Type\r\n1       Mac_OS_X_Cleanup_Suite                  $9.99           Software\r\n2       Basics_Notebook                         $14.99          Lifestyle\r\n3       Tactical_Pen                            $25.99          Lifestyle\r\n4       Scapple                                 $19.00          Unknown\r\n5       Nano_Tool_Pack                          $11.99          Unknown\r\n6       Ditto_Bluetooth_Altering_Device         $33.00          Tech\r\n7       Nano_Prowler_Mini_Drone                 $36.99          Tech \r\n<\/pre>\n<p>Say that we want only print and flag deals that are above\u00a0<strong>$20<\/strong>\u00a0and of type\u00a0<strong>\u201cTech\u201d<\/strong>\u00a0using the\u00a0<code>(**)<\/code>\u00a0sign at the end of each line.<\/p>\n<p>We shall need to run the command below.<\/p>\n<pre><strong># awk '($3 ~ \/^$[2-9][0-9]*\\.[0-9][0-9]$\/) &amp;&amp; ($4==\"Tech\") { printf \"%s\\t%s\\n\",$0,\"*\"; } ' tecmint_deals.txt <\/strong>\r\n\r\n6\tDitto_Bluetooth_Altering_Device\t\t<strong>$33.00<\/strong>\t\t<strong>Tech\t*<\/strong>\r\n7\tNano_Prowler_Mini_Drone\t\t\t<strong>$36.99<\/strong>          <strong>Tech\t *<\/strong>\r\n<\/pre>\n<p>In this example, we have used two expressions in a compound expression:<\/p>\n<ol>\n<li>First expression,\u00a0<strong>($3 ~ \/^\\$[2-9][0-9]*\\.[0-9][0-9]$\/)<\/strong>\u00a0; checks the for lines with deals with price above\u00a0<strong>$20<\/strong>, and it is only true if the value of\u00a0<strong>$3<\/strong>\u00a0which is the price matches the pattern\u00a0<strong>\/^\\$[2-9][0-9]*\\.[0-9][0-9]$\/<\/strong><\/li>\n<li>And the second expression,\u00a0<strong>($4 == \u201cTech\u201d)<\/strong>\u00a0; checks whether the deal is of type\u00a0<strong>\u201cTech\u201d<\/strong>\u00a0and it is only true if the value of\u00a0<strong>$4<\/strong>\u00a0equals to\u00a0<strong>\u201cTech\u201d<\/strong>.<\/li>\n<\/ol>\n<p>Remember, a line will only be flagged with the\u00a0<code>(**)<\/code>, if\u00a0<strong>first expression<\/strong>\u00a0and\u00a0<strong>second expression<\/strong>\u00a0are true as states the principle of the\u00a0<code>&amp;&amp;<\/code>\u00a0operator.<\/p>\n<h3>Summary<\/h3>\n<p>Some conditions always require building compound expressions for you to match exactly what you want. When you understand the use of comparison and compound expression operators then, filtering text or strings based on some difficult conditions will become easy.<\/p>\n<p>Hope you find this guide useful and for any questions or additions, always remember to leave a comment and your concern will be solved accordingly.<\/p>\n<h1 class=\"post-title\">How to Use \u2018next\u2019 Command with Awk in Linux \u2013 Part 6<\/h1>\n<p>In this sixth part of\u00a0<a href=\"https:\/\/www.tecmint.com\/use-linux-awk-command-to-filter-text-string-in-files\/\" target=\"_blank\" rel=\"noopener\">Awk series<\/a>, we shall look at using\u00a0<code>next<\/code>\u00a0command, which tells\u00a0<strong>Awk<\/strong>\u00a0to skip all remaining patterns and expressions that you have provided, but instead read the next input line.<\/p>\n<p>The\u00a0<code>next<\/code>\u00a0command helps you to prevent executing what I would refer to as time-wasting steps in a command execution.<\/p>\n<p>To understand how it works, let us consider a file called\u00a0<strong>food_list.txt<\/strong>\u00a0that looks like this:<\/p>\n<div class=\"code-label\" title=\"Food List Items\">Food List Items<\/div>\n<pre>No      Item_Name               Price           Quantity\r\n1       Mangoes                 $3.45              5\r\n2       Apples                  $2.45              25\r\n3       Pineapples              $4.45              55\r\n4       Tomatoes                $3.45              25\r\n5       Onions                  $1.45              15\r\n6       Bananas                 $3.45              30\r\n<\/pre>\n<p>Consider running the following command that will flag food items whose quantity is less than or equal to\u00a0<strong>20<\/strong>with a\u00a0<code>(*)<\/code>\u00a0sign at the end of each line:<\/p>\n<pre><strong># awk '$4 &lt;= 20 { printf \"%s\\t%s\\n\", $0,\"*\" ; } $4 &gt; 20 { print $0 ;} ' food_list.txt<\/strong> \r\n\r\nNo\tItem_Name\t\tPrice\t\tQuantity\r\n1\tMangoes\t\t\t$3.45\t\t   <strong>5<\/strong>\t<strong>*<\/strong>\r\n2\tApples\t\t\t$2.45              25\r\n3\tPineapples\t\t$4.45              55\r\n4\tTomatoes\t\t$3.45              25 \r\n5\tOnions\t\t\t$1.45              <strong>15<\/strong>\t<strong>*<\/strong>\r\n6\tBananas\t                $3.45              30\r\n<\/pre>\n<p>The command above actually works as follows:<\/p>\n<ol>\n<li>First, it checks whether the quantity, fourth field of each input line is less than or equal to\u00a0<strong>20<\/strong>, if a value meets that condition, it is printed and flagged with the\u00a0<code>(*)<\/code>\u00a0sign at the end using expression one:\u00a0<code>$4 &lt;= 20<\/code><\/li>\n<li>Secondly, it checks if the fourth field of each input line is greater than\u00a0<strong>20<\/strong>, and if a line meets the condition it gets printed using expression two:\u00a0<code>$4 &gt; 20<\/code><\/li>\n<\/ol>\n<p>But there is one problem here, when the\u00a0<strong>first expression<\/strong>\u00a0is executed, a line that we want to flag is printed using:\u00a0<code>{ printf \"%s\\t%s\\n\", $0,\"**\" ; }<\/code>\u00a0and then in the same step, the\u00a0<strong>second expression<\/strong>\u00a0is also checked which becomes a time wasting factor.<\/p>\n<p>So there is no need to execute the second expression,\u00a0<code>$4 &gt; 20<\/code>\u00a0again after printing already flagged lines that have been printed using the first expression.<\/p>\n<p>To deal with this problem, you have to use the\u00a0<code>next<\/code>\u00a0command as follows:<\/p>\n<pre><strong># awk '$4 &lt;= 20 { printf \"%s\\t%s\\n\", $0,\"*\" ; next; } $4 &gt; 20 { print $0 ;} ' food_list.txt<\/strong>\r\n\r\nNo\tItem_Name\t\tPrice\t\tQuantity\r\n1\tMangoes\t\t\t$3.45\t\t   <strong>5<\/strong>\t<strong>*<\/strong>\r\n2\tApples\t\t\t$2.45              25\r\n3\tPineapples\t\t$4.45              55\r\n4\tTomatoes\t\t$3.45              25 \r\n5\tOnions\t\t\t$1.45              <strong>15<\/strong>\t<strong>*<\/strong>\r\n6\tBananas\t                $3.45              30\r\n<\/pre>\n<p>After a single input line is printed using\u00a0<code>$4 &lt;= 20<\/code>\u00a0<code>{ printf \"%s\\t%s\\n\", $0,\"*\" ; next ; }<\/code>, the\u00a0<code>next<\/code>\u00a0command included will help skip the second expression\u00a0<code>$4 &gt; 20<\/code>\u00a0<code>{ print $0 ;}<\/code>, so execution goes to the next input line without having to waste time on checking whether the quantity is greater than\u00a0<strong>20<\/strong>.<\/p>\n<p>The\u00a0<strong>next<\/strong>\u00a0command is very important is writing efficient commands and where necessary, you can always use to speed up the execution of a script. Prepare for the next part of the series where we shall look at using standard input\u00a0<strong>(STDIN)<\/strong>\u00a0as input for\u00a0<strong>Awk<\/strong>.<\/p>\n<p>Hope you find this how to guide helpful and you can as always put your thoughts in writing by leaving a comment in the comment section below.<\/p>\n<h1 class=\"post-title\">How to Read Awk Input from STDIN in Linux \u2013 Part 7<\/h1>\n<p>In the previous parts of the\u00a0<a href=\"https:\/\/www.tecmint.com\/use-linux-awk-command-to-filter-text-string-in-files\/\" target=\"_blank\" rel=\"noopener\">Awk tool series<\/a>, we looked at reading input mostly from a file(s), but what if you want to read input from\u00a0<strong>STDIN<\/strong>.<\/p>\n<p>In this Part 7 of Awk series, we shall look at few examples where you can filter the output of other commands instead of reading input from a file.<\/p>\n<p>We shall start with the\u00a0<a href=\"https:\/\/www.tecmint.com\/linux-dir-command-usage-with-examples\/\" target=\"_blank\" rel=\"noopener\">dir utility<\/a>\u00a0that works similar to\u00a0<a href=\"https:\/\/www.tecmint.com\/15-basic-ls-command-examples-in-linux\/\" target=\"_blank\" rel=\"noopener\">ls command<\/a>, in the first example below, we use the output of\u00a0<code>dir -l<\/code>\u00a0command as input for Awk to print owner\u2019s username, groupname and the files he\/she owns in the current directory:<\/p>\n<pre># dir -l | awk '{print $3, $4, $9;}'\r\n<\/pre>\n<div id=\"attachment_20897\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/06\/List-Files-Owned-By-User-in-Directory.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-20897\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/06\/List-Files-Owned-By-User-in-Directory.png\" sizes=\"auto, (max-width: 801px) 100vw, 801px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/06\/List-Files-Owned-By-User-in-Directory.png 801w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/06\/List-Files-Owned-By-User-in-Directory-768x435.png 768w\" alt=\"List Files Owned By User in Directory\" width=\"801\" height=\"454\" aria-describedby=\"caption-attachment-20897\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-20897\" class=\"wp-caption-text\">List Files Owned By User in Directory<\/p>\n<\/div>\n<p>Take a look at another example where we\u00a0<a href=\"https:\/\/www.tecmint.com\/combine-multiple-expressions-in-awk\" target=\"_blank\" rel=\"noopener\">employ awk expressions<\/a>, here, we want to print files owned by the\u00a0<strong>root<\/strong>\u00a0user by using an expression to filter strings as in the awk command below:<\/p>\n<pre># dir -l | awk '$3==\"root\" {print $1,$3,$4, $9;} '\r\n<\/pre>\n<div id=\"attachment_20898\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/06\/List-Files-Owned-by-Root-User.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-20898\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/06\/List-Files-Owned-by-Root-User.png\" sizes=\"auto, (max-width: 801px) 100vw, 801px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/06\/List-Files-Owned-by-Root-User.png 801w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/06\/List-Files-Owned-by-Root-User-768x80.png 768w\" alt=\"List Files Owned by Root User\" width=\"801\" height=\"83\" aria-describedby=\"caption-attachment-20898\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-20898\" class=\"wp-caption-text\">List Files Owned by Root User<\/p>\n<\/div>\n<p>The command above includes the\u00a0<code>(==)<\/code>\u00a0comparison operator to help us filter out files in the current directory which are owned by the root user. This is achieved using the expression\u00a0<code>$3==\u201droot\u201d<\/code>.<\/p>\n<p>Let us look at another example of where we use a\u00a0<a href=\"https:\/\/www.tecmint.com\/comparison-operators-in-awk\" target=\"_blank\" rel=\"noopener\">awk comparison operator<\/a>\u00a0to match a certain string.<\/p>\n<p>Here, we have used the\u00a0<a href=\"https:\/\/www.tecmint.com\/13-basic-cat-command-examples-in-linux\/\" target=\"_blank\" rel=\"noopener\">cat utility<\/a>\u00a0to view the contents of a file named\u00a0<strong>tecmint_deals.txt<\/strong>\u00a0and we want to view the deals of type\u00a0<strong>Tech<\/strong>\u00a0only, so we shall run the following commands:<\/p>\n<pre># cat tecmint_deals.txt\r\n# cat tecmint_deals.txt | awk '$4 ~ \/tech\/{print}'\r\n# cat tecmint_deals.txt | awk '$4 ~ \/Tech\/{print}'\r\n<\/pre>\n<div id=\"attachment_20899\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/06\/Use-Comparison-Operator-to-Match-String.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-20899\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/06\/Use-Comparison-Operator-to-Match-String.png\" sizes=\"auto, (max-width: 801px) 100vw, 801px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/06\/Use-Comparison-Operator-to-Match-String.png 801w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/06\/Use-Comparison-Operator-to-Match-String-768x332.png 768w\" alt=\"Use Awk Comparison Operator to Match String\" width=\"801\" height=\"346\" aria-describedby=\"caption-attachment-20899\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-20899\" class=\"wp-caption-text\">Use Awk Comparison Operator to Match String<\/p>\n<\/div>\n<p>In the example above, we have used the value\u00a0<code>~ \/pattern\/<\/code>\u00a0comparison operator, but there are two commands to try and bring out something very important.<\/p>\n<p>When you run the command with pattern\u00a0<strong>tech<\/strong>\u00a0nothing is printed out because there is no\u00a0<strong>deal<\/strong>\u00a0of that type, but with\u00a0<strong>Tech<\/strong>, you get deals of type\u00a0<strong>Tech<\/strong>.<\/p>\n<p>So always be careful when using this comparison operator, it is case sensitive as we have seen above.<\/p>\n<p>You can always use the output of another command instead as input for awk instead of reading input from a file, this is very simple as we have looked at in the examples above.<\/p>\n<p>Hope the examples were clear enough for you to understand, if you have any concerns, you can express them through the comment section below and remember to check the next part of the series where we shall look at\u00a0<strong>awk<\/strong>\u00a0features such as\u00a0<strong>variables<\/strong>,\u00a0<strong>numeric expressions<\/strong>\u00a0and\u00a0<strong>assignment operators<\/strong>.<\/p>\n<h1 class=\"post-title\">Learn How to Use Awk Variables, Numeric Expressions and Assignment Operators \u2013 Part 8<\/h1>\n<p>The\u00a0<a href=\"https:\/\/www.tecmint.com\/category\/awk-command\/\" target=\"_blank\" rel=\"noopener\">Awk command series<\/a>\u00a0is getting exciting I believe, in the previous seven parts, we walked through some fundamentals of\u00a0<strong>Awk<\/strong>\u00a0that you need to master to enable you perform some basic text or string filtering in Linux.<\/p>\n<p>Starting with this part, we shall dive into advance areas of\u00a0<strong>Awk<\/strong>\u00a0to handle more complex text or string filtering operations. Therefore, we are going to cover\u00a0<strong>Awk<\/strong>\u00a0features such as variables, numeric expressions and assignment operators.<\/p>\n<div id=\"attachment_21539\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/Learn-Awk-Variables-Numeric-Expressions-Assignment-Operators.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-21539\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/Learn-Awk-Variables-Numeric-Expressions-Assignment-Operators.png\" alt=\"Learn Awk Variables, Numeric Expressions and Assignment Operators\" width=\"720\" height=\"345\" aria-describedby=\"caption-attachment-21539\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-21539\" class=\"wp-caption-text\">Learn Awk Variables, Numeric Expressions and Assignment Operators<\/p>\n<\/div>\n<p>These concepts are not comprehensively distinct from the ones you may have probably encountered in many programming languages before such shell, C, Python plus many others, so there is no need to worry much about this topic, we are simply revising the common ideas of using these mentioned features.<\/p>\n<p>This will probably be one of the easiest\u00a0<strong>Awk<\/strong>\u00a0command sections to understand, so sit back and lets get going.<\/p>\n<h3>1. Awk Variables<\/h3>\n<p>In any programming language, a variable is a place holder which stores a value, when you create a variable in a program file, as the file is executed, some space is created in memory that will store the value you specify for the variable.<\/p>\n<p>You can define\u00a0<strong>Awk<\/strong>\u00a0variables in the same way you define shell variables as follows:<\/p>\n<pre>variable_name=value \r\n<\/pre>\n<p>In the syntax above:<\/p>\n<ol>\n<li><code>variable_name<\/code>: is the name you give a variable<\/li>\n<li><code>value<\/code>: the value stored in the variable<\/li>\n<\/ol>\n<p>Let\u2019s look at some examples below:<\/p>\n<pre>computer_name=\u201dtecmint.com\u201d\r\nport_no=\u201d22\u201d\r\nemail=\u201dadmin@tecmint.com\u201d\r\nserver=\u201dcomputer_name\u201d\r\n<\/pre>\n<p>Take a look at the simple examples above, in the first variable definition, the value\u00a0<code>tecmint.com<\/code>\u00a0is assigned to the variable\u00a0<code>computer_name<\/code>.<\/p>\n<p>Furthermore, the value\u00a0<code>22<\/code>\u00a0is assigned to the variable\u00a0<code>port_no<\/code>, it is also possible to assign the value of one variable to another variable as in the last example where we assigned the value of\u00a0<code>computer_name<\/code>\u00a0to the variable server.<\/p>\n<p>If you can recall, right from\u00a0<a href=\"https:\/\/www.tecmint.com\/awk-print-fields-columns-with-space-separator\/\" target=\"_blank\" rel=\"noopener\">part 2 of this Awk series<\/a>\u00a0were we covered field editing, we talked about how Awk divides input lines into fields and uses standard field access operator,\u00a0<code>$<\/code>\u00a0to read the different fields that have been parsed. We can also use variables to store the values of fields as follows.<\/p>\n<pre>first_name=$2\r\nsecond_name=$3\r\n<\/pre>\n<p>In the examples above, the value of\u00a0<code>first_name<\/code>\u00a0is set to second field and\u00a0<code>second_name<\/code>\u00a0is set to the third field.<\/p>\n<p>As an illustration, consider a file named\u00a0<code>names.txt<\/code>\u00a0which contains a list of an application\u2019s users indicating their first and last names plus gender. Using the\u00a0<a href=\"https:\/\/www.tecmint.com\/13-basic-cat-command-examples-in-linux\/\" target=\"_blank\" rel=\"noopener\">cat command<\/a>, we can view the contents of the file as follows:<\/p>\n<pre>$ cat names.txt\r\n<\/pre>\n<div id=\"attachment_21529\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/List-File-Content-Using-cat-Command.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-21529\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/List-File-Content-Using-cat-Command.png\" alt=\"List File Content Using cat Command\" width=\"455\" height=\"125\" aria-describedby=\"caption-attachment-21529\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-21529\" class=\"wp-caption-text\">List File Content Using cat Command<\/p>\n<\/div>\n<p>Then, we can also use the variables\u00a0<code>first_name<\/code>\u00a0and\u00a0<code>second_name<\/code>\u00a0to store the first and second names of the first user on the list as by running the\u00a0<strong>Awk<\/strong>\u00a0command below:<\/p>\n<pre>$ awk '\/Aaron\/{ first_name=$2 ; second_name=$3 ; print first_name, second_name ; }' names.txt\r\n<\/pre>\n<div id=\"attachment_21530\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/Store-Variables-Using-Awk-Command.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-21530\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/Store-Variables-Using-Awk-Command.png\" sizes=\"auto, (max-width: 922px) 100vw, 922px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/Store-Variables-Using-Awk-Command.png 922w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/Store-Variables-Using-Awk-Command-768x126.png 768w\" alt=\"Store Variables Using Awk Command\" width=\"922\" height=\"151\" aria-describedby=\"caption-attachment-21530\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-21530\" class=\"wp-caption-text\">Store Variables Using Awk Command<\/p>\n<\/div>\n<p>Let us also take a look at another case, when you issue the command\u00a0<code>uname -a<\/code>\u00a0on your terminal, it prints out all your system information.<\/p>\n<p>The second field contains your\u00a0<code>hostname<\/code>, therefore we can store the\u00a0<strong>hostname<\/strong>\u00a0in a variable called\u00a0<code>hostname<\/code>and print it using\u00a0<strong>Awk<\/strong>\u00a0as follows:<\/p>\n<pre>$ uname -a\r\n$ uname -a | awk '{hostname=$2 ; print hostname ; }' \r\n<\/pre>\n<div id=\"attachment_21532\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/Store-Command-Output-to-Variable-Using-Awk.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-21532\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/Store-Command-Output-to-Variable-Using-Awk.png\" sizes=\"auto, (max-width: 852px) 100vw, 852px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/Store-Command-Output-to-Variable-Using-Awk.png 852w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/Store-Command-Output-to-Variable-Using-Awk-768x111.png 768w\" alt=\"Store Command Output to Variable Using Awk\" width=\"852\" height=\"123\" aria-describedby=\"caption-attachment-21532\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-21532\" class=\"wp-caption-text\">Store Command Output to Variable Using Awk<\/p>\n<\/div>\n<h3>2. Numeric Expressions<\/h3>\n<p>In\u00a0<strong>Awk<\/strong>, numeric expressions are built using the following numeric operators:<\/p>\n<ol>\n<li><code>*<\/code>\u00a0: multiplication operator<\/li>\n<li><code>+<\/code>\u00a0: addition operator<\/li>\n<li><code>\/<\/code>\u00a0: division operator<\/li>\n<li><code>-<\/code>\u00a0: subtraction operator<\/li>\n<li><code>%<\/code>\u00a0: modulus operator<\/li>\n<li><code>^<\/code>\u00a0: exponentiation operator<\/li>\n<\/ol>\n<p>The syntax for a numeric expressions is:<\/p>\n<pre>$ operand1 operator operand2\r\n<\/pre>\n<p>In the form above,\u00a0<code>operand1<\/code>\u00a0and\u00a0<code>operand2<\/code>\u00a0can be numbers or variable names, and\u00a0<code>operator<\/code>\u00a0is any of the operators above.<\/p>\n<p>Below are some examples to demonstrate how to build numeric expressions:<\/p>\n<pre>counter=0\r\nnum1=5\r\nnum2=10\r\nnum3=num2-num1\r\ncounter=counter+1\r\n<\/pre>\n<p>To understand the use of numeric expressions in\u00a0<strong>Awk<\/strong>, we shall consider the following example below, with the file\u00a0<code>domains.txt<\/code>\u00a0which contains all domains owned by\u00a0<strong>Tecmint<\/strong>.<\/p>\n<pre>news.tecmint.com\r\ntecmint.com\r\nlinuxsay.com\r\nwindows.tecmint.com\r\ntecmint.com\r\nnews.tecmint.com\r\ntecmint.com\r\nlinuxsay.com\r\ntecmint.com\r\nnews.tecmint.com\r\ntecmint.com\r\nlinuxsay.com\r\nwindows.tecmint.com\r\ntecmint.com\r\n<\/pre>\n<p>To view the contents of the file, use the command below:<\/p>\n<pre>$ cat domains.txt\r\n<\/pre>\n<div id=\"attachment_21533\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/View-Contents-of-File.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-21533\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/View-Contents-of-File.png\" alt=\"View Contents of File\" width=\"700\" height=\"465\" aria-describedby=\"caption-attachment-21533\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-21533\" class=\"wp-caption-text\">View Contents of File<\/p>\n<\/div>\n<p>If we want to count the number of times the domain\u00a0<code>tecmint.com<\/code>\u00a0appears in the file, we can write a simple script to do that as follows:<\/p>\n<pre>#!\/bin\/bash\r\nfor file in $@; do\r\n        if [ -f $file ] ; then\r\n                #print out filename\r\n                echo \"File is: $file\"\r\n                #print a number incrementally for every line containing tecmint.com \r\n                awk  '\/^tecmint.com\/ { counter=counter+1 ; printf \"%s\\n\", counter ; }'   $file\r\n        else\r\n                #print error info incase input is not a file\r\n                echo \"$file is not a file, please specify a file.\" &gt;&amp;2 &amp;&amp; exit 1\r\n        fi\r\ndone\r\n#terminate script with exit code 0 in case of successful execution \r\nexit 0\r\n<\/pre>\n<div id=\"attachment_21534\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/Shell-Script-to-Count-a-String-in-File.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-21534\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/Shell-Script-to-Count-a-String-in-File.png\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/Shell-Script-to-Count-a-String-in-File.png 850w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/Shell-Script-to-Count-a-String-in-File-768x260.png 768w\" alt=\"Shell Script to Count a String or Text in File\" width=\"850\" height=\"288\" aria-describedby=\"caption-attachment-21534\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-21534\" class=\"wp-caption-text\">Shell Script to Count a String or Text in File<\/p>\n<\/div>\n<p>After creating the script, save it and make it executable, when we run it with the file,\u00a0<code>domains.txt<\/code>\u00a0as out input, we get the following output:<\/p>\n<pre>$ .\/script.sh  ~\/domains.txt\r\n<\/pre>\n<div id=\"attachment_21535\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/Script-To-Count-String.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-21535\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/Script-To-Count-String.png\" sizes=\"auto, (max-width: 802px) 100vw, 802px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/Script-To-Count-String.png 802w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/Script-To-Count-String-768x134.png 768w\" alt=\"Script to Count String or Text\" width=\"802\" height=\"140\" aria-describedby=\"caption-attachment-21535\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-21535\" class=\"wp-caption-text\">Script to Count String or Text<\/p>\n<\/div>\n<p>From the output of the script, there are 6 lines in the file\u00a0<code>domains.txt<\/code>\u00a0which contain\u00a0<code>tecmint.com<\/code>, to confirm that you can manually count them.<\/p>\n<h3>3. Assignment Operators<\/h3>\n<p>The last\u00a0<strong>Awk<\/strong>\u00a0feature we shall cover is assignment operators, there are several assignment operators in Awk and these include the following:<\/p>\n<ol>\n<li><code>*=<\/code>\u00a0: multiplication assignment operator<\/li>\n<li><code>+=<\/code>\u00a0: addition assignment operator<\/li>\n<li><code>\/=<\/code>\u00a0: division assignment operator<\/li>\n<li><code>-=<\/code>\u00a0: subtraction assignment operator<\/li>\n<li><code>%=<\/code>\u00a0: modulus assignment operator<\/li>\n<li><code>^=<\/code>\u00a0: exponentiation assignment operator<\/li>\n<\/ol>\n<p>The simplest syntax of an assignment operation in\u00a0<strong>Awk<\/strong>\u00a0is as follows:<\/p>\n<pre>$ variable_name=variable_name operator operand\r\n<\/pre>\n<p>Examples:<\/p>\n<pre>counter=0\r\ncounter=counter+1\r\n\r\nnum=20\r\nnum=num-1\r\n<\/pre>\n<p>You can use the assignment operators above to shorten assignment operations in\u00a0<strong>Awk<\/strong>, consider the previous examples, we could perform the assignment in the following form:<\/p>\n<pre>variable_name operator=operand\r\n<\/pre>\n<pre>counter=0\r\ncounter+=1\r\n\r\nnum=20\r\nnum-=1\r\n<\/pre>\n<p>Therefore, we can alter the\u00a0<strong>Awk<\/strong>\u00a0command in the shell script we just wrote above using\u00a0<code>+=<\/code>\u00a0assignment operator as follows:<\/p>\n<pre>#!\/bin\/bash\r\nfor file in $@; do\r\n        if [ -f $file ] ; then\r\n                #print out filename\r\n                echo \"File is: $file\"\r\n                #print a number incrementally for every line containing tecmint.com \r\n                awk  '\/^tecmint.com\/ { counter+=1 ; printf  \"%s\\n\",  counter ; }'   $file\r\n        else\r\n                #print error info incase input is not a file\r\n                echo \"$file is not a file, please specify a file.\" &gt;&amp;2 &amp;&amp; exit 1\r\n        fi\r\ndone\r\n#terminate script with exit code 0 in case of successful execution \r\nexit 0\r\n<\/pre>\n<div id=\"attachment_21536\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/Alter-Shell-Script.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-21536\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/Alter-Shell-Script.png\" alt=\"Alter Shell Script\" width=\"742\" height=\"285\" aria-describedby=\"caption-attachment-21536\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-21536\" class=\"wp-caption-text\">Alter Shell Script<\/p>\n<\/div>\n<p>In this segment of the\u00a0<a href=\"https:\/\/www.tecmint.com\/category\/awk-command\/\" target=\"_blank\" rel=\"noopener\">Awk series<\/a>, we covered some powerful\u00a0<strong>Awk<\/strong>\u00a0features, that is variables, building numeric expressions and using assignment operators, plus some few illustrations of how we can actually use them.<\/p>\n<p>These concepts are not any different from the one in other programming languages but there may be some significant distinctions under Awk programming.<\/p>\n<p>In\u00a0<strong>part 9<\/strong>, we shall look at more\u00a0<strong>Awk<\/strong>\u00a0features that is special patterns:\u00a0<code>BEGIN<\/code>\u00a0and\u00a0<code>END<\/code>.<\/p>\n<h1 class=\"post-title\">Learn How to Use Awk Special Patterns \u2018BEGIN and END\u2019 \u2013 Part 9<\/h1>\n<p>In\u00a0<strong>Part 8<\/strong>\u00a0of\u00a0<a href=\"https:\/\/www.tecmint.com\/category\/awk-command\/\" target=\"_blank\" rel=\"noopener\">this Awk series<\/a>, we introduced some powerful Awk command features, that is\u00a0<a href=\"https:\/\/www.tecmint.com\/learn-awk-variables-numeric-expressions-and-assignment-operators\/\" target=\"_blank\" rel=\"noopener\">variables, numeric expressions and assignment operators<\/a>.<\/p>\n<p>As we advance, in this segment, we shall cover more\u00a0<strong>Awk<\/strong>\u00a0features, and that is the special patterns:\u00a0<code>BEGIN<\/code>\u00a0and\u00a0<code>END<\/code>.<\/p>\n<div id=\"attachment_21546\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/Learn-Awk-Patterns-BEGIN-and-END.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-21546\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/Learn-Awk-Patterns-BEGIN-and-END.png\" alt=\"Learn Awk Patterns BEGIN and END\" width=\"720\" height=\"345\" aria-describedby=\"caption-attachment-21546\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-21546\" class=\"wp-caption-text\">Learn Awk Patterns BEGIN and END<\/p>\n<\/div>\n<p>These special features will prove helpful as we try to expand on and explore more methods of building complex\u00a0<strong>Awk<\/strong>\u00a0operations.<\/p>\n<p>To get started, let us drive our thoughts back to the introduction of the\u00a0<strong>Awk<\/strong>\u00a0series, remember when we started this series, I pointed out that the general syntax of a running an\u00a0<strong>Awk<\/strong>\u00a0command is:<\/p>\n<pre># awk 'script' filenames  \r\n<\/pre>\n<p>And in the syntax above, the\u00a0<strong>Awk<\/strong>\u00a0script has the form:<\/p>\n<pre>\/pattern\/ { actions } \r\n<\/pre>\n<p>When you consider the pattern in the script, it is normally a regular expression, additionally, you can also think of pattern as special patterns\u00a0<code>BEGIN<\/code>\u00a0and\u00a0<code>END<\/code>. Therefore, we can also write an\u00a0<strong>Awk<\/strong>\u00a0command in the form below:<\/p>\n<pre>awk '\r\n \tBEGIN { actions } \r\n \t\/pattern\/ { actions }\r\n \t\/pattern\/ { actions }\r\n            \u2026\u2026\u2026.\r\n\t END { actions } \r\n' filenames  \r\n<\/pre>\n<p>In the event that you use the special patterns:\u00a0<code>BEGIN<\/code>\u00a0and\u00a0<code>END<\/code>\u00a0in an\u00a0<strong>Awk<\/strong>\u00a0script, this is what each of them means:<\/p>\n<ol>\n<li><strong>BEGIN pattern<\/strong>: means that Awk will execute the action(s) specified in\u00a0<strong>BEGIN<\/strong>\u00a0once before any input lines are read.<\/li>\n<li><strong>END pattern<\/strong>: means that Awk will execute the action(s) specified in\u00a0<strong>END<\/strong>\u00a0before it actually exits.<\/li>\n<\/ol>\n<p>And the flow of execution of the an\u00a0<strong>Awk<\/strong>\u00a0command script which contains these special patterns is as follows:<\/p>\n<ol>\n<li>When the\u00a0<code>BEGIN<\/code>\u00a0pattern is used in a script, all the actions for\u00a0<strong>BEGIN<\/strong>\u00a0are executed once before any input line is read.<\/li>\n<li>Then an input line is read and parsed into the different fields.<\/li>\n<li>Next, each of the non-special patterns specified is compared with the input line for a match, when a match is found, the action(s) for that pattern are then executed. This stage will be repeated for all the patterns you have specified.<\/li>\n<li>Next, stage 2 and 3 are repeated for all input lines.<\/li>\n<li>When all input lines have been read and dealt with, in case you specify the\u00a0<code>END<\/code>\u00a0pattern, the action(s) will be executed.<\/li>\n<\/ol>\n<p>You should always remember this sequence of execution when working with the special patterns to achieve the best results in an\u00a0<strong>Awk<\/strong>\u00a0operation.<\/p>\n<p>To understand it all, let us illustrate using the example from\u00a0<a href=\"https:\/\/www.tecmint.com\/learn-awk-variables-numeric-expressions-and-assignment-operators\/\" target=\"_blank\" rel=\"noopener\">part 8<\/a>, about the list of domains owned by\u00a0<strong>Tecmint<\/strong>, as stored in a file named\u00a0<strong>domains.txt<\/strong>.<\/p>\n<pre>news.tecmint.com\r\ntecmint.com\r\nlinuxsay.com\r\nwindows.tecmint.com\r\ntecmint.com\r\nnews.tecmint.com\r\ntecmint.com\r\nlinuxsay.com\r\ntecmint.com\r\nnews.tecmint.com\r\ntecmint.com\r\nlinuxsay.com\r\nwindows.tecmint.com\r\ntecmint.com\r\n<\/pre>\n<pre>$ cat ~\/domains.txt\r\n<\/pre>\n<div id=\"attachment_21533\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/View-Contents-of-File.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-21533\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/View-Contents-of-File.png\" alt=\"View Contents of File\" width=\"700\" height=\"465\" aria-describedby=\"caption-attachment-21533\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-21533\" class=\"wp-caption-text\">View Contents of File<\/p>\n<\/div>\n<p>In this example, we want to count the number of times the domain\u00a0<code>tecmint.com<\/code>\u00a0is listed in the file\u00a0<strong>domains.txt<\/strong>. So we wrote a small shell script to help us do that using the idea of variables, numeric expressions and assignment operators which has the following content:<\/p>\n<pre>#!\/bin\/bash\r\nfor file in $@; do\r\n        if [ -f $file ] ; then\r\n                #print out filename\r\n                echo \"File is: $file\"\r\n                #print a number incrementally for every line containing tecmint.com \r\n                awk '\/^tecmint.com\/ { counter+=1 ; printf \"%s\\n\", counter ; }' $file\r\n        else\r\n                #print error info incase input is not a file\r\n                echo \"$file is not a file, please specify a file.\" &gt;&amp;2 &amp;&amp; exit 1\r\n        fi\r\ndone\r\n#terminate script with exit code 0 in case of successful execution \r\nexit 0\r\n<\/pre>\n<p>Let us now employ the two special patterns:\u00a0<code>BEGIN<\/code>\u00a0and\u00a0<code>END<\/code>\u00a0in the\u00a0<strong>Awk<\/strong>\u00a0command in the script above as follows:<\/p>\n<p>We shall alter the script:<\/p>\n<pre>awk '\/^tecmint.com\/ { counter+=1 ; printf \"%s\\n\", counter ; }' $file\r\n<\/pre>\n<p>To:<\/p>\n<pre>awk ' <strong>BEGIN<\/strong> {  print \"The number of times tecmint.com appears in the file is:\" ; }\r\n                      \/^tecmint.com\/ {  counter+=1  ;  }\r\n                      <strong>END<\/strong> {  printf \"%s\\n\",  counter  ; } \r\n                    '  $file\r\n<\/pre>\n<p>After making the changes to the\u00a0<strong>Awk<\/strong>\u00a0command, the complete shell script now looks like this:<\/p>\n<pre>#!\/bin\/bash\r\nfor file in $@; do\r\n        if [ -f $file ] ; then\r\n                #print out filename\r\n                echo \"File is: $file\"\r\n                #print the total number of times tecmint.com appears in the file\r\n                awk ' <strong>BEGIN<\/strong> {  print \"The number of times tecmint.com appears in the file is:\" ; }\r\n                      \/^tecmint.com\/ {  counter+=1  ;  }\r\n                      <strong>END<\/strong> {  printf \"%s\\n\",  counter  ; } \r\n                    '  $file\r\n        else\r\n                #print error info incase input is not a file\r\n                echo \"$file is not a file, please specify a file.\" &gt;&amp;2 &amp;&amp; exit 1\r\n        fi\r\ndone\r\n#terminate script with exit code 0 in case of successful execution \r\nexit 0\r\n<\/pre>\n<div id=\"attachment_21542\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/Awk-BEGIN-and-END-Patterns.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-21542\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/Awk-BEGIN-and-END-Patterns.png\" sizes=\"auto, (max-width: 794px) 100vw, 794px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/Awk-BEGIN-and-END-Patterns.png 794w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/Awk-BEGIN-and-END-Patterns-768x372.png 768w\" alt=\"Awk BEGIN and END Patterns\" width=\"794\" height=\"385\" aria-describedby=\"caption-attachment-21542\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-21542\" class=\"wp-caption-text\">Awk BEGIN and END Patterns<\/p>\n<\/div>\n<p>When we run the script above, it will first of all print the location of the file\u00a0<strong>domains.txt<\/strong>, then the\u00a0<strong>Awk<\/strong>\u00a0command script is executed, where the\u00a0<code>BEGIN<\/code>\u00a0special pattern helps us print out the message \u201c<code>The number of times tecmint.com appears in the file is:<\/code>\u201d before any input lines are read from the file.<\/p>\n<p>Then our pattern,\u00a0<code>\/^tecmint.com\/<\/code>\u00a0is compared against every input line and the action,\u00a0<code>{ counter+=1 ; }<\/code>is executed for each input line, which counts the number of times\u00a0<code>tecmint.com<\/code>\u00a0appears in the file.<\/p>\n<p>Finally, the\u00a0<code>END<\/code>\u00a0pattern will print the total the number of times the domain\u00a0<code>tecmint.com<\/code>\u00a0appears in the file.<\/p>\n<pre>$ .\/script.sh ~\/domains.txt \r\n<\/pre>\n<div id=\"attachment_21543\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/Script-to-Count-Number-of-Times-String-Appears.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-21543\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/Script-to-Count-Number-of-Times-String-Appears.png\" alt=\"Script to Count Number of Times String Appears\" width=\"662\" height=\"79\" aria-describedby=\"caption-attachment-21543\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-21543\" class=\"wp-caption-text\">Script to Count Number of Times String Appears<\/p>\n<\/div>\n<p>To conclude, we walked through more\u00a0<strong>Awk<\/strong>\u00a0features exploring on the concepts of special pattern:\u00a0<code>BEGIN<\/code>\u00a0and\u00a0<code>END<\/code>.<\/p>\n<p>As I pointed out before, these Awk features will help us build more complex\u00a0<a href=\"https:\/\/www.tecmint.com\/use-linux-awk-command-to-filter-text-string-in-files\/\" target=\"_blank\" rel=\"noopener\">text filtering operations<\/a>, there is more to cover under\u00a0<strong>Awk<\/strong>\u00a0features and in\u00a0<strong>part 10<\/strong>, we shall approach the idea of Awk built-in variables, so stay connected.<\/p>\n<h1 class=\"post-title\">Learn How to Use Awk Built-in Variables \u2013 Part 10<\/h1>\n<p>As we uncover the section of\u00a0<strong>Awk<\/strong>\u00a0features, in this part of the series, we shall walk through the concept of built-in variables in Awk. There are two types of variables you can use in Awk, these are;\u00a0<strong>user-defined<\/strong>\u00a0variables, which we covered in\u00a0<a href=\"https:\/\/www.tecmint.com\/learn-awk-variables-numeric-expressions-and-assignment-operators\/\" target=\"_blank\" rel=\"noopener\">Part 8<\/a>\u00a0and\u00a0<strong>built-in<\/strong>\u00a0variables.<\/p>\n<div id=\"attachment_21559\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/Awk-Built-in-Variables-Examples.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-21559\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/Awk-Built-in-Variables-Examples.png\" alt=\"Awk Built in Variables Examples\" width=\"720\" height=\"345\" aria-describedby=\"caption-attachment-21559\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-21559\" class=\"wp-caption-text\">Awk Built in Variables Examples<\/p>\n<\/div>\n<p><strong>Built-in<\/strong>\u00a0variables have values already defined in\u00a0<strong>Awk<\/strong>, but we can also carefully alter those values, the built-in variables include:<\/p>\n<ol>\n<li><code>FILENAME<\/code>\u00a0: current input file name( do not change variable name)<\/li>\n<li><code>FR<\/code>\u00a0: number of the current input line (that is input line 1, 2, 3\u2026 so on, do not change variable name)<\/li>\n<li><code>NF<\/code>\u00a0: number of fields in current input line (do not change variable name)<\/li>\n<li><code>OFS<\/code>\u00a0: output field separator<\/li>\n<li><code>FS<\/code>\u00a0: input field separator<\/li>\n<li><code>ORS<\/code>\u00a0: output record separator<\/li>\n<li><code>RS<\/code>\u00a0: input record separator<\/li>\n<\/ol>\n<p>Let us proceed to illustrate the use of some of the Awk built-in variables above:<\/p>\n<p>To read the filename of the current input file, you can use the\u00a0<code>FILENAME<\/code>\u00a0built-in variable as follows:<\/p>\n<pre>$ awk ' { print FILENAME } ' ~\/domains.txt \r\n<\/pre>\n<div id=\"attachment_21549\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/Awk-FILENAME-Variable.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-21549\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/Awk-FILENAME-Variable.png\" alt=\"Awk FILENAME Variable\" width=\"689\" height=\"276\" aria-describedby=\"caption-attachment-21549\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-21549\" class=\"wp-caption-text\">Awk FILENAME Variable<\/p>\n<\/div>\n<p>You will realize that, the filename is printed out for each input line, that is the default behavior of\u00a0<strong>Awk<\/strong>\u00a0when you use\u00a0<code>FILENAME<\/code>\u00a0built-in variable.<\/p>\n<p>Using\u00a0<code>NR<\/code>\u00a0to count the number of lines (records) in an input file, remember that, it also counts the empty lines, as we shall see in the example below.<\/p>\n<p>When we view the file\u00a0<strong>domains.txt<\/strong>\u00a0using\u00a0<a href=\"https:\/\/www.tecmint.com\/13-basic-cat-command-examples-in-linux\/\" target=\"_blank\" rel=\"noopener\">cat command<\/a>, it contains\u00a0<strong>14<\/strong>\u00a0lines with text and empty\u00a0<strong>2<\/strong>\u00a0lines:<\/p>\n<pre>$ cat ~\/domains.txt\r\n<\/pre>\n<div id=\"attachment_21553\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/Print-Contents-of-File.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-21553\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/Print-Contents-of-File.png\" alt=\"Print Contents of File\" width=\"676\" height=\"275\" aria-describedby=\"caption-attachment-21553\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-21553\" class=\"wp-caption-text\">Print Contents of File<\/p>\n<\/div>\n<pre>$ awk ' END { print \"Number of records in file is: \", NR } ' ~\/domains.txt \r\n<\/pre>\n<div id=\"attachment_21551\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/Awk-Count-Number-of-Lines.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-21551\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/Awk-Count-Number-of-Lines.png\" alt=\"Awk Count Number of Lines\" width=\"706\" height=\"65\" aria-describedby=\"caption-attachment-21551\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-21551\" class=\"wp-caption-text\">Awk Count Number of Lines<\/p>\n<\/div>\n<p>To count the number of fields in a record or line, we use the NR built-in variable as follows:<\/p>\n<pre>$ cat ~\/names.txt\r\n<\/pre>\n<div id=\"attachment_21552\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/List-File-Contents.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-21552\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/List-File-Contents.png\" alt=\"List File Contents\" width=\"678\" height=\"155\" aria-describedby=\"caption-attachment-21552\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-21552\" class=\"wp-caption-text\">List File Contents<\/p>\n<\/div>\n<pre>$ awk '{ print \"Record:\",NR,\"has\",NF,\"fields\" ; }' ~\/names.txt\r\n<\/pre>\n<div id=\"attachment_21554\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/Awk-Count-Number-of-Fields-in-File.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-21554\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/Awk-Count-Number-of-Fields-in-File.png\" alt=\"Awk Count Number of Fields in File\" width=\"704\" height=\"169\" aria-describedby=\"caption-attachment-21554\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-21554\" class=\"wp-caption-text\">Awk Count Number of Fields in File<\/p>\n<\/div>\n<p>Next, you can also specify an input field separator using the\u00a0<code>FS<\/code>\u00a0built-in variable, it defines how\u00a0<strong>Awk<\/strong>\u00a0divides input lines into fields.<\/p>\n<p>The default value for\u00a0<code>FS<\/code>\u00a0is\u00a0<strong>space<\/strong>\u00a0and\u00a0<strong>tab<\/strong>, but we can change the value of\u00a0<code>FS<\/code>\u00a0to any character that will instruct Awk to divide input lines accordingly.<\/p>\n<p>There are two methods to do this:<\/p>\n<ol>\n<li>one method is to use the\u00a0<strong>FS<\/strong>\u00a0built-in variable<\/li>\n<li>and the second is to invoke the\u00a0<strong>-F<\/strong>\u00a0Awk option<\/li>\n<\/ol>\n<p>Consider the file\u00a0<strong>\/etc\/passwd<\/strong>\u00a0on a Linux system, the fields in this file are divided using the\u00a0<code>:<\/code>\u00a0character, so we can specify it as the new input field separator when we want to filter out certain fields as in the following examples:<\/p>\n<p>We can use the\u00a0<code>-F<\/code>\u00a0option as follows:<\/p>\n<pre>$ awk -F':' '{ print $1, $4 ;}' \/etc\/passwd\r\n<\/pre>\n<div id=\"attachment_21555\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/Awk-Filter-Fields-in-Password-File.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-21555\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/Awk-Filter-Fields-in-Password-File.png\" alt=\"Awk Filter Fields in Password File\" width=\"698\" height=\"359\" aria-describedby=\"caption-attachment-21555\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-21555\" class=\"wp-caption-text\">Awk Filter Fields in Password File<\/p>\n<\/div>\n<p>Optionally, we can also take advantage of the\u00a0<code>FS<\/code>\u00a0built-in variable as below:<\/p>\n<pre>$ awk ' BEGIN {  FS=\u201c:\u201d ; }  { print $1, $4  ; } ' \/etc\/passwd\r\n<\/pre>\n<div id=\"attachment_21556\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/Filter-Fields-in-File-Using-Awk.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-21556\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/Filter-Fields-in-File-Using-Awk.png\" alt=\"Filter Fields in File Using Awk\" width=\"702\" height=\"436\" aria-describedby=\"caption-attachment-21556\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-21556\" class=\"wp-caption-text\">Filter Fields in File Using Awk<\/p>\n<\/div>\n<p>To specify an output field separator, use the\u00a0<code>OFS<\/code>\u00a0built-in variable, it defines how the output fields will be separated using the character we use as in the example below:<\/p>\n<pre>$ awk -F':' ' BEGIN { OFS=\"==&gt;\" ;} { print $1, $4 ;}' \/etc\/passwd\r\n<\/pre>\n<div id=\"attachment_21557\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/Add-Separator-to-Field-in-File.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-21557\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/07\/Add-Separator-to-Field-in-File.png\" alt=\"Add Separator to Field in File\" width=\"701\" height=\"361\" aria-describedby=\"caption-attachment-21557\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-21557\" class=\"wp-caption-text\">Add Separator to Field in File<\/p>\n<\/div>\n<p>In this\u00a0<strong>Part 10<\/strong>, we have explored the idea of using Awk built-in variables which come with predefined values. But we can also change these values, though, it is not recommended to do so unless you know what you are doing, with adequate understanding.\u00a0After this, we shall progress to cover how we can use shell variables in Awk command operations.<\/p>\n<h1 class=\"post-title\">How to Allow Awk to Use Shell Variables \u2013 Part 11<\/h1>\n<p>When we write shell scripts, we normally include other smaller programs or commands such as\u00a0<strong>Awk<\/strong>\u00a0operations in our scripts. In the case of Awk, we have to find ways of passing some values from the shell to Awk operations.<\/p>\n<p>This can be done by using shell variables within Awk commands, and in this part of the series, we shall learn how to allow Awk to use shell variables that may contain values we want to pass to Awk commands.<\/p>\n<p>There possibly two ways you can enable Awk to use shell variables:<\/p>\n<h3>1. Using Shell Quoting<\/h3>\n<p>Let us take a look at an example to illustrate how you can actually use shell quoting to substitute the value of a shell variable in an Awk command. In this example, we want to search for a\u00a0<strong>username<\/strong>\u00a0in the file\u00a0<strong>\/etc\/passwd<\/strong>, filter and print the user\u2019s account information.<\/p>\n<p>Therefore, we can write a\u00a0<code>test.sh<\/code>\u00a0script with the following content:<\/p>\n<pre>#!\/bin\/bash\r\n\r\n#read user input\r\nread -p \"Please enter username:\" username\r\n\r\n#search for username in \/etc\/passwd file and print details on the screen\r\ncat \/etc\/passwd | awk \"\/$username\/ \"' { print $0 }'\r\n<\/pre>\n<p>Thereafter, save the file and exit.<\/p>\n<p>Interpretation of the Awk command in the\u00a0<code>test.sh<\/code>\u00a0script above:<\/p>\n<pre>cat \/etc\/passwd | awk \"\/$username\/ \"' { print $0 }'\r\n<\/pre>\n<p><code>\"\/$username\/ \"<\/code>\u00a0\u2013 shell quoting used to substitute value of shell variable\u00a0<strong>username<\/strong>\u00a0in Awk command. The value of\u00a0<strong>username<\/strong>\u00a0is the pattern to be searched in the file\u00a0<strong>\/etc\/passwd<\/strong>.<\/p>\n<p>Note that the double quote is outside the Awk script,\u00a0<code>\u2018{ print $0 }\u2019<\/code>.<\/p>\n<p>Then make the script executable and run it as follows:<\/p>\n<pre>$ chmod  +x  test.sh\r\n$ .\/text.sh \r\n<\/pre>\n<p>After running the script, you will be prompted to enter a\u00a0<strong>username<\/strong>, type a valid\u00a0<strong>username<\/strong>\u00a0and hit\u00a0<strong>Enter<\/strong>. You will view the user\u2019s account details from the\u00a0<strong>\/etc\/passwd<\/strong>\u00a0file as below:<\/p>\n<div id=\"attachment_22036\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/08\/Shell-Script-to-Find-Username-in-Passwd-File.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-22036\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/08\/Shell-Script-to-Find-Username-in-Passwd-File.png\" alt=\"Shell Script to Find Username in Password File\" width=\"749\" height=\"160\" aria-describedby=\"caption-attachment-22036\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-22036\" class=\"wp-caption-text\">Shell Script to Find Username in Password File<\/p>\n<\/div>\n<h3>2. Using Awk\u2019s Variable Assignment<\/h3>\n<p>This method is much simpler and better in comparison to method one above. Considering the example above, we can run a simple command to accomplish the job. Under this method, we use the\u00a0<code>-v<\/code>\u00a0option to assign a shell variable to a Awk variable.<\/p>\n<p>Firstly, create a shell variable,\u00a0<code>username<\/code>\u00a0and assign it the name that we want to search in the\u00a0<code>\/etc\/passswd<\/code>file:<\/p>\n<pre>username=\"aaronkilik\"\r\n<\/pre>\n<p>Then type the command below and hit Enter:<\/p>\n<pre># cat \/etc\/passwd | awk -v name=\"$username\" ' $0 ~ name {print $0}'\r\n<\/pre>\n<div id=\"attachment_22037\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/08\/Find-Username-in-Password-File-Using-Awk.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-22037\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/08\/Find-Username-in-Password-File-Using-Awk.png\" sizes=\"auto, (max-width: 934px) 100vw, 934px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/08\/Find-Username-in-Password-File-Using-Awk.png 934w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/08\/Find-Username-in-Password-File-Using-Awk-768x149.png 768w\" alt=\"Find Username in Password File Using Awk\" width=\"934\" height=\"181\" aria-describedby=\"caption-attachment-22037\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-22037\" class=\"wp-caption-text\">Find Username in Password File Using Awk<\/p>\n<\/div>\n<p>Explanation of the above command:<\/p>\n<ol>\n<li><code>-v<\/code>\u00a0\u2013 Awk option to declare a variable<\/li>\n<li><code>username<\/code>\u00a0\u2013 is the shell variable<\/li>\n<li><code>name<\/code>\u00a0\u2013 is the Awk variable<\/li>\n<\/ol>\n<p>Let us take a careful look at\u00a0<code>$0 ~ name<\/code>\u00a0inside the Awk script,\u00a0<code>' $0 ~ name {print $0}'<\/code>. Remember, when we covered\u00a0<a href=\"https:\/\/www.tecmint.com\/comparison-operators-in-awk\/\" target=\"_blank\" rel=\"noopener\">Awk comparison operators in Part 4<\/a>\u00a0of this series, one of the comparison operators was value\u00a0<code>~<\/code>pattern, which means: true if value matches the pattern.<\/p>\n<p>The\u00a0<code>output($0)<\/code>\u00a0of\u00a0<a href=\"https:\/\/www.tecmint.com\/13-basic-cat-command-examples-in-linux\/\" target=\"_blank\" rel=\"noopener\">cat command<\/a>\u00a0piped to Awk matches the pattern\u00a0<code>(aaronkilik)<\/code>\u00a0which is the name we are searching for in\u00a0<strong>\/etc\/passwd<\/strong>, as a result, the comparison operation is true. The line containing the user\u2019s account information is then printed on the screen.<\/p>\n<h3>Conclusion<\/h3>\n<p>We have covered an important section of Awk features, that can help us use shell variables within Awk commands. Many times, you will write small Awk programs or commands within shell scripts and therefore, you need to have a clear understanding of how to use shell variables within Awk commands.<\/p>\n<p>In the next part of the Awk series, we shall dive into yet another critical section of Awk features, that is flow control statements. So stay tunned and let\u2019s keep learning and sharing.<\/p>\n<h1 class=\"post-title\">How to Use Flow Control Statements in Awk \u2013 Part 12<\/h1>\n<p>When you review all the Awk examples we have covered so far, right from the start of the\u00a0<a href=\"https:\/\/www.tecmint.com\/category\/awk-command\/\" target=\"_blank\" rel=\"noopener\">Awk series<\/a>, you will notice that all the commands in the various examples are executed sequentially, that is one after the other. But in certain situations, we may want to run some\u00a0<a href=\"https:\/\/www.tecmint.com\/use-linux-awk-command-to-filter-text-string-in-files\/\" target=\"_blank\" rel=\"noopener\">text filtering operations<\/a>\u00a0based on some conditions, that is where the approach of flow control statements sets in.<\/p>\n<div id=\"attachment_22092\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/08\/Use-Flow-Control-Statements-in-Awk.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-22092\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/08\/Use-Flow-Control-Statements-in-Awk.png\" alt=\"Use Flow Control Statements in Awk\" width=\"720\" height=\"345\" aria-describedby=\"caption-attachment-22092\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-22092\" class=\"wp-caption-text\">Use Flow Control Statements in Awk<\/p>\n<\/div>\n<p>There are various flow control statements in\u00a0<a href=\"https:\/\/www.tecmint.com\/category\/awk-command\/\" target=\"_blank\" rel=\"noopener\">Awk programming<\/a>\u00a0and these include:<\/p>\n<ol>\n<li>if-else statement<\/li>\n<li>for statement<\/li>\n<li>while statement<\/li>\n<li>do-while statement<\/li>\n<li>break statement<\/li>\n<li>continue statement<\/li>\n<li>next statement<\/li>\n<li>nextfile statement<\/li>\n<li>exit statement<\/li>\n<\/ol>\n<p>However, for the scope of this series, we shall expound on:\u00a0<code>if-else<\/code>,\u00a0<code>for<\/code>,\u00a0<code>while<\/code>\u00a0and\u00a0<code>do while<\/code>\u00a0statements. Remember that we already walked through\u00a0<a href=\"https:\/\/www.tecmint.com\/use-next-command-with-awk-in-linux\/\" target=\"_blank\" rel=\"noopener\">how to use next statement in Part 6<\/a>\u00a0of this\u00a0<a href=\"https:\/\/www.tecmint.com\/category\/awk-command\/\" target=\"_blank\" rel=\"noopener\">Awk series<\/a>.<\/p>\n<h3>1. The if-else Statement<\/h3>\n<p>The expected syntax of the\u00a0<code>if statement<\/code>\u00a0is similar to that of the shell\u00a0<strong>if statement<\/strong>:<\/p>\n<pre>if  (condition1) {\r\n     actions1\r\n}\r\nelse {\r\n      actions2\r\n}\r\n<\/pre>\n<p>In the above syntax,\u00a0<code>condition1<\/code>\u00a0and\u00a0<code>condition2<\/code>\u00a0are Awk expressions, and\u00a0<code>actions1<\/code>\u00a0and\u00a0<code>actions2<\/code>\u00a0are Awk commands executed when the respective conditions are satisfied.<\/p>\n<p>When\u00a0<strong>condition1<\/strong>\u00a0is satisfied, meaning it\u2019s true, then\u00a0<strong>actions1<\/strong>\u00a0is executed and the\u00a0<strong>if statement<\/strong>\u00a0exits, otherwise\u00a0<strong>actions2<\/strong>\u00a0is executed.<\/p>\n<p>The\u00a0<strong>if statement<\/strong>\u00a0can also be expanded to a\u00a0<code>if-else_if-else<\/code>\u00a0statement as below:<\/p>\n<pre>if (condition1){\r\n     actions1\r\n}\r\nelse if (conditions2){\r\n      actions2\r\n}\r\nelse{\r\n     actions3\r\n}\r\n<\/pre>\n<p>For the form above, if\u00a0<strong>condition1<\/strong>\u00a0is true, then\u00a0<strong>actions1<\/strong>\u00a0is executed and the\u00a0<strong>if statement<\/strong>\u00a0exits, otherwise\u00a0<strong>condition2<\/strong>\u00a0is evaluated and if it is true, then\u00a0<strong>actions2<\/strong>\u00a0is executed and the\u00a0<strong>if statement<\/strong>\u00a0exits. However, when\u00a0<strong>condition2<\/strong>\u00a0is false then,\u00a0<strong>actions3<\/strong>\u00a0is executed and the\u00a0<strong>if statement<\/strong>\u00a0exits.<\/p>\n<p>Here is a case in point of using\u00a0<strong>if statements<\/strong>, we have a list of\u00a0<strong>users<\/strong>\u00a0and their\u00a0<strong>ages<\/strong>\u00a0stored in the file,\u00a0<strong>users.txt<\/strong>.<\/p>\n<p>We want to print a statement indicating a user\u2019s name and whether the user\u2019s age is less or more than\u00a0<strong>25<\/strong>\u00a0years old.<\/p>\n<pre><strong>aaronkilik@tecMint ~ $<\/strong> <strong>cat users.txt<\/strong>\r\nSarah L\t\t\t35    \tF\r\nAaron Kili\t\t40    \tM\r\nJohn  Doo\t\t20    \tM\r\nKili  Seth\t\t49    \tM    \r\n<\/pre>\n<p>We can write a short shell script to carry out our job above, here is the content of the script:<\/p>\n<pre>#!\/bin\/bash\r\nawk ' { \r\n        if ( $3 &lt;= 25 ){\r\n           print \"User\",$1,$2,\"is less than 25 years old.\" ;\r\n        }\r\n        else {\r\n           print \"User\",$1,$2,\"is more than 25 years old\" ; \r\n}\r\n}'    ~\/users.txt\r\n<\/pre>\n<p>Then save the file and exit, make the script executable and run it as follows:<\/p>\n<pre>$ chmod +x test.sh\r\n$ .\/test.sh\r\n<\/pre>\n<h5>Sample Output<\/h5>\n<pre>User Sarah L is more than 25 years old\r\nUser Aaron Kili is more than 25 years old\r\nUser John Doo is less than 25 years old.\r\nUser Kili Seth is more than 25 years old\r\n<\/pre>\n<h3>2. The for Statement<\/h3>\n<p>In case you want to execute some Awk commands in a loop, then the\u00a0<strong>for statement<\/strong>\u00a0offers you a suitable way to do that, with the syntax below:<\/p>\n<p>Here, the approach is simply defined by the use of a counter to control the loop execution, first you need to initialize the counter, then run it against a test condition, if it is true, execute the actions and finally increment the counter. The loop terminates when the counter does not satisfy the condition.<\/p>\n<pre>for ( counter-initialization; test-condition; counter-increment ){\r\n      actions\r\n}\r\n<\/pre>\n<p>The following Awk command shows how the\u00a0<strong>for statement<\/strong>\u00a0works, where we want to print the numbers\u00a0<strong>0-10<\/strong>:<\/p>\n<pre>$ awk 'BEGIN{ for(counter=0;counter&lt;=10;counter++){ print counter} }'\r\n<\/pre>\n<h5>Sample Output<\/h5>\n<pre>0\r\n1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n<\/pre>\n<h3>3. The while Statement<\/h3>\n<p>The conventional syntax of the\u00a0<strong>while statement<\/strong>\u00a0is as follows:<\/p>\n<pre>while ( condition ) {\r\n          actions\r\n}\r\n<\/pre>\n<p>The condition is an Awk expression and actions are lines of Awk commands executed when the condition is true.<\/p>\n<p>Below is a script to illustrate the use of\u00a0<strong>while statement<\/strong>\u00a0to print the numbers\u00a0<strong>0-10<\/strong>:<\/p>\n<pre>#!\/bin\/bash\r\nawk ' BEGIN{ counter=0 ;\r\n         \r\n        while(counter&lt;=10){\r\n              print counter;\r\n              counter+=1 ;\r\n             \r\n}\r\n}  \r\n<\/pre>\n<p>Save the file and make the script executable, then run it:<\/p>\n<pre>$ chmod +x test.sh\r\n$ .\/test.sh\r\n<\/pre>\n<h5>Sample Output<\/h5>\n<pre>0\r\n1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n<\/pre>\n<h3>4. The do while Statement<\/h3>\n<p>It is a modification of the\u00a0<strong>while statement<\/strong>\u00a0above, with the following underlying syntax:<\/p>\n<pre>do {\r\n     actions\r\n}\r\n while (condition) \r\n<\/pre>\n<p>The slight difference is that, under\u00a0<strong>do while<\/strong>, the Awk commands are executed before the condition is evaluated. Using the very example under\u00a0<strong>while statement<\/strong>\u00a0above, we can illustrate the use of\u00a0<strong>do while<\/strong>\u00a0by altering the Awk command in the\u00a0<strong>test.sh<\/strong>\u00a0script as follows:<\/p>\n<pre>#!\/bin\/bash\r\n\r\nawk ' BEGIN{ counter=0 ;  \r\n        do{\r\n            print counter;  \r\n            counter+=1 ;    \r\n        }\r\n        while (counter&lt;=10)   \r\n} \r\n'\r\n<\/pre>\n<p>After modifying the script, save the file and exit. Then make the script executable and execute it as follows:<\/p>\n<pre>$ chmod +x test.sh\r\n$ .\/test.sh\r\n<\/pre>\n<h5>Sample Output<\/h5>\n<pre>0\r\n1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n<\/pre>\n<h3>Conclusion<\/h3>\n<p>This is not a comprehensive guide regarding Awk flow control statements, as I had mentioned earlier on, there are several other\u00a0<strong>flow control statements<\/strong>\u00a0in Awk.<\/p>\n<p>Nonetheless, this part of the\u00a0Awk series\u00a0should give you a clear fundamental idea of how execution of Awk commands can be controlled based on certain conditions.<\/p>\n<p>You can as well expound more on the rest of the flow control statements to gain more understanding on the subject matter. Finally, in the next section of the Awk series, we shall move into writing Awk scripts.<\/p>\n<h1 class=\"post-title\">How to Write Scripts Using Awk Programming Language \u2013 Part 13<\/h1>\n<p>All along from the beginning of the\u00a0<a href=\"https:\/\/tecmint.com\/category\/awk-command\/\" target=\"_blank\" rel=\"noopener\">Awk series up to Part 12<\/a>, we have been writing small Awk commands and programs on the command line and in shell scripts respectively.<\/p>\n<p>However,\u00a0<strong>Awk<\/strong>, just as Shell, is also an interpreted language, therefore, with all that we have walked through from the start of this series, you can now write Awk executable scripts.<\/p>\n<p>Similar to how we write a shell script, Awk scripts start with the line:<\/p>\n<pre>#! \/path\/to\/awk\/utility -f \r\n<\/pre>\n<p>For example on my system, the\u00a0<strong>Awk<\/strong>\u00a0utility is located in\u00a0<strong>\/usr\/bin\/awk<\/strong>, therefore, I would start an Awk script as follows:<\/p>\n<pre>#! \/usr\/bin\/awk -f \r\n<\/pre>\n<p><center><ins class=\"adsbygoogle\" data-ad-client=\"ca-pub-2601749019656699\" data-ad-slot=\"5590002574\" data-ad-format=\"auto\" data-adsbygoogle-status=\"done\"><ins id=\"aswift_4_expand\"><ins id=\"aswift_4_anchor\"><iframe id=\"aswift_4\" name=\"aswift_4\" width=\"780\" height=\"96\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\" allowfullscreen=\"allowfullscreen\" data-mce-fragment=\"1\"><\/iframe><\/ins><\/ins><\/ins><\/center>Explaining the line above:<\/p>\n<ol>\n<li><code>#!<\/code>\u00a0\u2013 referred to as\u00a0<strong>Shebang<\/strong>, which specifies an interpreter for the instructions in a script<\/li>\n<li><code>\/usr\/bin\/awk<\/code>\u00a0\u2013 is the interpreter<\/li>\n<li><code>-f<\/code>\u00a0\u2013 interpreter option, used to read a program file<\/li>\n<\/ol>\n<p>That said, let us now dive into looking at some examples of Awk executable scripts, we can start with the simple script below. Use your favorite editor to open a new file as follows:<\/p>\n<pre>$ vi script.awk\r\n<\/pre>\n<p>And paste the code below in the file:<\/p>\n<pre>#!\/usr\/bin\/awk -f \r\nBEGIN { printf \"%s\\n\",\"Writing my first Awk executable script!\" }\r\n<\/pre>\n<p>Save the file and exit, then make the script executable by issuing the command below:<\/p>\n<pre>$ chmod +x script.awk\r\n<\/pre>\n<p>Thereafter, run it:<\/p>\n<pre>$ .\/script.awk\r\n<\/pre>\n<h5>Sample Output<\/h5>\n<pre>Writing my first Awk executable script!\r\n<\/pre>\n<p>A critical programmer out there must be asking, \u201c<strong>where are the comments?<\/strong>\u201d, yes, you can also include comments in your Awk script. Writing comments in your code is always a good programming practice.<\/p>\n<p>It helps other programmers looking through your code to understand what you are trying to achieve in each section of a script or program file.<\/p>\n<p>Therefore, you can include comments in the script above as follows.<\/p>\n<pre>#!\/usr\/bin\/awk -f \r\n\r\n#This is how to write a comment in Awk\r\n#using the BEGIN special pattern to print a sentence \r\n\r\nBEGIN { printf \"%s\\n\",\"Writing my first Awk executable script!\" }\r\n<\/pre>\n<p>Next, we shall look at an example where we read input from a file. We want to search for a system user named\u00a0<strong>aaronkilik<\/strong>\u00a0in the account file,\u00a0<strong>\/etc\/passwd<\/strong>, then print the username, user\u00a0<strong>ID<\/strong>\u00a0and user\u00a0<strong>GID<\/strong>\u00a0as follows:<\/p>\n<p>Below is the content of our script called\u00a0<strong>second.awk<\/strong>.<\/p>\n<pre>#! \/usr\/bin\/awk -f \r\n\r\n#use BEGIN sepecial character to set FS built-in variable\r\nBEGIN { FS=\":\" }\r\n\r\n#search for username: aaronkilik and print account details \r\n\/aaronkilik\/ { print \"Username :\",$1,\"User ID :\",$3,\"User GID :\",$4 }\r\n<\/pre>\n<p>Save the file and exit, make the script executable and execute it as below:<\/p>\n<pre>$ chmod +x second.awk\r\n$ .\/second.awk \/etc\/passwd\r\n<\/pre>\n<h5>Sample Output<\/h5>\n<pre>Username : aaronkilik User ID : 1000 User GID : 1000\r\n<\/pre>\n<p>In the last example below, we shall use\u00a0<strong>do while statement<\/strong>\u00a0to print out numbers from\u00a0<strong>0-10<\/strong>:<\/p>\n<p>Below is the content of our script called\u00a0<strong>do.awk<\/strong>.<\/p>\n<pre>#! \/usr\/bin\/awk -f \r\n\r\n#printing from 0-10 using a do while statement \r\n#do while statement \r\nBEGIN {\r\n#initialize a counter\r\nx=0\r\n\r\ndo {\r\n    print x;\r\n    x+=1;\r\n}\r\nwhile(x&lt;=10)\r\n}\r\n<\/pre>\n<p>After saving the file, make the script executable as we have done before. Afterwards, run it:<\/p>\n<pre>$ chmod +x do.awk\r\n$ .\/do.awk\r\n<\/pre>\n<h5>Sample Output<\/h5>\n<pre>0\r\n1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n<\/pre>\n<h3>Summary<\/h3>\n<p>We have come to the end of this\u00a0<a href=\"https:\/\/tecmint.com\/category\/awk-command\/\" target=\"_blank\" rel=\"noopener\">interesting Awk series<\/a>, I hope you have learned a lot from all the 13 parts, as an introduction to Awk programming language.<\/p>\n<p>As I mentioned from the beginning, Awk is a complete text processing language, for that reason, you can learn more other aspects of Awk programming language such as environmental variables, arrays, functions (built-in &amp; user defined) and beyond.<\/p>\n<p>There is yet additional parts of Awk programming to learn and master, so, below, I have provided some links to important online resources that you can use to expand your Awk programming skills, these are not necessarily all that you need, you can also look out for useful Awk programming books.<\/p>\n<p><strong>Reference Links<\/strong>:\u00a0<a href=\"https:\/\/www.gnu.org\/software\/gawk\/manual\/gawk.html\" target=\"_blank\" rel=\"noopener\">The GNU Awk User\u2019s Guide<\/a>\u00a0and\u00a0<a href=\"https:\/\/www.chemie.fu-berlin.de\/chemnet\/use\/info\/gawk\/gawk_toc.html\" target=\"_blank\" rel=\"noopener\">AWK Language Programming<\/a><\/p>\n<p>For any thoughts you wish to share or questions, use the comment form below.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/linux-awk-command-book-for-beginners\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ebook: Introducing the Awk Getting Started Guide for Beginners As a Linux system administrator, many times, you will get into situations where you need to manipulate and reformat the output from different commands, to simply display part of a output by filtering out a few lines. This process can be referred to as text filtering, &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/03\/awk\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;AWK (Alfred V. Aho &#8211; Peter J. Weinberger &#8211; Brian W. Kernighan)&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-13612","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13612","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=13612"}],"version-history":[{"count":3,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13612\/revisions"}],"predecessor-version":[{"id":13618,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13612\/revisions\/13618"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=13612"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=13612"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=13612"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}