Reader small image

You're reading from  Learning AWK Programming

Product typeBook
Published inMar 2018
Reading LevelIntermediate
PublisherPackt
ISBN-139781788391030
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Shiwang Kalkhanda
Shiwang Kalkhanda
author image
Shiwang Kalkhanda

Shiwang Kalkhanda (RHCA, RHCSS, MCSE) is a Linux geek and consultant with expertise in the automation of infrastructure deployment and management. He has more than 10 years' experience in security, system, and network administration, and training on open source tech. For most of his automation work, he uses Shell Scripting, Python, and Go. He holds a master's and a bachelor's degree in computer applications. He enjoys traveling and spending time with his children. He is also the author of a book on text processing utilities in Unix-like environments, Learning Awk Programming.
Read more about Shiwang Kalkhanda

Right arrow

Printing Output in AWK

In most programming languages, the most common task is to display or print the output after processing the input. Inside AWK, we have two statements, print and printf, to accomplish the task of generating output. The print statement generates a simple output, while printf is used to generate formatted output or reports. These statements can be used together; the output comes in the order that they were used. In this chapter, we focus on basic and formatted printing (pretty printing). In the end, we will also cover I/O redirections to files instead of printing the output on screen.

In this chapter, we will cover the following topics:

    • Basic printing using the print statement
    • Using an output separator with the print statement
    • Pretty printing with printf
    • Using escape sequences
    • Printing with a format specifier
    • Printing with optional parameters
    • Redirecting...

The print statement

So far, we have been using print statements mainly to produce simple yet standardized output. With a print statement, we specify the expressions to print as a list separated by commas. The output is separated by single spaces, followed by a newline. The print statement has two forms:

print expr1, expr2, ……, exprn
print (expr1, expr2, ………, exprn)

Both print the string value of each expression separated by the Output Field Separator (OFS); the default is a single space followed by the Output Record Separator (ORS); the default is newline. Using parentheses is necessary if an expression uses the > relational operator to mark its differentiation from redirection operator. Here expression could be any AWK expression or any constant string, or number, or field of current input record (like $1, $2, ... and so on). Numeric values...

Role of output separator in print statement

When we print multiple fields separated by comma using print command, it uses OFS and ORS built-in variable values to decide how to print the fields and rows. Output field separator is stored in the OFS variable and output record separator is stored in the ORS variable. By default OFS is set to single space and ORS is set to a single newline. We can change these values anytime as required, but the usually best place to assign new values to OFS and ORS is in the BEGIN statement. For example, in the following example we print all the fields of car database with a colon between them as separator, and the two newlines after each processing record as follows:

$ vi output_separator.awk

BEGIN { OFS = ":"
ORS = "\n\n"
}
{ print $1,$2,$3,$4,$5 }

$ awk -f output_separator.awk cars.dat

The output on execution of the...

Pretty printing with the printf statement

Most of the programs we have written so far have used the print command to display the output. Sometimes, the columns don't line up properly with the print statement. So, to have more control over output formatting we have to use a printf statement. It is very flexible and is used to generate formatted output. It is similar to the C language's printf statement, with only one exception: the absence of a * format specifier. Like the print statement, it can be used with parentheses or without parentheses, as follows:

printf format, expr1, expr2, expr3 ……… exprn
printf ( format, expr1, expr2, expr3 ……… exprn )

The main difference between print and printf is the format argument. It is an expression whose value is taken as a string; it specifies how to output each of the other arguments. It...

Escape sequences for special character printing

As we use a simple string with printf, we can use any escape sequences to print control characters that are difficult to represent. These are special characters that do not represent their literal meaning when used inside a string; instead they represent something special that would otherwise be difficult to represent as such. Most of the escape sequences consist of at least two characters, the first of which is a backslash character \, which is used to escape or mark a special character.

The following table lists the special characters that form the escape sequences with special meanings inside printf:

Special character

Description

\n

Newline

\t

Tab

\v

Vertical tab

\b

Backspace

\r

Carriage return

\f

Form feed

\<any character>

That character

\'

Single quotation

...

Different format control characters in the format specifier

Format specifiers begin with a percentage character (%) and end with a format control character. It tells the printf statement how to output an item. The format control character decides what kind of value to print. The rest of the format specifier is made up of optional modifiers that control field width. The following are the format control characters used in format specifiers with printf in AWK:

  • %c: It prints a single character. If the argument is a number, then its corresponding ASCII character is printed. If a string is given as the argument, then only the first character of that string is printed. For example, if we give 65 to printf for printing, it outputs the letter A, which is the ASCII equivalent of 65:
$ awk 'BEGIN { printf "ASCII representation of 65 = character %c\n", 65 }'

The output...

Format specification modifiers

Each format specification begins with a % and ends with a character that determines the conversion, known as format control letter. In between, it may contain optional modifiers that control how much of the item's value is printed or how much of total space it gets. The following are the possible modifiers that may appear in a printf format specifier.

Printing with fixed column width

To create a fixed-column-width report, we have to specify a number immediately after the % in the format specifier. This number shows the minimum number of characters to be printed. This is the width (minimum size) of the field. If the input in the field becomes large, it automatically grows to prevent information...

Redirecting output to file

Till now we have been sending the output of print and printf commands to stdout, that is, the screen. However, we can also redirect the output to files by using the redirection operator. Redirection is done after the print command. It is the same as we do in shell commands using redirection operator.

There are three forms of output redirection:

  • Output to file
  • Output appended to a file
  • Output through a pipe to another command

Redirecting output to a file (>)

This redirection operator (>) prints the items into the output file. Its syntax is as follows:

print items > demo

In this type of redirection, if the output file named demo exists, then it is erased before the first output is written...

Summary

In this chapter, we learned to use print() and printf() for finer control over output. We began with OFS and ORS for formatting the output, which was followed by introduction to escape sequences in printf for printing special characters. Then we learned how AWK uses format-control characters for different data types, and optional modifiers for modifying the behavior of format control characters. Finally, we covered how the output from both print and printf can be redirected to files and pipes. In the end, we learned the importance of the close() function to close open files and pipes.

In the next chapter, we will learn about different types of expressions in AWK programming language and how they form the core logic of a program.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Learning AWK Programming
Published in: Mar 2018Publisher: PacktISBN-13: 9781788391030
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Shiwang Kalkhanda

Shiwang Kalkhanda (RHCA, RHCSS, MCSE) is a Linux geek and consultant with expertise in the automation of infrastructure deployment and management. He has more than 10 years' experience in security, system, and network administration, and training on open source tech. For most of his automation work, he uses Shell Scripting, Python, and Go. He holds a master's and a bachelor's degree in computer applications. He enjoys traveling and spending time with his children. He is also the author of a book on text processing utilities in Unix-like environments, Learning Awk Programming.
Read more about Shiwang Kalkhanda