igawk Command in Linux



The igawk command in Linux enables using the @include directive to include the external .awk files. It is essentially a wrapper around gawk, adding support for include files. This is especially useful for more complex scripts, as it allows splitting up the code and reusing it in other scripts.

Table of Contents

Here is a comprehensive guide to the options available with the igawk command −

Note − The igawk command is considered obsolete because recent versions of gawk now natively support the @include directive. This development removes the need for a separate igawk command for including external files in Awk scripts. It may still exist on some systems and can be useful if older scripts rely on it.

Syntax of igawk Command

The syntax of the igawk command is as follows −

igawk [all gawk options] -f [program-file] [--] [file]

In the above syntax, [all gawk options] represents any options typically used with gawk. The -f option is used to specify an Awk script file to be executed. This is the primary script file, and it may contain @include directives to bring in additional files. The [--] is optional and separates the options from a script. And [file] refers to the input files on which the Awk script will operate.

igawk [all gawk options] [--] [program-text] [file]

In the above syntax, [all gawk options] represents any options typically used with gawk. The [program-text] provides Awk program text directly on the command line instead of using a script file. And [file] is used to specify the input files for processing.

In both syntaxes, igawk allows the use of @include statements within [program-file] or [program-text] to include other Awk files.

igawk Command Options

The igawk uses all options available in the gawk command. Commonly used options are listed below −

Flags Options Description
-F fs --field-separator fs Sets the field separator to fs (e.g., -F, for comma-separated fields)
-v var=value --assign var=value Assigns a value to a variable var before program execution. Useful for passing parameters into the script
-f program-file --file program-file Loads an Awk script from program-file. Essential when running a script stored in a file
-W compat Enables compatibility mode, making gawk behave more like other versions of awk
--lint Warns about code that may cause issues, helping identify potential problems
--profile [=prof_file] Generates a profiling report for the Awk program, useful for optimizing performance.
--help Displays a summary of options and then exits
--version Displays the version of gawk and exits

Example of igawk Command in Linux

This section demonstrates the usage of the igawk command in Linux with examples −

Note − The igawk command, which was designed to add the capability of including external Awk files via the @include directive, is now considered obsolete. This functionality has been integrated into recent versions of gawk, making igawk unnecessary for most use cases. In the following example, the gawk command will be used, however, if your system is still compatible with igawk then simply replace the gawk with igawk.

Create a .awk file that performs a simple functionality, such as adding two numbers. Use the following command to create the .awk file −

sudo nano sum.awk

Now, add the following line to it −

function add(a, b) {
	return a + b
}
igawk Command in Linux1

The add function takes two parameters, a and b, and returns their sum.

Save and exit the editor.

Next, create another file in which the above script file will be included.

sudo nano main.awk

And add the following lines to it −

@include "sum.awk"
{
	print "Sum of two numbers is:", add($1, $2)
}
igawk Command in Linux2

In the above script, the @include "utils.awk" directive tells igawk to load the contents of sum.awk into main.awk. This means that the sum function is available for use in this script. The script processes each line of the input file, where $1 and $2 refer to the first and second fields (columns) of the input data.

Create an input file to process.

sudo nano input.txt

For example, to add two numbers 4 and 9, insert them in the text file separated by a space as shown in the image −

igawk Command in Linux3

Now, run the script (main.awk) using the igawk (gawk) command with the input text file −

gawk -f main.awk input.txt
igawk Command in Linux4

The input file can contain multiple lines as shown below −

igawk Command in Linux5

In such case, the script will perform addition to each line as shown in the following output image −

igawk Command in Linux6

In the above input file (input.txt), the numbers are separated by a space. However, the field separator can be changed, for example, they can be separated by a comma (,).

igawk Command in Linux7

As shown in the following output image, the igawk (gawk) command does not process the input file properly.

igawk Command in Linux8

To obtain the correct output, use the -F or --field-separator option to specify the field separator ( comma in this case) with the igawk (or gawk) command −

gawk -F , -f main.awk input.txt
igawk Command in Linux9

Conclusion

The igawk command was useful for including external Awk files with the @include directive, enhancing code organization in complex scripts. However, recent updates to gawk have made igawk obsolete, as gawk now natively supports this functionality. Consequently, it is recommended to use gawk directly for better efficiency and simpler script management.

In this tutorial, we explained the igawk command, its syntax, options, and usage in Linux with an example.

Advertisements