In this blog, you will learn one of the most important commands i.e. awk command. It is mostly used for arranging the output in a readable format which you can easily analyze the data. So let’s get started with the blog.
Introduction to AWK Command
"awk
“ command is a powerful text processing tool in Linux and Unix-like operating systems. It allows you to process and manipulate text files based on patterns and actions defined in a program.
The “awk” command is a tool for reporting and data extraction that was created as a domain-specific
language for text processing.
It is a data-driven scripting language made comprised of several operations to be performed on textual data streams, either directly on files or as part of a pipeline, with the goal of extracting and converting data to produce prepared reports.
Syntax of Awk Command
$ awk options 'pattern { action }' file
Let’s break down the components:
options
(optional): You can specify various options to customize the behavior of theawk
commands, such as field separators or output formatting.pattern
(optional): It defines a pattern that specifies which lines or records to match. If omitted, the action block will be executed for all input lines.action
: This is a set of commands enclosed in curly braces executed for each line or record that matches the pattern. If a pattern is not provided, the action block is executed for every line.file
: Specifies the input file(s) to be processed. If no file is provided,awk
it reads from standard input (e.g., data piped from another command).
Full Guide for AWK Command
Let’s see our data.txt file and what content we have stored in the file.
$ cat data.txt
ID Name Position Salary
1 Bob HR 1000
2 White Developer 2000
3 Gill Tester 3000
4 Lara Tester 4000
5 Sam Developer 5000
A) Print the specific column of the data.
$ awk '{print $2}' data.txt
Name
Bob
White
Gill
Lara
Sam
From the above command, we can clearly see that we want to display the second column i.e. Name column, that’s why we have to use $2 in the command.
B) Print multiple columns of the data
$ awk '{print $2,$4}' data.txt
Name Salary
Bob 1000
White 2000
Gill 3000
Lara 4000
Sam 5000
From the above command, we can clearly see that we want to display multiple columns i.e. 2 and $4, that’s why we have to use $2 and $4 in the command.
C) Print the last column of the data
$ awk '{print $NF}' data.txt
Salary
1000
2000
3000
4000
5000
From the above command, we can clearly see that we have successfully displayed the last column of the data file.
D) Print Specific Columns with Delimeter
$ awk '{print NR ": ", $2}' data.txt
1: Name
2: Bob
3: White
4: Gill
5: Lara
6: Sam
From the above command, we can clearly see that we have successfully displayed the data in the proper format addition with the colon(:).
E) Remove the delimiter of the data and display the data.
$ cat info.txt
ID,employee_name,salary
1,sam,3000
2,copper,4000
3,travis,6000
From the above output, we can clear see theoutput of the data but the problem is that it includes the comma which is not easy to understand by the user.
$ awk -F, '{print $2,$3}' info.txt
employee_name salary
sam 3000
copper 4000
travis 6000
From the above output, we can clear see theoutput of the data in which we clearly see that comma is being remove with the option -F with the comma operator.
F) Condition Based Output with Awk Command
$ cat data.txt
ID Name Position Salary
1 Bob HR 1000
2 White Developer 2000
3 Gill Tester 3000
4 Lara Tester 4000
5 Sam Developer 5000
$ awk '{if($4 > 3000) print $0}' data.txt
ID Name Position Salary
4 Lara Tester 4000
5 Sam Developer 5000
From the above output, we can conclude that the display output have salary greater than 3000, we have achieve that with the help of if condition in the awk command.
F) Update the data with awk Command
$ cat data.txt
ID Name Position Salary
1 Bob HR 1000
2 White Developer 2000
3 Gill Tester 3000
4 Lara Tester 4000
5 Sam Developer 5000
$ awk '{if($2=="Bob") {($4=8000)} print $0}' data.txt
ID Name Position Salary
1 Bob HR 8000
2 White Developer 2000
3 Gill Tester 3000
4 Lara Tester 4000
5 Sam Developer 5000
From the above output, we can conclude that we have update the data of Bob with the salary of 8000.
G) Search and print the specific record
$ cat data.txt
ID Name Position Salary
1 Bob HR 1000
2 White Developer 2000
3 Gill Tester 3000
4 Lara Tester 4000
5 Sam Developer 5000
$ awk '/Tester/ {print $0}' data.txt
3 Gill Tester 3000
4 Lara Tester 4000
From the above output, we can conclude that we have search the term “Tester” and print that records.
H) Print the specific line of the record
$ awk 'NR=="2"{print $0}' data.txt
1 Bob HR 1000
From the above output, we have display only the second record.
I) Print the specific range of the line of the record
$ awk 'NR=="2",NR=="4" {print $0}' data.txt
1 Bob HR 1000
2 White Developer 2000
3 Gill Tester 3000
From the above output, we have display record from range 2 to 4 that means 3 records.
J) Print the number of lines in the record
$ awk 'END {print NR}' data.txt
6
K) For Loop in awk command
$ awk 'BEGIN {for(i=0;i<10;i++) print i;}'
0
1
2
3
4
5
6
7
8
9
L) While loop in awk command
$ awk 'BEGIN {while(i<10) {i++; print "num is " i}}'
num is 1
num is 2
num is 3
num is 4
num is 5
num is 6
num is 7
num is 8
num is 9
num is 10
Recent Articles on Linux
- What is Linux Operating System | Introduction to Linux
- Directory in Linux Define | Linux Directory & its Commands
- Explain the chmod command in Linux | Linux chmod command
- Linux User Management || User Management in Linux
- Linux Computer Network Advanced Command | Network Command
- Redirection in Linux I/O| Linux I/O Redirection
- CronTab and Job Scheduling in Linux | Make CronTab Project
- Linux Firewall Unlock Rules with Firewall-cmd Tutorial
- netstat command in Linux | Linux netstat command
- SSH Command Full Guide with Practical | Linux SSH Service
Related Articles to Cybersecurity Tools
- Dirb Command Kali Linux | Dirb: A Web-Content Scanner
- Introduction to Burp Suite | How to Download Burp Suite in Linux
- What is Tmux? | Introduction to Tmux
- Introduction to Termux | Termux Introduction
- EyeZy: How to log in to other Emails without receiving a Notification.
- Nmap Scanning Tool in Cyber Security with Nmap Cheatsheet
- WPScan Full Tutorial in 10 minutes| How to scan with WPScan
- Modules and Components of Metasploit Framework
- Data Packet Capture and Filters in WireShark
- Tshark: An Alternative for WireShark and How to use it
- SqlMap command in CyberSecurity | SQL Injection Attack Tool
- Hydra Tool Full Guide | Learn Hydra Command Tutorial
- John the Ripper Tool | How to crack the Password of Files
- Nikto Tool Web Vulnerability Scanner That Every Hacker Uses