

{"id":110396,"date":"2022-09-02T09:00:46","date_gmt":"2022-09-02T03:30:46","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=110396"},"modified":"2022-09-02T09:41:39","modified_gmt":"2022-09-02T04:11:39","slug":"awk-command-in-linux","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/awk-command-in-linux\/","title":{"rendered":"AWK Command in Linux"},"content":{"rendered":"<p>In this article, you will learn all there is to the AWK command in Linux-based operating systems. We will go through what the awk command is, why it is used, how it works, syntax, options, and the built-in variables of the awk command.<\/p>\n<p>We will also look into the technical side of the awk command as we go through awk patterns, awk statements, awk variables, awk actions, and finally some practical commands of the awk command in the terminal. In this article, we will look into some of the coolest features of the awk command, so pay attention and read right to the end!<\/p>\n<h3>What is AWK?<\/h3>\n<p>Awk is not a command-line-based utility, instead, it is a scripting language used for manipulating and generating reports. The awk language, unlike c programing, needs no compiling. AWK is the abbreviation of the names of the developers &#8211; Aho, Weinberger, and Kernighan.<\/p>\n<p>Awk is designed for advanced text processing and is mostly used as a tool for reporting and analysis. Since awk is data-driven, you can define a set of actions to be performed against the input text. Awk takes in and transforms the input data, and sends it to stdout (standard output).<\/p>\n<p>The awk scripting language is used very commonly by programmers, as they write scaled-down programs in the form of a statement that defines text designs and patterns.<\/p>\n<h3>How does awk work?<\/h3>\n<p>The primary purpose of the awk command is to make text manipulation and information retrieval an easy job in Linux distributions. The command works by scanning a set of input lines and then searches for the lines that match the pattern specified by the user.<\/p>\n<p>The awk command accepts input data, which gets transformed and sent out to the standard output. For each pattern that awk recognizes, the user can describe an action to perform on each line. Awk can easily process complex log files and output a readable output.<\/p>\n<h3>Why use awk?<\/h3>\n<p>As we have seen awk is used for processing and manipulating text. It enables a programmer to write tine program in the form of a statement where he or she can specify text patterns that are searched for in each line of a file.<\/p>\n<p>Awk also has the ability to search more than one file to see if they contain the lines that match the specified pattern and perform the defined actions.<\/p>\n<p>Let us look at what we can do with the awk command:<\/p>\n<h4>1. Operations capable of AWK<\/h4>\n<p>a. Scan files line by line.<\/p>\n<p>b. Divides each input line into fields.<\/p>\n<p>c. awk compares the fields of the input lines to patterns<\/p>\n<p>d. On successful matching, awk can perform specified actions.<\/p>\n<h4>2. What is awk useful for?<\/h4>\n<p>a. Transform data files<\/p>\n<p>b. Produce formatted reports<\/p>\n<p>c. processing text<\/p>\n<p>d. Manipulating text<\/p>\n<h4>3. Programming constructs<\/h4>\n<p>a. Producing formatted output lines<\/p>\n<p>b. Performing arithmetic and string operations<\/p>\n<p>c. loops and conditionals<\/p>\n<h3>Syntax of the awk command<\/h3>\n<p>When you first look at the syntax of the awk command, it may look a little intimidating, but once you understand what each field is for, and see a few examples, you will get the hang of it. The syntax for th awk command is:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">awk &lt;options&gt; &lt;\u2019selection criteria &lt;action&gt;\u2019 input filename&gt; output file<\/pre>\n<p>Let us look at each field in the syntax briefly:<\/p>\n<h4>1. Options<\/h4>\n<p>This field takes in the options available with the awk command, which we will look into in the next section.<\/p>\n<h4>2. Selection criteria<\/h4>\n<p>This field specifies the pattern of string we are searching for.<\/p>\n<h4>3. Action<\/h4>\n<p>This field specifies the action that has to be carried out if it found the match<\/p>\n<h4>4. Input filename<\/h4>\n<p>This field specifies the name of the file you want the search of the string to happen in.<\/p>\n<h4>5. Input filename<\/h4>\n<p>This field specifies the name of the files you want the formatted or manipulated print in.<\/p>\n<h3>Options used with the awk command<\/h3>\n<p>We have seen that in the syntax of the awk command, there is a field for entering options. Options specify how awk should work or how the output should be formatted. Let us look into the option available with the awk command.<\/p>\n<h4>1. -f &lt;program file&gt;<\/h4>\n<p>This option reads the awk program source from the file specified instead of the first command-line argument.<\/p>\n<h4>2. -F &lt;fs&gt;<\/h4>\n<p>This option uses fs for the input field separator.<\/p>\n<h3>Fields and records<\/h3>\n<p>In earlier sections, we were speaking about awk dividing input lines into fields. Let us look at what files and records are in slightly greater depth. We saw that awk processes text files and streams, and divides this input data into records and files.<\/p>\n<p>Awk operators only one record at a time. These records are separated by a character called separators and the default separator is a new line. This means that each line in the text file is a record.<\/p>\n<p>These records consist of fields. Just like records, fields are also separated by a separator, except in this case the default separator is a space. Fields in each record are referred using a dollar sign ($) Followed by the filed number starting with 1. In layman&#8217;s language, a record is a sentence and a field is a word.<\/p>\n<h3>Statements of the AWK command<\/h3>\n<p>Just like every programming language, awk also has the standard loops like if-else, while, for, and many more, in this section we will be looking at each of them in detail.<\/p>\n<h4>If -else statement<\/h4>\n<p>The if-else statement works just like you expect it, if a statement is true it will perform a specific set of instructions and if it is false, it will perform another set of instructions.<\/p>\n<p>Let us write a simple program in one line: to check if the second field and 3rd field of each record are the same, if they are we will print what they are and if not we will print a message saying they are not equal.<\/p>\n<p>To write the program as expected as above, use the command shown below:<\/p>\n<p>awk -F &#8216;,&#8217; &#8216;{if($2==$3){print $1&#8243;,&#8221;$2&#8243;,&#8221;$3} else {print &#8220;Not equal&#8221;}}&#8217; if .txt<\/p>\n<p>To execute this program, we obviously need a sample text file, for the time being, let us consider the following text file:<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/sample-text-file.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110528\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/sample-text-file.webp\" alt=\"sample text file\" width=\"358\" height=\"164\" \/><\/a><\/p>\n<p>And upon running the command above, we get the expected output.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/using-the-awk-command-on-the-sample-text-file-shown-above.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110529\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/using-the-awk-command-on-the-sample-text-file-shown-above.webp\" alt=\"linux awk command output\" width=\"780\" height=\"123\" \/><\/a><\/p>\n<h4>While loop<\/h4>\n<p>The while loop also works just like anticipated, it performs a specific task until the specified condition is false. Let us write a simple while loop to break down the field in every record, and also precede it with a number. To do so, use the command shown below:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">awk '{i=0; while(i&lt;=NF) { print i \":\"$i; i++;}}' if.txt<\/pre>\n<p>For this statement also let us take the text file we used in the previous example.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/sample-text-file.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110528\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/sample-text-file.webp\" alt=\"sample text file\" width=\"358\" height=\"164\" \/><\/a><\/p>\n<p>Upon running the while loop, we get the expected output:<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/while-loop-with-the-awk-command.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110530\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/while-loop-with-the-awk-command.webp\" alt=\"while loop with the awk command\" width=\"780\" height=\"512\" \/><\/a><\/p>\n<h4>For loop<\/h4>\n<p>The for loop is exactly the same as the while loop, it performs a specific set of actions only as long as the condition is true. Let us write a simple program for printing the squares of numbers till 10 using for loop. To do so, use the command shown below:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">awk 'BEGIN{for(i=1; i&lt;=10; i++) print \"The square of\", i, \"is\", i*i;}'<\/pre>\n<p>This specific example does not need any text file to work on as we are directly printing in on the terminal and have no input data. Upon running the command, we get the following output:<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/for-loop-with-the-awk-command.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110531\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/for-loop-with-the-awk-command.webp\" alt=\"for loop with the awk command\" width=\"780\" height=\"199\" \/><\/a><\/p>\n<h4>Break statement<\/h4>\n<p>The break statement is used to exit out of a loop with a specific condition met. For example, let us write a code to print the word \u201cubuntu\u201d only 5 times (using the break statement), which means the 6th time the command goes through the loop, it should break away from it. To do the following, use the command shown below:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">awk 'BEGIN{x=1; while(1) {print \"Example\"; if ( x==6 ) break; x++; }}'<\/pre>\n<p>Even this example does not need any text file to work on as we are directly printing in on the terminal and have no input data. Upon running the command, we get the following output:<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/break-statement-with-the-awk-command.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110532\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/break-statement-with-the-awk-command.webp\" alt=\"break statement with the awk command\" width=\"780\" height=\"138\" \/><\/a><\/p>\n<h3>Patterns of AWK<\/h3>\n<p>You can insert a pattern in front of the awk command so that it acts as a selector. The selector determines whether to perform the specified action or not. Let us look at the different types of patterns<\/p>\n<p>1. Regular expressions<\/p>\n<p>2. Arithmetic and relational expressions<\/p>\n<p>3. String valued expressions<\/p>\n<p>4. Arbitrary boolean combinations of the above expressions.<\/p>\n<p>Let us look at each expression in detail.<\/p>\n<h4>Regular expressions using linux AWK Command<\/h4>\n<p>These expressions are the simplest form of expressions containing a string of characters enclosed in slashes (\/). For example, let us write a command to print out the letters starting with the letter capital \u2018A\u2019 in a specific text file. To do so type the command shown below in the terminal:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">awk '$1 ~ \/^A\/ {print $0}' expression.txt<\/pre>\n<p>Upon running the command, we get the expected output: All the content starting with the letter \u201cA\u201d gets printed out.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/regular-expressions-of-awk.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110533\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/regular-expressions-of-awk.webp\" alt=\"regular expressions of awk\" width=\"854\" height=\"400\" \/><\/a><\/p>\n<h4>Relational expressions using AWK Linux<\/h4>\n<p>As the name suggests relational expressions relate 2 quantities, they include:<\/p>\n<ul>\n<li>&lt; &#8211; less than<\/li>\n<li>&lt;= &#8211; lessa than or equal to<\/li>\n<li>== equal to<\/li>\n<li>!= &#8211; not equal to<\/li>\n<li>= greater than or equal to<\/li>\n<li>&gt; &#8211; greater than<\/li>\n<\/ul>\n<p>Let us write a program to check if 2 numbers are equal or not, to do so, type the command shown below in the terminal:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">awk 'BEGIN { a = 10; b = 10; if (a == b) print \"a is equal ti b\" }'<\/pre>\n<p>Since the numbers are equal the command prints \u201ca is equal to b\u201d:<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/relational-expressions-using-awk.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110534\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/relational-expressions-using-awk.webp\" alt=\"relational expressions using awk\" width=\"780\" height=\"58\" \/><\/a><\/p>\n<h4>Range patterns using Linux AWK<\/h4>\n<p>Another type of pattern is the range pattern, which consists of 2 comma-separated patterns. The range expression performs the specified actions for each record between th occurrence of pattern one and pattern 2.<\/p>\n<p>For example, the command shown below will display the word which starts with \u201cclerk\u201d and ends with \u201cmanager\u201d, not the other way round:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">awk '\/clerk\/, \/manager\/ {print $1, $2}' expression.txt<\/pre>\n<h4><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/range-patterns-using-awk.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110535\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/range-patterns-using-awk.webp\" alt=\"range patterns using awk\" width=\"780\" height=\"302\" \/><\/a><\/h4>\n<h4>Special expression patterns using AWK<\/h4>\n<p>These special expression patterns include \u201cBEGIN\u201d and \u201cEND\u201d which denote what to print at the beginning and end of the specified file. For example, the command shown below will print the message \u201clist of employees\u201d at the beginning and \u201cend of list\u201d at the end:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">awk 'BEGIN { print \"List of employees:\" }; {print $1, $2}; END {print \"End of list\"}' expression.txt<\/pre>\n<h4><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/special-expression-patterns-using-awk.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110536\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/special-expression-patterns-using-awk.webp\" alt=\"special expression patterns using awk\" width=\"1194\" height=\"359\" \/><\/a><\/h4>\n<h4>Combining patterns<\/h4>\n<p>We cal also combine 2 0r more expressions using the following connectors:<\/p>\n<ul>\n<li>|| &#8211; or<\/li>\n<li>&amp;&amp; &#8211; and<\/li>\n<li>! &#8211; not<\/li>\n<\/ul>\n<h3>AWK variables<\/h3>\n<p>The awk command has many built-in variables, these variables denote the fields in the records, for example:<\/p>\n<ul>\n<li>$0 &#8211; represents the whole line<\/li>\n<li>$1 &#8211; represents the first word of a line<\/li>\n<li>$2 &#8211; represents the second word of a line<\/li>\n<\/ul>\n<p>We have already used the above variables in the above examples, let us look at some other new variables in this section:<\/p>\n<h4>1. NR<\/h4>\n<p>This variable counts the number of input lines For example if we have to print the whole line along with its line number, we can use the command:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">awk '{print NR,$0}' expression.txt<\/pre>\n<h4><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/using-nr-variable-of-awk.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110537\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/using-nr-variable-of-awk.webp\" alt=\"using nr variable of awk\" width=\"662\" height=\"211\" \/><\/a><\/h4>\n<h4>2. NF<\/h4>\n<p>The \u201cNF\u201d variable counts the number of words in th current input record and displays the last wors of the file<\/p>\n<p>For example, the command shown below will print the last word of every line in the text file \u201cexpression\u201d:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">awk '{print $NF}' expression.txt<\/pre>\n<h4><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/using-nf-variable-of-awk.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110538\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/using-nf-variable-of-awk.webp\" alt=\"using nf variable of awk\" width=\"706\" height=\"233\" \/><\/a><\/h4>\n<h4>3. FS<\/h4>\n<p>The \u201cFS\u201d variable contains the character used to divide words on the input line. The default separator of words is a space, but you can use the \u201cFS\u201d variable to change the separator to another character.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/using-fs-variable-of-awk.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110539\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/using-fs-variable-of-awk.webp\" alt=\"using fs variable of awk\" width=\"765\" height=\"328\" \/><\/a><\/p>\n<h4>4. RS<\/h4>\n<p>The \u201cRS\u201d variable stores the current record separator character. The default record separator is a new line.<\/p>\n<h4>5. OFS<\/h4>\n<p>The \u201cOFS\u201d variable stores the output field separator.<\/p>\n<p>For example, the command shown below will separate the 2nd word and 1st word by the text \u201cis a\u201d.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">awk 'OFS=\" is a \" {print $2,$1}' expression.txt<\/pre>\n<h4><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/using-ofs-variable.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110540\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/using-ofs-variable.webp\" alt=\"using ofs variable\" width=\"780\" height=\"375\" \/><\/a><\/h4>\n<h4>6. FNR<\/h4>\n<p>This variable contains the ordinal number of the current record in the current file.<\/p>\n<h4>7. FILENAME<\/h4>\n<p>This variable contains the name of the current input file.<\/p>\n<h4>8. ORS<\/h4>\n<p>This variable contains the output record separator, just like OFS word separation, ORS is for line separation.<\/p>\n<h4>9. OFMT<\/h4>\n<p>This variable contains the output format for numbers.<\/p>\n<h4>9. SUBSEP<\/h4>\n<p>This variable contains the Character to separate multiple subscripts.<\/p>\n<h4>10. ARGC<\/h4>\n<p>This variable contains the count of the arguments.<\/p>\n<h3>AWK actions<\/h3>\n<p>We have already seen that the syntax of awk has a field that accepts the actions. We can also just specify the action without manipulating the text.<\/p>\n<p>For example, the command shown below will simply print the message \u201cThis is an action\u201d on the terminal:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">awk '{print \"This is an action\"}'<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/awk-actions.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110541\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/awk-actions.webp\" alt=\"awk actions\" width=\"752\" height=\"262\" \/><\/a><\/p>\n<p>The command will execute every time you hit \u201center\u201d, terminate it press \u201cctrl\u201d + \u201cD\u201d.<\/p>\n<h3>Built-in functions of the AWK command<\/h3>\n<p>The AWK command comes with a lot of built-in functions like sqrt and atan2:<\/p>\n<p>awk &#8216;BEGIN { print sqrt(625)}&#8217;<\/p>\n<p>awk &#8216;BEGIN { print sqrt((2+3)*5)}&#8217;<\/p>\n<p>awk &#8216;BEGIN {print atan2(0, -1)}&#8217;<\/p>\n<p>awk &#8216;BEGIN {print atan2(0, -1)*100}&#8217;<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/built-in-functions-of-awk-command.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110542\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/built-in-functions-of-awk-command.webp\" alt=\"built in functions of awk command\" width=\"694\" height=\"216\" \/><\/a><\/p>\n<h3>The \u201cBEGIN\u201d and \u201cEND\u201d rules<\/h3>\n<p>The \u2018BEGIN\u2019 rule is executed at the very beginning of the awk command. In fact, it is the first thing to even be executed, even before awk processes the input text.<\/p>\n<p>The \u2018END\u2019 rule is executed after all the processes have been completed. You can have multiple \u2018BEGIN\u2019 and \u2018END\u2019 rules in an awk command, and they will execute in the same order.<\/p>\n<h3>Scripting using awk<\/h3>\n<p>Instead of simply writing single line command, we can use awk to create a script program that can later be run<\/p>\n<p>Let us write a simple script program and save it as a \u201c.awk\u201d file:<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/writing-a-script-program-using-the-awk-command.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110543\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/writing-a-script-program-using-the-awk-command.webp\" alt=\"writing a script program using the awk command\" width=\"475\" height=\"490\" \/><\/a><\/p>\n<p>After you create the file, add the permission to execute it by using the command \u201cchmod +x &lt;filename&gt;\u201d<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/giving-the-file-execute-permissions.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110544\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/giving-the-file-execute-permissions.webp\" alt=\"giving the file execute permissions\" width=\"546\" height=\"62\" \/><\/a><\/p>\n<p>Then execute the file by using th command: .\/&lt;name&gt;.awk \/etc\/passwd<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/output-of-the-shell-program.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110545\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/output-of-the-shell-program.webp\" alt=\"output of the shell program\" width=\"780\" height=\"787\" \/><\/a><\/p>\n<p>We passed the \/etc\/passwd directory as an argument, it will print the number of accounts after successful execution just as anticipated.<\/p>\n<p>Using all of this basic knowledge, we can use the awk command to do different tasks like:<\/p>\n<p><strong>Filtering using Linux AWK<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">awk 'length($0) &gt; 8' \/etc\/shells<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/filtering-using-the-awk-command.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110546\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/filtering-using-the-awk-command.webp\" alt=\"filtering using the awk command\" width=\"646\" height=\"211\" \/><\/a><\/p>\n<p>In the above output, we passed the \/etc\/shells system file as an argument and filtered the output to contain only the lines containing more than 8 characters.<\/p>\n<p><strong>Counting using Linux AWK Command<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">awk '{ print \"The number of characters in line\", NR,\"=\" length($0) }' employees.txt<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/counting-using-the-awk-command.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110547\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/counting-using-the-awk-command.webp\" alt=\"counting using the awk command\" width=\"780\" height=\"136\" \/><\/a><\/p>\n<p>In the above output, we counted the number of characters per each line using the NR variable.<\/p>\n<p>Apart from these, we can perform many tasks using the AWK command.<\/p>\n<h3>Summary<\/h3>\n<p>As you have seen, linux awk command is a really beautiful command that lets you do complex tasks in just 1 line of code! It is not necessarily simple, but it helps very much in the efficiency of workflow as it can easily process and manipulate texts of large directories, logs, and files.<\/p>\n<p>You have now learned what AWK is, why we use it, how it works, the syntax, and the options of the AWK command. We have also seen the technical part of it as we went through the statements (if-else, while, for, break), patterns, variables, actions, and how to script using the AWK command.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, you will learn all there is to the AWK command in Linux-based operating systems. We will go through what the awk command is, why it is used, how it works, syntax,&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":110527,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[27066,27067,27068],"class_list":["post-110396","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","tag-awk-command-in-linux","tag-linux-awk","tag-need-of-awk"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>AWK Command in Linux - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn what the awk command in linux is, why it is used, how it works, syntax, options, and the built-in variables of the awk command.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/awk-command-in-linux\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"AWK Command in Linux - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn what the awk command in linux is, why it is used, how it works, syntax, options, and the built-in variables of the awk command.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/awk-command-in-linux\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-02T03:30:46+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-09-02T04:11:39+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/awk-command-in-linux.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"16 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"AWK Command in Linux - DataFlair","description":"Learn what the awk command in linux is, why it is used, how it works, syntax, options, and the built-in variables of the awk command.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/awk-command-in-linux\/","og_locale":"en_US","og_type":"article","og_title":"AWK Command in Linux - DataFlair","og_description":"Learn what the awk command in linux is, why it is used, how it works, syntax, options, and the built-in variables of the awk command.","og_url":"https:\/\/data-flair.training\/blogs\/awk-command-in-linux\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2022-09-02T03:30:46+00:00","article_modified_time":"2022-09-02T04:11:39+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/awk-command-in-linux.webp","type":"image\/webp"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"16 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/awk-command-in-linux\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/awk-command-in-linux\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/b49855299264df5e27e3ec6c2cd9fde9"},"headline":"AWK Command in Linux","datePublished":"2022-09-02T03:30:46+00:00","dateModified":"2022-09-02T04:11:39+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/awk-command-in-linux\/"},"wordCount":2379,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/awk-command-in-linux\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/awk-command-in-linux.webp","keywords":["AWK Command in Linux","Linux AWK","need of AWK"],"articleSection":["Linux Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/awk-command-in-linux\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/awk-command-in-linux\/","url":"https:\/\/data-flair.training\/blogs\/awk-command-in-linux\/","name":"AWK Command in Linux - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/awk-command-in-linux\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/awk-command-in-linux\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/awk-command-in-linux.webp","datePublished":"2022-09-02T03:30:46+00:00","dateModified":"2022-09-02T04:11:39+00:00","description":"Learn what the awk command in linux is, why it is used, how it works, syntax, options, and the built-in variables of the awk command.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/awk-command-in-linux\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/awk-command-in-linux\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/awk-command-in-linux\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/awk-command-in-linux.webp","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/awk-command-in-linux.webp","width":1200,"height":628,"caption":"awk command in linux"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/awk-command-in-linux\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Linux Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/linux\/"},{"@type":"ListItem","position":3,"name":"AWK Command in Linux"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/b49855299264df5e27e3ec6c2cd9fde9","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/ef46b745ddad2fad690af626c6ef29b91809ad0a9f5ef398d07817d8cad042f5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/ef46b745ddad2fad690af626c6ef29b91809ad0a9f5ef398d07817d8cad042f5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ef46b745ddad2fad690af626c6ef29b91809ad0a9f5ef398d07817d8cad042f5?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team is a group of passionate educators and industry experts dedicated to providing high-quality online learning resources on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. With years of experience in the field, the team aims to simplify complex topics and help learners advance their careers. At DataFlair, we believe in empowering students and professionals with the knowledge and skills needed to thrive in today\u2019s fast-paced tech industry. Follow us for Free courses, expert insights, tutorials, and practical tips to boost your learning journey.","url":"https:\/\/data-flair.training\/blogs\/author\/datafbdad\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/110396","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=110396"}],"version-history":[{"count":3,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/110396\/revisions"}],"predecessor-version":[{"id":110549,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/110396\/revisions\/110549"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/110527"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=110396"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=110396"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=110396"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}