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 “Aho, Weinberger, and Kernighan”
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 reads from a file or from its standard input, and outputs to its standard output. Awk does not get along with non-text files. The Syntax:
- awk '/search pattern1/ {Actions}
- /search pattern2/ {Actions}' file
Awk Working Methodology
Awk reads the input files one line at a time.
Let us create employee.txt file which has the following content, which will be used in the examples mentioned below.
- examplee.txt
- 100 Thomas Manager Sales $5,000
- 200 Jason Developer Technology $5,500
- 300 Sanjay Sysadmin Technology $7,000
- 400 Nisha Manager Marketing $9,500
- 500 Randy DBA Technology $6,000
Awk Example 1. Default behavior of Awk
By default Awk prints every line from the file.
In the above example pattern is not given. So the actions are applicable to all the lines. Action print with out any argument prints the whole line by default. So it prints all the lines of the file with out fail. Actions has to be enclosed with in the braces.
Awk Example 2. Print the lines which matches with the pattern.
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.
Awk Example 3. Print only specific field.
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.
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.
Awk Example 4. Initialization and Final Action
Awk has two important patterns which are specified by the keyword called BEGIN and END. Syntax:
- BEGIN { Actions}
- {ACTION} # Action for everyline in a file
- END { Actions }
- # is for comments in Awk
In the above example, it prints headline and last file for the reports.
Awk Example 5. Find the employees who has employee id greater than 200
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.
Awk Example 6. Print the list of employees in Technology department
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.
Operator ~ is for comparing with the regular expressions. If it matches the default action i.e print whole line will be performed.
Awk Example 7. Print number of employees in Technology department
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.
Then at the end of the process, just print the value of count which gives you the number of employees in Technology department.
Recommended Reading
Sed and Awk 101 Hacks, by Ramesh Natarajan. 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’ve 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’ve been using Sed and Awk for several years and have not read this book, please do yourself a favor and read this book. You’ll be amazed with the capabilities of Sed and Awk utilities.
Supplement
* Awk User-defined Variables with 3 Practical Examples
* 8 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR
* 7 Powerful Awk Operators Examples (Unary, Binary, ...itional, Reg-Ex Awk Operators)
* 4 Awk If Statement Examples ( if, if else, if else if, : ? )
* Caught In the Loop? Awk While, Do While, For Loop,...Break, Continue, Exit Examples
* Awk Tutorial
沒有留言:
張貼留言