7.1. awk¶
7.1.1. What is AWK?¶
AWK is a command-line utility that makes text processing easier. It helps filter and manipulate data, especially when you’re working with data files where you need to apply rules or conditions to extract the information you want.
7.1.2. Why to use AWK?¶
AWK can be incredibly helpful for:
- Filtering Data:
Use commands like “if”, “else,” “for,” and others to filter and extract specific information from your data based on predefined criteria.
- Text Transformation:
Change the format of text, remove unnecessary parts, combine pieces of text, or apply other manipulations.
- Data Analysis:
Summarize and analyze data by calculating averages, finding duplicates, identifying outliers, and more.
It not only works on files, but directly on the output of another program or command.
AWK is a powerful tool that can be used in various fields such as data analysis, scripting, and automation. It’s a versatile command-line utility that can help you process and analyze text data efficiently.
7.1.3. Sample Example¶
Here’s a simple example of how to use AWK
Name Marks
Purnank 89
Meera 92
Dehlia 90
Ved 91
Who got more than 90 marks?
awk '$2 > 90 {print $0}' sample_awk.txt
Name Marks
Meera 92
Ved 91
Same question. Who got more than 90 marks? But print little better.
awk '$2 > 90 {print $1,"\t",$2}' sample_awk.txt
Name Marks
Meera 92
Ved 91
Find which lines are more than 8 characters wide.
awk 'length($0) > 8 {print $0}' sample_awk.txt
Name Marks
Purnank 89
Dehlia 90
7.1.4. Further Reading¶
The goal of this book/document is not to make you and expert at AWK, but just to give you a glimpse as to what is basically possible with AWK. You must be now aware, of the utility AWK and think about using it for post processing some file our output to check some summary as a part of your scripts.
AWK is a powerful tool and it can be used for many different tasks. Here are some of the things that you can do with AWK from the Open Group website: