articles

Home / DeveloperSection / Articles / COMPLETE OVERVIEW ON LINUX AWK COMMAND

COMPLETE OVERVIEW ON LINUX AWK COMMAND

COMPLETE OVERVIEW ON LINUX AWK COMMAND

HKR TRAININGS324 04-Apr-2023

The Linux AWK Command is a powerful text processing tool that allows you to manipulate and analyze text data in a variety of ways. The general syntax of the AWK command is:


 

arduino

Copy code

awk 'pattern {action}' filename

where:


 

pattern is a regular expression or a logical expression that matches certain lines in the input file

action is a series of commands that are executed on the lines that match the pattern

filename is the name of the input file

Here are some examples of how to use AWK:


 

Print the first field of each line in a file:

arduino

Copy code

awk '{print $1}' filename

Print the lines that contain a certain pattern:

arduino

Copy code

awk '/pattern/{print}' filename

Print the total number of lines in a file:

arduino

Copy code

awk 'END{print NR}' filename

Print the sum of values in the second column of a file:

arduino

Copy code

awk '{sum+=$2} END{print sum}' filename

These are just a few examples of what you can do with AWK. The possibilities are endless and you can use AWK to perform complex data processing tasks.


 

BASIC SYNTAX FOR AWK COMMAND:


 

The basic syntax for the awk command in Linux is:


 

arduino

Copy code

awk 'pattern {action}' filename

pattern: A pattern is a regular expression that specifies which lines to process. If the pattern matches the current line, then the associated action is executed.

{action}: This is a set of commands that are executed on the lines that match the pattern.

filename: This is the name of the file(s) to process.

Some common options that can be used with the awk command include:


 

-F: This option specifies the field separator. By default, awk treats whitespace as the field separator, but with -F, you can specify a different delimiter.

-v: This option allows you to set a variable value before running the awk script.

Here's an example of how to use the basic syntax of the awk command to print the first column of a file:


 

arduino

Copy code

awk '{print $1}' filename

In this example, the pattern is not specified, so the action will be applied to every line of the file. The action {print $1} tells awk to print the first field of each line. The field separator is whitespace, which is the default.


 

Here's another example that shows how to use a pattern with the awk command to print lines that contain a specific string:


 

arduino

Copy code

awk '/pattern/ {print}' filename

In this example, the pattern is /pattern/, which means that awk will only execute the action for lines that contain the string "pattern". The action is {print}, which tells awk to print the entire line.


 

These are just a few examples of how to use the awk command in Linux. Awk is a powerful tool that can be used to perform complex text processing tasks.


 


HKR Trainings excel at providing you the best online classes with high quality facilities at a low price without any compromise on quality. What can you expect from us? A dedicated learning platform with 24*7 support, best in class training materials to help you learn advance techniques and practical knowledge of all IT Technologies. VISIT:https://hkrtrainings.com/

Leave Comment

Comments

Liked By