AWK command in Linux

Learn AWK command in Linux with examples

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.

Awk is a general-purpose scripting language designed for advanced text processing. It is mostly used as a reporting and analysis tool.

Unlike most other programming languages that are procedural, awk is data-driven, which means that you define a set of actions to be performed against the input text. It takes the input data, transforms it, and sends the result to standard output.

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.

AWK is suitable for pattern search and processing. The script runs to search one or more files to identify matching patterns and if the said patterns perform specific tasks. In this guide, we take a look into AWK Linux command and see what it can do.

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.

AWK Command Syntax:

$ awk options ‘selection _criteria {action }’ input-file > output-file

Awk has a number of built-in variables that contain useful information and allows you to control how the program is processed. Below are some of the most common built-in Variables:

 

NF – The number of fields in the record.

NR – The number of the current record.

FILENAME – The name of the input file that is currently processed.

FS – Field separator.

RS – Record separator.

OFS – Output field separator.

ORS – Output record separator.

 

AWK Command Examples:

In the examples below, we will use a file named “teams.txt” that looks like the one below:

Name Span Runs Average
SR Tendulkar 1989-2013 34357 48.52
R Dravid 1996-2012 24064 45.57
V Kohli 2008-2022 23409 54.69
SC Ganguly 1992-2008 18433 41.42
MS Dhoni 2004-2019 17092 44.74

Awk Patterns

Patterns in awk control whether the associated action should be executed or not.

Awk supports different types of patterns, including regular expression, relation expression, range, and special expression patterns.

When the rule has no pattern, each input record is matched. Here is an example of a rule containing only an action:

Example 1: awk ‘{ print $3 }’ teams.txt

The program will print the third field of each record:

Runs

34357

24064

23409

18433

17092

Example 2: awk ‘{ print $1, $3, $4 }’ teams.txt

Name Runs Average

SR Tendulkar 34357 48.52

R Dravid   24064 45.57

V Kohli 23409 54.69

SC Ganguly 18433 41.42

MS Dhoni 17092 44.74

 

For more such weblogic issues and resolution click here.

Latest Weblogic Interview Questions and Answers

How to Prevent the ‘WLS_DIAGNOSTICSxxxxxx.DAT’ file from Growing Too Large

ThreadDump and How to Analyse it?

AWK command in Linux with examples

Apache Proxy log Throws READ_ERROR_FROM_FILE

ORA-01555: snapshot too old: rollback segment number with name “” too small

Weblogic Issue-Caused By: weblogic.utils.ErrorCollectionException

Server subsystem failed. Reason: java.lang.NumberFormatException: null

Cloud Computing Interview Questions And Answers

Frequently Asked CSS Interview Questions And Answers

[UPDATED] Django Interview Questions And Answers

HTTP Error codes and it’s meanings

 

Happy Learning !!