{"id":834,"date":"2015-05-20T08:39:58","date_gmt":"2015-05-20T17:39:58","guid":{"rendered":"http:\/\/blog.box.kr\/?p=834"},"modified":"2015-05-20T08:39:58","modified_gmt":"2015-05-20T17:39:58","slug":"scrap","status":"publish","type":"post","link":"https:\/\/blog.box.kr\/?p=834","title":{"rendered":"[scrap]awk introduction tutorial 7 awk print examples"},"content":{"rendered":"<p><a href=\"http:\/\/www.thegeekstuff.com\/2010\/01\/awk-introduction-tutorial-7-awk-print-examples\/\">http:\/\/www.thegeekstuff.com\/2010\/01\/awk-introduction-tutorial-7-awk-print-examples\/<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>This is the first article on the new awk tutorial series. We\u2019ll be posting several articles on awk in the upcoming weeks that will cover all features of awk with practical examples.<\/p>\n<p>In this article, let us review the fundamental awk working methodology along with 7 practical awk print examples.<br \/>\n<span id=\"more-3059\"><\/span><br \/>\n<strong>Note:<\/strong> Make sure you review our earlier <a href=\"http:\/\/www.thegeekstuff.com\/tag\/sed-tips-and-tricks\/\">Sed Tutorial Series<\/a>.<\/p>\n<h2>Awk Introduction and Printing Operations<\/h2>\n<p>Awk is a programming language which allows easy manipulation of structured data and the generation of formatted reports. Awk stands for the names of its authors \u201c<strong>A<\/strong>ho, <strong>W<\/strong>einberger, and <strong>K<\/strong>ernighan\u201d<\/p>\n<p>The Awk is mostly used for pattern scanning and processing. It searches one or more files to see if they contain lines that matches with the specified patterns and then perform associated actions.<\/p>\n<p>Some of the key features of Awk are:<\/p>\n<ul>\n<li>Awk views a text file as records and fields.<\/li>\n<li>Like common programming language, Awk has variables, conditionals and loops<\/li>\n<li>Awk has arithmetic and string operators.<\/li>\n<li>Awk can generate formatted reports<\/li>\n<\/ul>\n<p>Awk reads from a file or from its standard input, and outputs to its standard output. Awk does not get along with non-text files.<\/p>\n<p>&nbsp;<\/p>\n<div>\n<\/div>\n<p>&nbsp;<\/p>\n<pre>Syntax:\n\nawk '\/search pattern1\/ {Actions}\n     \/search pattern2\/ {Actions}' file<\/pre>\n<p>In the above awk syntax:<\/p>\n<ul>\n<li>search pattern is a regular expression.<\/li>\n<li>Actions \u2013 statement(s) to be performed.<\/li>\n<li>several patterns and actions are possible in Awk.<\/li>\n<li>file \u2013 Input file.<\/li>\n<li>Single quotes around program is to avoid shell not to interpret any of its special characters.<\/li>\n<\/ul>\n<h2>Awk Working Methodology<\/h2>\n<ol>\n<li>Awk reads the input files one line at a time.<\/li>\n<li>For each line, it matches with given pattern in the given order, if matches performs the corresponding action.<\/li>\n<li>If no pattern matches, no action will be performed.<\/li>\n<li>In the above syntax, either search pattern or action are optional, But not both.<\/li>\n<li>If the search pattern is not given, then Awk performs the given actions for each line of the input.<\/li>\n<li>If the action is not given, print all that lines that matches with the given patterns which is the default action.<\/li>\n<li>Empty braces with out any action does nothing. It wont perform default printing operation.<\/li>\n<li>Each statement in Actions should be delimited by semicolon.<\/li>\n<\/ol>\n<p>Let us create employee.txt file which has the following content, which will be used in the<br \/>\nexamples mentioned below.<\/p>\n<pre>$cat employee.txt\n100  Thomas  Manager    Sales       $5,000\n200  Jason   Developer  Technology  $5,500\n300  Sanjay  Sysadmin   Technology  $7,000\n400  Nisha   Manager    Marketing   $9,500\n500  Randy   DBA        Technology  $6,000<\/pre>\n<h3>Awk Example 1. Default behavior of Awk<\/h3>\n<p>By default Awk prints every line from the file.<\/p>\n<pre>$ awk '{print;}' employee.txt\n100  Thomas  Manager    Sales       $5,000\n200  Jason   Developer  Technology  $5,500\n300  Sanjay  Sysadmin   Technology  $7,000\n400  Nisha   Manager    Marketing   $9,500\n500  Randy   DBA        Technology  $6,000<\/pre>\n<p>In the above example pattern is not given. So the actions are applicable to all the lines.<br \/>\nAction print with out any argument prints the whole line by default. So it prints all the<br \/>\nlines of the file with out fail. Actions has to be enclosed with in the braces.<\/p>\n<h3>Awk Example 2. Print the lines which matches with the pattern.<\/h3>\n<pre>$ awk '\/Thomas\/\n&gt; \/Nisha\/' employee.txt\n100  Thomas  Manager    Sales       $5,000\n400  Nisha   Manager    Marketing   $9,500<\/pre>\n<p>In the above example it prints all the line which matches with the \u2018Thomas\u2019 or \u2018Nisha\u2019. It has two patterns. Awk accepts any number of patterns, but each set (patterns and its corresponding actions) has to be separated by newline.<\/p>\n<h3>Awk Example 3. Print only specific field.<\/h3>\n<p>Awk has number of built in variables. For each record i.e line, it splits the record delimited by whitespace character by default and stores it in the $n variables. If the line has 4 words, it will be stored in $1, $2, $3 and $4. $0 represents whole line. NF is a built in variable which represents total number of fields in a record.<\/p>\n<pre>$ awk '{print $2,$5;}' employee.txt\nThomas $5,000\nJason $5,500\nSanjay $7,000\nNisha $9,500\nRandy $6,000\n\n$ awk '{print $2,$NF;}' employee.txt\nThomas $5,000\nJason $5,500\nSanjay $7,000\nNisha $9,500\nRandy $6,000<\/pre>\n<p>In the above example $2 and $5 represents Name and Salary respectively. We can get the Salary using\u00a0 $NF also, where $NF represents last field. In the print statement \u2018,\u2019 is a concatenator.<\/p>\n<h3>Awk Example 4. Initialization and Final Action<\/h3>\n<p>Awk has two important patterns which are specified by the keyword called BEGIN and END.<\/p>\n<pre>Syntax:\n\nBEGIN { Actions}\n{ACTION} # Action for everyline in a file\nEND { Actions }\n\n# is for comments in Awk<\/pre>\n<p>Actions specified in the BEGIN section will be executed before starts reading the lines from the input.<br \/>\nEND actions will be performed after completing the reading and processing the lines from the input.<\/p>\n<pre>$ awk 'BEGIN {print \"NametDesignationtDepartmenttSalary\";}\n&gt; {print $2,\"t\",$3,\"t\",$4,\"t\",$NF;}\n&gt; END{print \"Report Generatedn--------------\";\n&gt; }' employee.txt\nName\tDesignation\tDepartment\tSalary\nThomas \t Manager \t Sales \t         $5,000\nJason \t Developer \t Technology \t $5,500\nSanjay \t Sysadmin \t Technology \t $7,000\nNisha \t Manager \t Marketing \t $9,500\nRandy \t DBA \t \t Technology \t $6,000\nReport Generated\n--------------<\/pre>\n<p>In the above example, it prints headline and last file for the reports.<\/p>\n<h3>Awk Example 5. Find the employees who has employee id greater than 200<\/h3>\n<pre>$ awk '$1 &gt;200' employee.txt\n300  Sanjay  Sysadmin   Technology  $7,000\n400  Nisha   Manager    Marketing   $9,500\n500  Randy   DBA        Technology  $6,000<\/pre>\n<p>In the above example, first field ($1) is employee id. So if $1 is greater than 200, then just do the default print action to print the whole line.<\/p>\n<h3>Awk Example 6. Print the list of employees in Technology department<\/h3>\n<p>Now department name is available as a fourth field, so need to check if $4 matches with the string \u201cTechnology\u201d, if yes print the line.<\/p>\n<pre>$ awk '$4 ~\/Technology\/' employee.txt\n200  Jason   Developer  Technology  $5,500\n300  Sanjay  Sysadmin   Technology  $7,000\n500  Randy   DBA        Technology  $6,000<\/pre>\n<p>Operator ~ is for comparing with the regular expressions. If it matches the default action i.e print whole line will be\u00a0 performed.<\/p>\n<h3>Awk Example 7. Print number of employees in Technology department<\/h3>\n<p>The below example, checks if the department is Technology, if it is yes, in the Action, just increment the count variable, which was initialized with zero in the BEGIN section.<\/p>\n<pre>$ awk 'BEGIN { count=0;}\n$4 ~ \/Technology\/ { count++; }\nEND { print \"Number of employees in Technology Dept =\",count;}' employee.txt\nNumber of employees in Tehcnology Dept = 3<\/pre>\n<p>Then at the end of the process, just print the value of count which gives you the number of employees in Technology department.<\/p>\n<h3>Recommended Reading<\/h3>\n<p><strong><img decoding=\"async\" class=\"frame alignright size-full\" title=\"Sed and Awk 101 Hacks\" src=\"https:\/\/i0.wp.com\/static.thegeekstuff.com\/images\/sed-and-awk-132.png?w=623\" alt=\"\" data-recalc-dims=\"1\" \/><a href=\"http:\/\/www.thegeekstuff.com\/sed-awk-101-hacks-ebook\/\" rel=\"nofollow\">Sed and Awk 101 Hacks<\/a>, by Ramesh Natarajan<\/strong>. I spend several hours a day on UNIX \/ Linux environment dealing with text files (data, config, and log files). I use Sed and Awk for all my my text manipulation work. Based on my Sed and Awk experience, I\u2019ve written Sed and Awk 101 Hacks eBook that contains 101 practical examples on various advanced features of Sed and Awk that will enhance your UNIX \/ Linux life. Even if you\u2019ve been using Sed and Awk for several years and have not read this book, please do yourself a favor and read this book. You\u2019ll be amazed with the capabilities of Sed and Awk utilities.<\/p>\n<h3>Additional Awk Articles<\/h3>\n<ul>\n<li><a href=\"http:\/\/www.thegeekstuff.com\/2010\/01\/awk-tutorial-understand-awk-variables-with-3-practical-examples\/\">Awk User-defined Variables with 3 Practical Examples<\/a><\/li>\n<li><a href=\"http:\/\/www.thegeekstuff.com\/2010\/01\/8-powerful-awk-built-in-variables-fs-ofs-rs-ors-nr-nf-filename-fnr\/\">8 Powerful Awk Built-in Variables<\/a> \u2013 FS, OFS, RS, ORS, NR, NF, FILENAME, FNR<\/li>\n<li><a href=\"http:\/\/www.thegeekstuff.com\/2010\/02\/unix-awk-operators\/\">7 Powerful Awk Operators Examples<\/a> (Unary, Binary, Arithmetic, String, Assignment, Conditional, Reg-Ex Awk Operators)<\/li>\n<li><a href=\"http:\/\/www.thegeekstuff.com\/2010\/02\/awk-conditional-statements\/\">4 Awk If Statement Examples<\/a> ( if, if else, if else if, : ? )<\/li>\n<li><a href=\"http:\/\/www.thegeekstuff.com\/2010\/02\/unix-awk-do-while-for-loops\/\">Caught In the Loop?<\/a> Awk While, Do While, For Loop, Break, Continue, Exit Examples<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>http:\/\/www.thegeekstuff.com\/2010\/01\/awk-introduction-tutorial-7-awk-print-examples\/ &nbsp; This is the first article on the new awk tutorial series. We\u2019ll be posting several articles on awk in the upcoming weeks that will cover all features of awk with practical examples. In this article, let us review the fundamental awk working methodology along with 7 practical awk print examples. Note: Make sure you review our earlier Sed Tutorial Series. Awk Introduction and Printing Operations Awk is a programming language which allows easy manipulation of structured data and the generation of formatted reports. Awk stands for the names of its authors \u201cAho, Weinberger, and Kernighan\u201d The Awk is mostly used for pattern scanning and processing. It searches one or more files to see if they contain lines that matches with the specified patterns and then perform associated actions. Some of the key features of Awk are: Awk views a text file as records and fields. Like common programming language, Awk has variables, conditionals and loops Awk has arithmetic and string operators. Awk can generate formatted reports Awk reads from a file or from its standard input, and outputs to its standard output. Awk does not get along with non-text files. &nbsp; &nbsp; Syntax: awk &#8216;\/search pattern1\/ {Actions} \/search [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"ngg_post_thumbnail":0,"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[4,5],"tags":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/s5q9Zn-scrap","jetpack-related-posts":[{"id":799,"url":"https:\/\/blog.box.kr\/?p=799","url_meta":{"origin":834,"position":0},"title":"[scrap] grep , sed, awk \uc815\uaddc\uc2dd","date":"2015-05-17","format":false,"excerpt":"** grep \ud55c \ub370\uc774\ud130 \uc911 3\ubc88\uc9f8 \ud56d\ubaa9\ub9cc \uac00\uc838 \uc624\uace0 \uc2f6\uc744\ub54c.. grep \".xml\" | awk 'print {$3}' http:\/\/unabated.tistory.com\/447 \uc815\uaddc\uc2dd\uc774\ub780 \ubb34\uc5c7\uc778\uac00 ? \uc5b4\ub5a4 \ubb38\uc790\uc5f4\uc758 \uc9d1\ud569\uc744 \ubb18\uc0ac\ud558\ub294\ub370 \uc0ac\uc6a9\ub418\ub294 \ud14d\uc2a4\ud2b8 \uc2a4\ud2b8\ub9c1 \uc815\ud574\uc9c4 \uad6c\ubb38 \uaddc\uce59\uc5d0 \ub530\ub978\ub2e4 Editor, Utillity, Programming \uc5b8\uc5b4\uc5d0\uc11c \ud14d\uc2a4\ud2b8 \ud328\ud134\uc744 \uae30\uc900\uc73c\ub85c \uac80\uc0c9, \ud639\uc740 \uc870\uc791\ud558\ub294\ub370 \uc0ac\uc6a9\ub41c\ub2e4 \ubcf4\ud1b5 \uc0ac\ub78c\ub4e4\uc774 \uc4f0\ub294 \ud45c\ud604 \uc911\uc5d0\ub3c4 \uadf8\ub300\ub85c \ubb38\uc7a5\uc744 \ud574\uc11d\ud558\uba74\u2026","rel":"","context":"In &quot;Linux&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":830,"url":"https:\/\/blog.box.kr\/?p=830","url_meta":{"origin":834,"position":1},"title":"[scrap]50 Most Frequently Used UNIX \/ Linux Commands (With Examples)","date":"2015-05-20","format":false,"excerpt":"http:\/\/www.thegeekstuff.com\/2010\/11\/50-linux-commands\/ \u00a0 This article provides practical examples for 50 most frequently used commands in Linux \/ UNIX. This is not a comprehensive list by any means, but this should give you a jumpstart on some of the common Linux commands. Bookmark this article for your future reference. Did I miss\u2026","rel":"","context":"In &quot;\uae30\uc220&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":718,"url":"https:\/\/blog.box.kr\/?p=718","url_meta":{"origin":834,"position":2},"title":"How To Install Linux, Nginx, MySQL, PHP (LEMP) stack On CentOS 7","date":"2015-04-15","format":false,"excerpt":"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-install-linux-nginx-mysql-php-lemp-stack-on-centos-7 \u00a0 How To Install Linux, Nginx, MySQL, PHP (LEMP) stack On CentOS 7 Introduction A LEMP software stack is a group of open source software that is typically installed together to enable a server to host dynamic websites and web apps. This term is actually an acronym which represents\u2026","rel":"","context":"In &quot;\uae30\uc220\uc790\ub8cc&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":454,"url":"https:\/\/blog.box.kr\/?p=454","url_meta":{"origin":834,"position":3},"title":"RED5 &amp; FFMPEG &amp; FFserver \uc2a4\ud2b8\ub9ac\ubc0d \uc11c\ubc84 \uad6c\ucd95\ud558\uae30","date":"2014-12-18","format":false,"excerpt":"\u00a0 http:\/\/blog.syszone.co.kr\/2498?category=17 \u00a0 \uc791\uc131\uc790 : \uc11c\uc9c4\uc6b0(alang@syszone.co.kr) \uc791\uc131\uc77c : 2009\ub144 1\uc6d4 5\uc77c 4.1 FFMPEG\ub85c \uc778\ucf54\ub529 \ud658\uacbd \uad6c\ucd95\ud558\uae30 - \uae30\ubcf8 \ud504\ub85c\uadf8\ub7a8 \uc124\uce58 # yum install ruby # yum install ncurses-devel* # yum install lame # yum install libogg # yum install libvorbis # yum install flvtool2 # yum install ffmpeg - \ucf54\ub371\u2026","rel":"","context":"In &quot;\uae30\uc220\uc790\ub8cc&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":832,"url":"https:\/\/blog.box.kr\/?p=832","url_meta":{"origin":834,"position":4},"title":"[scrap]Unix Sed Tutorial: Advanced Sed Substitution Examples","date":"2015-05-20","format":false,"excerpt":"http:\/\/www.thegeekstuff.com\/2009\/10\/unix-sed-tutorial-advanced-sed-substitution-examples\/ This article is part of the on-goingUnix Sed Tips and Tricks series. In our previous sed articles we learned \u2014 sed printing, sed deletion, sed substitute , sed file write, and sed multiple commands. In this article, let us review some interesting workarounds with the \u201cs\u201d substitute command in\u2026","rel":"","context":"In &quot;\uae30\uc220&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":244,"url":"https:\/\/blog.box.kr\/?p=244","url_meta":{"origin":834,"position":5},"title":"\ud29c\ud1a0\ub9ac\uc5bc: \uc2a4\uce7c\ub77c(Scala), akka \ub85c scalable \ud558\uace0, fault-tolerant \ud55c \ub124\ud2b8\uc6cc\ud06c \ucc44\ud305 \uc11c\ubc84\uc640 \ud074\ub77c\uc774\uc5b8\ud2b8 \ub9cc\ub4e4\uae30","date":"2014-07-23","format":false,"excerpt":"\ud29c\ud1a0\ub9ac\uc5bc: \uc2a4\uce7c\ub77c(Scala), akka \ub85c scalable \ud558\uace0, fault-tolerant \ud55c \ub124\ud2b8\uc6cc\ud06c \ucc44\ud305 \uc11c\ubc84\uc640 \ud074\ub77c\uc774\uc5b8\ud2b8 \ub9cc\ub4e4\uae30 SW\uac1c\ubc1c \u00a0http:\/\/doc.akka.io\/docs\/akka\/1.3.1\/scala\/tutorial-chat-ser..","rel":"","context":"In &quot;JAVA&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/blog.box.kr\/index.php?rest_route=\/wp\/v2\/posts\/834"}],"collection":[{"href":"https:\/\/blog.box.kr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.box.kr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.box.kr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.box.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=834"}],"version-history":[{"count":0,"href":"https:\/\/blog.box.kr\/index.php?rest_route=\/wp\/v2\/posts\/834\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.box.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=834"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.box.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=834"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.box.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=834"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}