Usage of awk in Linux

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

awk has some set of Rules, Patterns, and Actions that we need to follow.

awk works on programs that contain rules comprised of patterns and actions. The action is executed on the text that matches the pattern. Patterns are enclosed in curly braces ({}). Together, a pattern and an action form a rule. The entire awk program is enclosed in single quotes (').

In the above example when we run the ls -la, basically it will list all the content from the current directory. Now when we want to display the customized output rather than the whole output we can use the awk command with the pattern.

$0: Represents the entire line of text.

$1: Represents the first field.

$2: Represents the second field and so on we can put numbers.

$NF: Stands for “number of fields" and end of the file.

$NR - it will add the number to the given output. Like 1, 2, and n, etc.

Please do share your feedback.

Rahul