Skip to content
Menu
DevSecOps Now!!!
  • About
  • Certifications
  • Contact
  • Courses
  • DevSecOps Consulting
  • DevSecOps Tools
  • Training
  • Tutorials
DevSecOps Now!!!

Top 20 Commands with explanation of “awk” command

Posted on February 1, 2025

Limited Time Offer!

For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!

Enroll Now

The awk command in Linux is a powerful text-processing tool used for pattern scanning, extracting, formatting, and manipulating text files, logs, CSVs, and structured data. It reads files line by line, splits them into fields, and performs actions on them.

Letโ€™s go deep into 20 powerful awk commands, explained in a humanized and detailed way.


1. Print the Entire File

awk '{print}' file.txt

โœ” How it works:

  • {print} tells awk to print every line.
  • This is equivalent to cat file.txt, but awk allows modifications.

2. Print Only Specific Columns

awk '{print $1, $3}' file.txt

โœ” How it works:

  • $1 โ†’ Represents the first column.
  • $3 โ†’ Represents the third column.
  • Example: If file.txt contains:
John 25 Developer
Sarah 30 Designer

Output:

John Developer
Sarah Designer
  • This is useful for CSV files and log analysis.

3. Print Lines Matching a Pattern

awk '/error/ {print}' log.txt

โœ” How it works:

  • Searches for “error” in log.txt and prints the matching lines.
  • Example:
INFO: Process started
ERROR: File not found
WARNING: Disk space low

Output:

ERROR: File not found

โœ” Alternative:
To match case-insensitively:

awk 'tolower($0) ~ /error/' log.txt

4. Print Lines Where a Column Matches a Value

awk '$2 == "30" {print $1}' file.txt

โœ” How it works:

  • If the second column is “30”, print the first column.

๐Ÿ“Œ Example Input (file.txt):

John 25 Developer
Sarah 30 Designer
Mike 30 Manager

Output:

Sarah
Mike

5. Print Only Lines Where a Number is Greater Than a Value

awk '$2 > 25' file.txt

โœ” How it works:

  • Prints lines where the second column is greater than 25.

6. Print Line Number Along with Each Line

awk '{print NR, $0}' file.txt

โœ” How it works:

  • NR โ†’ Stands for “Number of Record” (line number).

๐Ÿ“Œ Example:

1 John 25 Developer
2 Sarah 30 Designer
3 Mike 40 Engineer

7. Print Total Number of Lines

awk 'END {print NR}' file.txt

โœ” How it works:

  • END executes after processing all lines.
  • NR gives the last line number (total lines).

8. Calculate the Sum of a Column

awk '{sum += $2} END {print "Total:", sum}' file.txt

โœ” How it works:

  • Adds all values in column 2 and prints the total.

๐Ÿ“Œ Example Input:

John 25
Sarah 30
Mike 40

Output:

Total: 95

9. Calculate Average of a Column

awk '{sum += $2; count++} END {print "Average:", sum/count}' file.txt

โœ” How it works:

  • Sums up column 2 and divides by count.

10. Print Only Even-Numbered Lines

awk 'NR % 2 == 0' file.txt

โœ” How it works:

  • NR % 2 == 0 means print only even lines.

11. Print Only Odd-Numbered Lines

awk 'NR % 2 == 1' file.txt

โœ” How it works:

  • NR % 2 == 1 means print only odd lines.

12. Print Only the Last Column

awk '{print $NF}' file.txt

โœ” How it works:

  • $NF โ†’ Represents the last field in each line.

๐Ÿ“Œ Example Input:

John 25 Developer
Sarah 30 Designer
Mike 40 Engineer

Output:

Developer
Designer
Engineer

13. Print Only the First and Last Column

awk '{print $1, $NF}' file.txt

โœ” How it works:

  • Prints first and last column in each line.

14. Change Column Separator (FS)

awk -F',' '{print $1, $3}' file.csv

โœ” How it works:

  • -F',' sets , as the field separator for CSV files.

15. Replace a Word in a Column

awk '{gsub("old", "new", $2); print}' file.txt

โœ” How it works:

  • Replaces “old” with “new” only in column 2.

16. Extract Unique Values from a Column

awk '!seen[$2]++' file.txt

โœ” How it works:

  • Uses an associative array to filter duplicates from column 2.

17. Count Occurrences of Words

awk '{count[$1]++} END {for (word in count) print word, count[word]}' file.txt

โœ” How it works:

  • Counts occurrences of each unique word in column 1.

๐Ÿ“Œ Example Input:

Apple
Banana
Apple
Mango
Banana

Output:

Apple 2
Banana 2
Mango 1

18. Find the Longest Line

awk '{if (length > max) {max = length; line = $0}} END {print line}' file.txt

โœ” How it works:

  • Finds the longest line in a file.

19. Extract Lines Between Two Patterns

awk '/START/,/END/' file.txt

โœ” How it works:

  • Prints everything between “START” and “END”.

20. Execute Shell Commands in awk

awk 'BEGIN { system("date") }'

โœ” How it works:

  • Runs date before executing awk.

Final Thoughts

  • awk is an advanced text-processing tool, making it essential for system administrators, data analysts, and programmers.
  • These 20 commands cover real-world applications, including data extraction, calculations, filtering, and reporting.
Post Views: 1,441
  • awk
  • awk commands
  • Commands
  • Linux
  • linux command
  • Top 20 Commands
  • Top 20 Commands linux command
  • Top 20 Linux Commands
Subscribe
Login
Notify of
guest
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
  • Master the Machine Learning Lifecycle:Guide to Becoming a Certified MLOps Architect
  • How to Build a Project-Level AI Memory System That Works Across Codex, Claude, and Other AI Coding Tools
  • Certified MLOps Professional: A Deep Dive into the Certified MLOps Professional Certification
  • Certified MLOps Engineer : The Comprehensive Guide to Mastering Machine Learning Operations
  • Codex vs Claude: A Complete Practical Guide for Modern Developers (2026)
  • Certified AIOps Professional Program A Guide to Career Growth
  • Keycloak Multi-Client Architecture with Project-Based Email Validation (Student, Trainer, Company, Consulting)
  • Incorrect definition of table mysql.column_stats
  • Mautic and PHP 8.3 Compatibility Guide (2026)
  • Certified AIOps Engineer: The Complete Career Path and Certification Guide
  • How to Rename Apache Virtual Host Files Safely (Step-by-Step Guide for Linux)
  • AIOps Foundation Certification: Everything You Need to Know to Get Certified
  • DevOps to Certified Site Reliability Professional: A Senior Mentorโ€™s Guide
  • Certified Site Reliability Manager Training, Preparation, and Career Mapping
  • Certified Site Reliability Architect: The Complete Career Guide
  • What Is a VPN? A Complete Beginner-to-Advanced Tutorial
  • How to Install, Secure, and Tune MySQL 8.4 on Ubuntu 24.04 for Apache Event MPM and PHP-FPM
  • Complete Guide to Certified Site Reliability Engineer Career
  • Certified DevSecOps Professional Step by Step
  • Certified DevSecOps Manager: Complete Career Guide
  • Certified DevSecOps Engineer: Skills, Career Path and Certification Guide
  • Step-by-Step: Become a Certified DevSecOps Architect
  • Tuning PHP 8.3 for Apache Event MPM and PHP-FPM on Ubuntu: A Complete Step-by-Step Production Guide
  • Complete Step-by-Step Guide to Configure Apache Event MPM, Create index.php, Set Up VirtualHost, and Fix Ubuntu Default Page
  • Convert XAMPP Apache to Event MPM + System PHP-FPM
  • The Gateway to System Observability Engineering (MOE)
  • How to Finetune Apache and Prove It Works: A Real-World Guide to Testing Performance, Concurrency, HTTP/2, Memory, CPU, and Security
  • Building a High-Performance Apache Event MPM + PHP-FPM + MariaDB Stack (Advanced Server Optimization Guide)
  • Master Infrastructure as Code: The Complete Hashicorp Terraform Associate Guide
  • Building a High-Performance Apache Server with Event MPM + PHP-FPM (Step-by-Step Guide)

Recent Comments

  1. emmy day on SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘provider’ in ‘field list’
  2. digital banking on Complete Tutorial: Setting Up Laravel Telescope Correctly (Windows + XAMPP + Custom Domain)
  3. SAHIL DHINGRA on How to Uninstall Xampp from your machine when it is not visible in Control panel programs & Feature ?
  4. Abhishek on MySQL: List of Comprehensive List of approach to secure MySQL servers.
  5. Kristina on Best practices to followed in .httacess to avoid DDOS attack?

Archives

  • April 2026
  • March 2026
  • February 2026
  • January 2026
  • December 2025
  • November 2025
  • October 2025
  • September 2025
  • August 2025
  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • July 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022

Categories

  • Ai
  • AI Blogging
  • AiOps
  • ajax
  • Android Studio
  • Antimalware
  • Antivirus
  • Apache
  • Api
  • API Security
  • Api Testing
  • APK
  • Aws
  • Bike Rental Services
  • ChatGPT
  • Code Linting
  • Composer
  • cPanel
  • Cyber Threat Intelligence
  • Cybersecurity
  • Data Loss Prevention
  • Database
  • dataops
  • Deception Technology
  • DeepSeek
  • Devops
  • DevSecOps
  • DevTools
  • Digital Asset Management
  • Digital Certificates
  • Docker
  • Drupal
  • emulator
  • Encryption Tools
  • Endpoint Security Tools
  • Error
  • facebook
  • Firewalls
  • Flutter
  • git
  • GITHUB
  • Google Antigravity
  • Google play console
  • Google reCAPTCHA
  • Gradle
  • Guest posting
  • health and fitness
  • IDE
  • Identity and Access Management
  • Incident Response
  • Instagram
  • Intrusion Detection and Prevention Systems
  • jobs
  • Joomla
  • Keycloak
  • Laravel
  • Law News
  • Lawyer Discussion
  • Legal Advice
  • Linkedin
  • Linkedin Api
  • Linux
  • Livewire
  • Mautic
  • Medical Tourism
  • MlOps
  • MobaXterm
  • Mobile Device Management
  • Multi-Factor Authentication
  • MySql
  • Network Traffic Analysis tools
  • Paytm
  • Penetration Testing
  • php
  • PHPMyAdmin
  • Pinterest Api
  • Quora
  • SAST
  • SecOps
  • Secure File Transfer Protocol
  • Security Analytics Tools
  • Security Auditing Tools
  • Security Information and Event Management
  • Seo
  • Server Management Tools
  • Single Sign-On
  • Site Reliability Engineering
  • soft 404
  • software
  • SuiteCRM
  • SysOps
  • Threat Model
  • Twitter
  • Twitter Api
  • ubuntu
  • Uncategorized
  • Virtual Host
  • Virtual Private Networks
  • VPNs
  • Vulnerability Assessment Tools
  • Web Application Firewalls
  • Windows Processor
  • Wordpress
  • WSL (Windows Subsystem for Linux)
  • X.com
  • Xampp
  • Youtube
©2026 DevSecOps Now!!! | WordPress Theme: EcoCoded
wpDiscuz