Home » Linux AWK Command

Linux AWK Command

by Online Tutorials Library

AWK Command

The awk command is used for text processing in Linux. Although, the sed command is also used for text processing, but it has some limitations, so the awk command becomes a handy option for text processing. It provides powerful control to the data.

The Awk is a powerful scripting language used for text scripting. It searches and replaces the texts and sorts, validates, and indexes the database.

It is one of the most widely used tools for the programmer, as they write the scaled-down effective program in the form of a statement to define the text patterns and designs.

It acts as a filter in Linux. It is also referred as gawk (GNU awk) In Linux.

How is it named as AWK?

This command is named by using the first letter of the name of three people who wrote the original version of this command in 1977. Their names are Alfred Aho, Peter Weinberger, and Brian Kernighan and they were from AT & T Bell Laboratories.

Features of AWK command

Various features of the Awk command are as follows:

  • It scans a file line by line.
  • It splits a file into multiple fields.
  • It compares the input text or a segment of a text file.
  • It performs various actions on a file like searching a specified text and more.
  • It formats the output lines.
  • It performs arithmetic and string operations.
  • It applies the conditions and loops on output.
  • It transforms the files and data on a specified structure.
  • It produces the format reports.

Syntax:

The Awk command is used as follows:

The options can be:

  • -f program files: It reads the source code of the script written on the awk command
  • -F fs: It is used as the input field separator.

How to define AWK Script?

To define the awk script, use the awk command followed by curly braces {} surrounded by single quotation mark ” as follows:

The above command will print the inputted string every time we execute the command. Press CTRL+D key to terminate the program. Consider the below output:

AWK Command

AWK Command Examples

To better understand the Awk command, have a look at the below example:

Let’s create a data to apply the various awk operations. Consider student data from different streams.

To create data, execute the cat command as follows:

Press CTRL + D key to save the file and ESC key to exit from the command-line editor. It will create data. Consider the below output:

AWK Command

A student data has been created, and we will operate the awk command on this data.

Example1: List students with the specified pattern.

Consider the below command:

Output:

AWK Command

Example2: Default behaviour of awk command.

If we do not specify the pattern, it will show all of the content of the file.

Consider the below command:

We have not specified any pattern in the above command so, it will display all lines of the file.

Output:

AWK Command

Example3: Print the specified column.

If we specify the column number on this command, it will print that line only. Consider the below output:

The above command will print the column number 1 and 5. If column 5 does not exist in the file system, it will only print column number 1.

Consider the below output:

AWK Command

Consider the below command:

The above command will list the column number 1 & 2. Consider the below output:

AWK Command

Built-in variables in AWK command

Awk command supports many built-in variables, which include $1, $2, and so on, that break the file content into individual segments.

NR: It is used to show the current count of the lines. The awk command performs action once for each line. These lines are said as records.

NF: It is used to count the number of fields within the current database.

FS: It is used to create a field separator character to divide fields into the input lines.

OFS: It is used to store the output field separator. It separates the output fields.

ORS: It is used to store the output record separator. It separates the output records. It prints the content of the ORS command automatically.

Example4: Print the output and display the line number.

To display the line number in output, use the NR variable with the Awk command as follows:

Consider the below output:

AWK Command

Example5: Print the last field of the file.

To display the last field of the file, execute the NF variable with the Awk command as follows:

Consider the below output:

AWK Command

Example6: Separate the output in the specified format.

To separate the output by a ‘-‘ symbol or (:) semicolon, specify it with ORS command as follows:

The above command will separate the output by an underscore (_) symbol. Consider the below output:

Example7: Print the square of the numbers from 1 to 8.

To print the numbers from 1 to 8, execute below command:

The above command will print the square of 1 to 8. consider the below output:

square of 1 is 1  square of 2 is 4  square of 3 is 9  square of 4 is 16  square of 5 is 25  square of 6 is 36  square of 7 is 49  square of 8 is 64  

Example8: Calculate the sum of a particular column.

Let’s create a data to apply the sum operation on a column. To create students marks data, execute the cat command as follows:

Press CTRL+D to save the file. We have successfully created StudentsMarks data. We can check it by executing the cat command as follows:

To calculate the third column of the created data, execute the below command:

Output:

600  

Consider the below output:

AWK Command

Example9: Find some of the individual records.

To print some of the individual student marks record, execute the below command:

The above command will print the individual’s name with his marks from the StudentMarks file. Consider the below output:

AWK Command

Example10: Find the value of exp 8.

To find the value of exp 8, execute the below command:

The above command will print the value of exp 8. consider the below output:

AWK Command


Next TopicLinux make command

You may also like