AWK Basic Syntax - AWK

What is AWK - Basic Syntax?

AWK is very easy to use. We can facilitate AWK commands either in both the ways either from the command line or in the form of a text file consisiting AWK commands.

What is AWK Command Line?

We can define an AWK command in between single quotes at command line as shown below:

Example

Let us take a text file marks.txt with the below content −

Let us show the entire content of the file with the AWK as shown below −

Example

Upon executing this code, you get the below output −

Output

What is AWK Program File?

We can facilitate AWK commands in a script file as shown below –

Firstly, create a text file command.awk consisting of the AWK command as shown –

Now we may guide the AWK to read commands from the text file and execute the action. Here, we accomplish the same result as shown in the earlier example.

Example

Upon executing this code, you will get the below output −

Output

What are the AWK Standard Options?

AWK supports the below standard options which can be facilitated from the command line.

The -v option

This option will assign a value to a variable. It permits assignment in prior the program execution. The below example explains the usage of the -v option.

Example

Upon executing this code, you get the below result −

Output

The --dump-variables[=file] option

It will print a sorted list of global variables and their ultimate values to file. Default file is awkvars.out.

Example

Upon executing the above code, you get the below result −

Output

The --help option

This option will print the help message on standard output.

Example

Upon executing this code, you get the below output −

Output

This option will enable you checking of non-portable or problematic constructs. When an argument fatal is facilitated, it treats as warning messages as errors. The below example explains this −

Example

Upon executing this code, you get the below result −

Output

The --posix option

This option will turn on strict POSIX compatibility,where all common and gawk-specific extensions are disabled.

The --profile[=file] option

This option will generate a pretty-printed version of the program in file. Default file is awkprof.out. Following simple example explains this −

Example

Upon executing this code, you get the below output −

Output

The --traditional option

This option will disable all gawk-specific extensions.

The --version option

This option will display the information related to version of the AWK program.

Example

Upon executing this code, it procures the elow result −

Output

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

AWK Topics