Awk Introduction Tutorial – 7 Awk Print Examples


/ Published in: Bash
Save to your folder(s)



Copy this code and paste it in your HTML
  1. by SASIKALA on JANUARY 6, 2010
  2.  
  3.  
  4. This is the first article on the new awk tutorial series. We’ll be posting several articles on awk in the upcoming weeks that will cover all features of awk with practical examples.
  5.  
  6. In this article, let us review the fundamental awk working methodology along with 7 practical awk print examples.
  7.  
  8. Note: Make sure you review our earlier Sed Tutorial Series.
  9.  
  10. Awk Introduction and Printing Operations
  11.  
  12. 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 “Aho, Weinberger, and Kernighan”
  13.  
  14. 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.
  15.  
  16. Some of the key features of Awk are:
  17.  
  18. Awk views a text file as records and fields.
  19. Like common programming language, Awk has variables, conditionals and loops
  20. Awk has arithmetic and string operators.
  21. Awk can generate formatted reports
  22. 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.
  23.  
  24.  
  25.  
  26. Syntax:
  27.  
  28. awk '/search pattern1/ {Actions}
  29. /search pattern2/ {Actions}' file
  30. In the above awk syntax:
  31.  
  32. search pattern is a regular expression.
  33. Actions – statement(s) to be performed.
  34. several patterns and actions are possible in Awk.
  35. file – Input file.
  36. Single quotes around program is to avoid shell not to interpret any of its special characters.
  37. Awk Working Methodology
  38.  
  39. Awk reads the input files one line at a time.
  40. For each line, it matches with given pattern in the given order, if matches performs the corresponding action.
  41. If no pattern matches, no action will be performed.
  42. In the above syntax, either search pattern or action are optional, But not both.
  43. If the search pattern is not given, then Awk performs the given actions for each line of the input.
  44. If the action is not given, print all that lines that matches with the given patterns which is the default action.
  45. Empty braces with out any action does nothing. It wont perform default printing operation.
  46. Each statement in Actions should be delimited by semicolon.
  47. Let us create employee.txt file which has the following content, which will be used in the
  48. examples mentioned below.
  49.  
  50. $cat employee.txt
  51. 100 Thomas Manager Sales $5,000
  52. 200 Jason Developer Technology $5,500
  53. 300 Sanjay Sysadmin Technology $7,000
  54. 400 Nisha Manager Marketing $9,500
  55. 500 Randy DBA Technology $6,000
  56. Awk Example 1. Default behavior of Awk
  57.  
  58. By default Awk prints every line from the file.
  59.  
  60. $ awk '{print;}' employee.txt
  61. 100 Thomas Manager Sales $5,000
  62. 200 Jason Developer Technology $5,500
  63. 300 Sanjay Sysadmin Technology $7,000
  64. 400 Nisha Manager Marketing $9,500
  65. 500 Randy DBA Technology $6,000
  66. In the above example pattern is not given. So the actions are applicable to all the lines.
  67. Action print with out any argument prints the whole line by default. So it prints all the
  68. lines of the file with out fail. Actions has to be enclosed with in the braces.
  69.  
  70. Awk Example 2. Print the lines which matches with the pattern.
  71.  
  72. $ awk '/Thomas/
  73. > /Nisha/' employee.txt
  74. 100 Thomas Manager Sales $5,000
  75. 400 Nisha Manager Marketing $9,500
  76. In the above example it prints all the line which matches with the ‘Thomas’ or ‘Nisha’. It has two patterns. Awk accepts any number of patterns, but each set (patterns and its corresponding actions) has to be separated by newline.
  77.  
  78. Awk Example 3. Print only specific field.
  79.  
  80. 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.
  81.  
  82. $ awk '{print $2,$5;}' employee.txt
  83. Thomas $5,000
  84. Jason $5,500
  85. Sanjay $7,000
  86. Nisha $9,500
  87. Randy $6,000
  88.  
  89. $ awk '{print $2,$NF;}' employee.txt
  90. Thomas $5,000
  91. Jason $5,500
  92. Sanjay $7,000
  93. Nisha $9,500
  94. Randy $6,000
  95. In the above example $2 and $5 represents Name and Salary respectively. We can get the Salary using $NF also, where $NF represents last field. In the print statement ‘,’ is a concatenator.
  96.  
  97. Awk Example 4. Initialization and Final Action
  98.  
  99. Awk has two important patterns which are specified by the keyword called BEGIN and END.
  100.  
  101. Syntax:
  102.  
  103. BEGIN { Actions}
  104. {ACTION} # Action for everyline in a file
  105. END { Actions }
  106.  
  107. # is for comments in Awk
  108. Actions specified in the BEGIN section will be executed before starts reading the lines from the input.
  109. END actions will be performed after completing the reading and processing the lines from the input.
  110.  
  111. $ awk 'BEGIN {print "Name\tDesignation\tDepartment\tSalary";}
  112. > {print $2,"\t",$3,"\t",$4,"\t",$NF;}
  113. > END{print "Report Generated\n--------------";
  114. > }' employee.txt
  115. Name Designation Department Salary
  116. Thomas Manager Sales $5,000
  117. Jason Developer Technology $5,500
  118. Sanjay Sysadmin Technology $7,000
  119. Nisha Manager Marketing $9,500
  120. Randy DBA Technology $6,000
  121. Report Generated
  122. --------------
  123. In the above example, it prints headline and last file for the reports.
  124.  
  125. Awk Example 5. Find the employees who has employee id greater than 200
  126.  
  127. $ awk '$1 >200' employee.txt
  128. 300 Sanjay Sysadmin Technology $7,000
  129. 400 Nisha Manager Marketing $9,500
  130. 500 Randy DBA Technology $6,000
  131. 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.
  132.  
  133. Awk Example 6. Print the list of employees in Technology department
  134.  
  135. Now department name is available as a fourth field, so need to check if $4 matches with the string “Technology”, if yes print the line.
  136.  
  137. $ awk '$4 ~/Technology/' employee.txt
  138. 200 Jason Developer Technology $5,500
  139. 300 Sanjay Sysadmin Technology $7,000
  140. 500 Randy DBA Technology $6,000
  141. Operator ~ is for comparing with the regular expressions. If it matches the default action i.e print whole line will be performed.
  142.  
  143. Awk Example 7. Print number of employees in Technology department
  144.  
  145. 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.
  146.  
  147. $ awk 'BEGIN { count=0;}
  148. $4 ~ /Technology/ { count++; }
  149. END { print "Number of employees in Technology Dept =",count;}' employee.txt
  150. Number of employees in Tehcnology Dept = 3
  151. Then at the end of the process, just print the value of count which gives you the number of employees in Technology department.
  152.  
  153.  

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.