What is Awk Command in Linux?

What is Awk Command in Linux?

4 mins read275 Views Comment
Updated on Feb 16, 2023 17:48 IST

Here we will understand the concept of Awk command in Linux. You will also find its features, AWK Script, along with examples.

2023_02_Awk-Command-in-Linux.jpg

Awk Command

Linux uses the awk command for text processing. Although the sed command is also used for text processing, it has significant drawbacks, making the awk command a useful alternative. It gives the data strong control. For text scripting, Awk is a powerful scripting language. It involves searching and replacing the texts and sorting, checking, and indexing the database. This article will discuss more awk commands in Linux.

You can also explore: Linux courses

Features of AWK command in Linux

The Awk command has the following features:

  • Line by line, a file is scanned.
  • A file is divided into various fields by it.
  • It contrasts the text that is input or a section of a text file.
  • It performs numerous operations on a file, such as searching for a particular text.
  • The lines of output are formatted.
  • It can operate on strings and numbers.
  • It applies the criteria and loops based on the results.
  • It modifies the data and files on a given structure.
  • It generates reports in the format.

Read Also: What is Operating Systems – Types, Function, and Example 

Syntax:

Here’s a demonstration of how to use the awk command:

 
awk options 'selection _criteria {action }' input-file > output-file
Copy code

Options include:

 -f program files: It examines the script’s source code, which was written using the awk tool.

-F fs: It serves as the field separator for the input.

Must Check: Operating Systems Online Courses and Certifications

How to Define AWK Script?

Use the awk command, curly braces, and a single quote mark to define the awk script. For example

 
awk '{ print "Welcome Awk command"}'
Copy code

Must read: Difference between Linux and Unix

AWK Command Examples

Look at the example below to learn more about the Awk command:

To apply the various awk operations, let’s establish a data set. Think about the student data from various streams.

Use the cat command as follows to generate data:

 
cat > studentlist.txt
Ava CS
Dani IT
John CS
Alex IT
Micheal ECE
Helena CS
Copy code

On the created student data, we will use the awk command.

Also check: Learn Basic Linux Commands (With Syntax)

Example 1: List the students who fit the required pattern.

Consider the below command:

 
awk '/ CS/ {print} ' studentlist.txt
Copy code

Example 2: Find the default behavior of the awk command

We will get the file’s entire contents if we don’t specify a pattern.

Have a look at the command below:

 
awk '{print}' studentlist.txt
Copy code

 Example 3: Print the specified column.

This command will only print that line if we supply the column number. Think about the result below.

 
awk '{print $1,$5} studentlist.txt
Copy code

Built-in Variables in AWK Command

Numerous built-in variables supported by the awk tool divide the file’s content into smaller chunks, including $1, $2, and others.

NR: It is utilized to display the number of lines as of the moment. For each line, the awk program executes an operation once. These are records.

NF: It is employed to determine the many fields in the current database.

FS: To divide fields into input lines, it is used to build a field separator character.

ORF: The output field separator is kept there for further use. The output fields are divided by this.

OFS: The output record separator is stored in the ORS. The output records are divided by this. It automatically prints the ORS command’s contents.

Suggested Read: Linux vs Windows

Example 4: Print the output and display the line number.

Use the NR variable in conjunction with the Awk command, as shown below, to display the line number in the output:

 
awk '{print NR,$0}' studentlist.txt
Copy code

Example 5: Print the last field of the file.

Use the NF variable along with the Awk command to display the last field of the file:

 
awk '{print $NF}' studentlist.txt
Copy code

Example 6: Separate the output in the specified format.

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

 
awk 'BEGIN { ORS ="-"} {print $0}' studentlist.txt
Copy code

We can use the underscore (_) symbol in the command above to denote output separation. Think about the result below:

 
Ava CS -Dani CS-John CS-Alex IT-Micheal ECE-
Copy code

Example 7: Print the square of the numbers from 1 to 8.

Execute the following command to print the numbers from 1 to 8:

 
awk 'BEGIN { for(i=1;i<=8;i++) print "square of", i, "is",i*i; }'
Copy code
How to list files in Linux?
How to list files in Linux?
In this article we will study about How to list files in Linux?And this is done Using ls Command with flags.
How to Delete a File in Linux?
How to Delete a File in Linux?
Need to clear some space on your Linux system? It’s easier than you might think! With a simple command, you can swiftly delete any file. But what’s the right way...read more
What is Grep Command in Linux?
What is Grep Command in Linux?
Here you will understand the Grep command in Linux. We have also explained options description and sample commands. Let’s check out.

Example 8: Calculate the sum of a particular column.

Create a data set and then apply the sum operation to a column. Use the cat command to create student marks data as follows:

 
naukrilearning@ naukrilearning -GB-BXBT-2807:~$ cat > student_marks
Name, Marks, Max marks
Ava,75,100
Dani,80,100
John,74,100
Alex,85,100
Micheal,70,100
Helena,74,100
Copy code

To save the file, press CTRL+D. The data for student_marks has been produced successfully. By using the cat command as follows, we can verify it:

 
cat student_marks
Copy code

Execute the following command to calculate the third column of the generated data:

 
awk -F"," '{x+=$3}END{print x}' student_marks
Copy code

Example 9: Find some of the individual records.

Execute the following command to print some of the individual student marks records:

 
wk -F, '{a[$1]+=$2;}END{for(i in a)print i", "a[i];}' student_marks
Copy code

Example 10: Find the value of exp 8.

Execute the following command to find the value of exp 8:

 
awk 'BEGIN{x=exp(8); print x}'
Copy code

Also Read: Top Linux Interview Questions and Answers

Conclusion

Companies can employee Unix AWK Command Shell scripting for sophisticated text processing. We can typically use Awk Command in Linux as a reporting tool and for pattern matching. Awk can perform a set of operations on the input file.

Contributed By: Megha Chadha

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio