Learning AWK Programming
eBook - ePub

Learning AWK Programming

  1. English
  2. ePUB (mobile friendly)
  3. Available on iOS & Android
eBook - ePub

Learning AWK Programming

About this book

Text processing and pattern matching simplified

Key Features

  • -Master the fastest and most elegant big data munging language
  • -Implement text processing and pattern matching using the advanced features of AWK and GAWK
  • -Implement debugging and inter-process communication using GAWK

Book Description

AWK is one of the most primitive and powerful utilities which exists in all Unix and Unix-like distributions. It is used as a command-line utility when performing a basic text-processing operation, and as programming language when dealing with complex text-processing and mining tasks. With this book, you will have the required expertise to practice advanced AWK programming in real-life examples.

The book starts off with an introduction to AWK essentials. You will then be introduced to regular expressions, AWK variables and constants, arrays and AWK functions and more. The book then delves deeper into more complex tasks, such as printing formatted output in AWK, control flow statements, GNU's implementation of AWK covering the advanced features of GNU AWK, such as network communication, debugging, and inter-process communication in the GAWK programming language which is not easily possible with AWK.

By the end of this book, the reader will have worked on the practical implementation of text processing and pattern matching using AWK to perform routine tasks.

What you will learn

  • -Create and use different expressions and control flow statements in AWK
  • -Use Regular Expressions with AWK for effective text-processing
  • -Use built-in and user-defined variables to write AWK programs
  • -Use redirections in AWK programs and create structured reports
  • -Handle non-decimal input, 2-way inter-process communication with Gawk
  • -Create small scripts to reformat data to match patterns and process texts

Who this book is for

This book is for developers or analysts who are inclined to learn how to do text processing and data extraction in a Unix-like environment. Basic understanding of Linux operating system and shell scripting will help you to get the most out of the book.

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription.
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn more here.
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Yes! You can use the Perlego app on both iOS or Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app.
Yes, you can access Learning AWK Programming by Shiwang Kalkhanda in PDF and/or ePUB format, as well as other popular books in Computer Science & Data Processing. We have over one million books available in our catalogue for you to explore.

Information

AWK Functions

A function, also known as a named procedure, is a set of instructions that is used by programming languages to return a single result or a set of results. The statement that requests the function is called a function call. The functions extend the usefulness and functionality of AWK. This chapter covers the different types of built-in functions that are available in AWK. The built-in functions of AWK are generally divided into three categories, namely numeric, string, and I/O. Apart from these, we will cover the additional functions provided by GAWK to represent time, to provide type information, and to enable bit manipulation. Then, we will discuss how AWK is used for writing user-defined functions for use in the rest of the program.
In this chapter, we will cover the following topics:
  • Arithmetic functions
  • String functions
  • Input/output functions
  • Time functions
  • Bit-manipulating functions
  • User-defined functions

Built-in functions

Built-in functions are always available to the programmer for use in the program. This section covers the built-in functions in AWK. These functions generally accept arguments as input and return a value. Whitespace is ignored between the built-in function name and the opening parenthesis; however, we should avoid using whitespace in this way as user-defined functions do not permit whitespace.
If an expression is given as an argument to the function, the expression is evaluated before the call is made to the function. For example:
In the preceding case, p is incremented to the value of 6 before the sqrt function is called. It is good practice to evaluate the expression first and then pass the argument to the function.

Arithmetic functions

Arithmetic functions are those built-in functions that deal with numbers. The optional argument is enclosed in square brackets ([]).

The sin (expr) function

The sin(expr) function returns the sine of the expression expr, where expr is expressed in radians. The following code block shows an example of a sine function:
$ vi sinefunc.awk

BEGIN {
print "sin(90) = ", sin(90);
print "sin(45) = ", sin(45);
}

$ awk -f sinefunc.awk
The output of the execution of the previous code is as follows:
sin(90) = 0.893997
sin(45) = 0.850904

The cos (expr) function

The cos (expr) function returns the cosine of the expression expr, where expr is expressed in radians. The following code block shows an example of a cosine function:
$ vi cosfunc.awk

BEGIN {
print "cos(90) = ", cos(90);
print "cos(45) = ", cos(45);
}

$ awk -f cosfunc.awk
The output of the execution of the previous code is as follows:
cos(90) = -0.448074
cos(45) = 0.525322
The remaining trigonometric functions, such as sec(), cosec(), tan(), and cot(), are measured in radians, and can be obtained with the use of the sin() and cos() functions.

The atan2 (x, y) function

The atan2 (x,y) function returns the arc tangent of x/y in radians. It is similar to the arc tangent, except that the signs of both arguments are used to determine the quadrant of the result, which is expressed in radians. The following code block shows an example of the atan2 function:
$ awk 'BEGIN { print "atan2(45,30) = ",atan2(45,30)}'
The output of the execution of the previous code is as follows:
atan2(45,30) = 0.982794

The int (expr) function

The int (expr) function returns the truncated numeric value by removing the digits to the right of the decimal point for the given argument that is the lowest integer of the given number that is returned. If a whole number is given as an argument, it is returned as such, whereas if a floating point number is given as an argument, it returns an integer value truncated towards zero. It does not round the argument value up or down. The following code block shows an example of an integer function:
$ vi intfunc.awk

BEGIN {
print "int(3.5678) = ", int(3.5678);
print "int(5.9876) = ", int(5.9876);
print "int(4) = ", int(4);
print "int(-3.1234)= ", int(-3.1234);
print "int(-4) = ", int(-4);
 }

$ awk -f intfunc.awk
The output of the execution of the previous code is as fol...

Table of contents

  1. Title Page
  2. Copyright and Credits
  3. Dedication
  4. Packt Upsell
  5. Contributors
  6. Preface
  7. Getting Started with AWK Programming
  8. Working with Regular Expressions
  9. AWK Variables and Constants
  10. Working with Arrays in AWK
  11. Printing Output in AWK
  12. AWK Expressions
  13. AWK Control Flow Statements
  14. AWK Functions
  15. GNU's Implementation of AWK – GAWK (GNU AWK)
  16. Practical Implementation of AWK