{"id":5971,"date":"2021-04-26T11:28:39","date_gmt":"2021-04-26T11:28:39","guid":{"rendered":"https:\/\/linuxways.net\/?p=5971"},"modified":"2021-04-26T11:28:39","modified_gmt":"2021-04-26T11:28:39","slug":"10-awesome-awk-command-examples-in-linux","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/centos\/10-awesome-awk-command-examples-in-linux\/","title":{"rendered":"10 Awesome Awk Command Examples in Linux"},"content":{"rendered":"<p>Awk is the most popular utility that is developed for the purpose of data extraction, text processing, and moreover like creating formatted reports. It is way more similar to sed but more powerful than sed as sed has limitations in text processing. AWK doesn\u2019t have a specific meaning to its name as it is named using the first letter of its developers Alfred Aho, Peter J. Weinberger, and Brian Kernighan.<\/p>\n<p>In this article, we will learn 10 awesome awk commands you must need to know. I have created and added the following set of data in student.txt as an example. The data set has 4 columns where the first field contains the first name, the second field contains the second name, the third field contains age and the last one contains the class.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"727\" height=\"133\" class=\"wp-image-5972\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-37.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-37.jpeg 727w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-37-300x55.jpeg 300w\" sizes=\"auto, (max-width: 727px) 100vw, 727px\" \/><\/p>\n<h4>Printing Specific Field Using Variable<\/h4>\n<p>Awk has many prebuilt variables that have their respective purpose. Using this command we can print all the specific field data using $x where x refers to the field numbering position.<\/p>\n<pre>$ awk '{print $1, $2}' student.txt<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"727\" height=\"136\" class=\"wp-image-5973\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-38.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-38.jpeg 727w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-38-300x56.jpeg 300w\" sizes=\"auto, (max-width: 727px) 100vw, 727px\" \/><\/p>\n<h4>BEGIN Variable<\/h4>\n<p>BEGIN Variable is used to add header or title to resulting data as it executed the script before processing the data. It helps in indexing while formatting the data tables. In the following example, I have printed some text as indexing and then print all student names.<\/p>\n<pre>$ awk 'BEGIN {print \"Students : \"} {print $1}' student.txt<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"727\" height=\"151\" class=\"wp-image-5974\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-39.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-39.jpeg 727w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-39-300x62.jpeg 300w\" sizes=\"auto, (max-width: 727px) 100vw, 727px\" \/><\/p>\n<h4>END Variable<\/h4>\n<p>END is just the opposite of BEGIN as it executes the script after data processing. It can be used for the final reporting of the data set. In the following example, I have printed all the student age and then printed some ending messages.<\/p>\n<pre>$ awk '{print $3}\r\n\r\nEND {\r\n\r\nprint \"These are student age \"\r\n\r\n} ' student.txt<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"727\" height=\"199\" class=\"wp-image-5975\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-40.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-40.jpeg 727w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-40-300x82.jpeg 300w\" sizes=\"auto, (max-width: 727px) 100vw, 727px\" \/><\/p>\n<h4>File Separator<\/h4>\n<p>Space and Tab space are default separators of the awk command however we can separate text based on other separators like comma, slash, etc. To achieve this we need to add the -F flag to the command and the provide separator in a single quotation mark.<\/p>\n<pre>$ awk -F':' '{print $1}' \/etc\/passwd<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"727\" height=\"148\" class=\"wp-image-5976\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-41.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-41.jpeg 727w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-41-300x61.jpeg 300w\" sizes=\"auto, (max-width: 727px) 100vw, 727px\" \/><\/p>\n<h4>Running Script From File<\/h4>\n<p>We can execute the awk script from the file also which provides us the tendency of creating reports efficiently. For this, you need to create the file then write the script and execute it using the awk command. For the demo, you can create a file name demo_script and copy-paste the following script.<\/p>\n<pre>$ vi demo_script\r\n\r\n{\r\n\r\nsum+=$3\r\n\r\n}\r\n\r\nEND {\r\n\r\nprint(\"Sum of all student age is\", sum)\r\n\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"727\" height=\"173\" class=\"wp-image-5977\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-42.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-42.jpeg 727w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-42-300x71.jpeg 300w\" sizes=\"auto, (max-width: 727px) 100vw, 727px\" \/><\/p>\n<p>The awk command provides a -f flag for executing the script from the file.<\/p>\n<pre>$ awk -f demo_script student.txt<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"727\" height=\"60\" class=\"wp-image-5978\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-43.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-43.jpeg 727w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-43-300x25.jpeg 300w\" sizes=\"auto, (max-width: 727px) 100vw, 727px\" \/><\/p>\n<h4>Using Multiple Script<\/h4>\n<p>We can execute the multiple scripts using the semicolon. In the following example, I have printed some text then pipe the output, with awk and print out the modified result.<\/p>\n<pre>$ echo \"Hello, Dr. John\" | awk '{$3=\"George\"; print $0}'<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"727\" height=\"61\" class=\"wp-image-5979\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-44.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-44.jpeg 727w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-44-300x25.jpeg 300w\" sizes=\"auto, (max-width: 727px) 100vw, 727px\" \/><\/p>\n<h4>Count Number of Lines<\/h4>\n<p>We can allocate the number to the report using the NR variable which is awk built-in variable that automatically prints the line number to the report.<\/p>\n<pre>$ awk '{print NR \"\\t\" $0}' student.txt<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"727\" height=\"134\" class=\"wp-image-5980\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-45.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-45.jpeg 727w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-45-300x55.jpeg 300w\" sizes=\"auto, (max-width: 727px) 100vw, 727px\" \/><\/p>\n<h4>Count Number of Fields<\/h4>\n<p>Sometimes, while preparing the data we forgot to add data in the specific column which may lead to irregularity in the report. We can count fields using the NF variable which makes us easier to review and arrange the reports.<\/p>\n<pre>$ awk '{print NR\".\",$0 \"\\n Count=\" NF}' student.txt<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"727\" height=\"220\" class=\"wp-image-5981\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-46.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-46.jpeg 727w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-46-300x91.jpeg 300w\" sizes=\"auto, (max-width: 727px) 100vw, 727px\" \/><\/p>\n<h4>If Condition<\/h4>\n<p>We can use if condition in preparing a conditional report. In the following example, we print all the student whose age is below 16<\/p>\n<pre>$ awk '\r\n\r\nBEGIN{\r\n\r\nprint \"Student whose age are under 16 are:\"\r\n\r\n}\r\n\r\n{\r\n\r\nif($3&lt;16){\r\n\r\nprint $1\r\n\r\n}\r\n\r\n}' student.txt<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"727\" height=\"238\" class=\"wp-image-5982\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-47.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-47.jpeg 727w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-47-300x98.jpeg 300w\" sizes=\"auto, (max-width: 727px) 100vw, 727px\" \/><\/p>\n<h4>For Loop<\/h4>\n<p>In the following example, we use for loop to print 5 random numbers in succession. For generating random numbers we will use the rand() function which is a system inbuilt function. This function will generate a random number in decimal so we need to multiply 100 to get random numbers 1 to 100.<\/p>\n<pre>$ awk 'BEGIN {\r\n\r\nfor (i = 1; i &lt;= 5; i++){\r\n\r\nprint int(100 * rand())\r\n\r\n}\r\n\r\n}'<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"727\" height=\"203\" class=\"wp-image-5983\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-48.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-48.jpeg 727w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-48-300x84.jpeg 300w\" sizes=\"auto, (max-width: 727px) 100vw, 727px\" \/><\/p>\n<h2>Conclusion<\/h2>\n<p>In this article, we learned about the 10 awesome awk commands and scripts. I hope you would like this article.<\/p>\n<p>&nbsp;<\/p>","protected":false},"excerpt":{"rendered":"<p>Awk is the most popular utility that is developed for the purpose of data extraction, text processing, and moreover like creating formatted reports. It is way more similar&hellip;<\/p>","protected":false},"author":1,"featured_media":6087,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,4,5,83,165,2],"tags":[291,35],"class_list":["post-5971","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-centos","category-debian","category-mint","category-opensuse","category-red-hat","category-ubuntu","tag-awk","tag-linux"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/5971","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=5971"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/5971\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/6087"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=5971"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=5971"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=5971"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}