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,570
  • 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
  • Laravel Posts Installation Guide
  • Strategies to Align DevSecOps With Agile and DevOps Practices
  • How to Fix Laravel Migration Error: Field โ€˜idโ€™ Doesnโ€™t Have a Default Value in the Migrations Table
  • A Practical Guide to Proving DevSecOps Business Value for Engineering Leaders
  • Mastering Secure Software Delivery by Solving DevSecOps Adoption Challenges
  • Operationalizing Security for Faster and Safer Software Deployments
  • DevSecOps Server Security Checklist 2026: 50 Must-Check Points Before Going Live
  • The Complete DevOps Salary Overview for IT Professionals
  • The Modern DevOps Certification Guide: Roadmaps for Every Engineering Role
  • Security Champions in DevSecOps: Responsibilities and Best Practices
  • The DevSecOps Handbook for Shift-Left Security
  • Top DevSecOps Principles for Effective Secure Software Delivery
  • Guide to DevSecOps Maturity Levels for Platform and Security Teams
  • Canada PR CRS Calculator: Express Entry Points System Explained
  • Austria PR Points Calculator: Ultimate Guide to Navigating the Red-White-Red Card System
  • The Essential Guide to Enterprise DevSecOps Implementation
  • How to Set Up Claude Code Agent on a Local Windows Laptop and Use claude Command from Anywhere
  • DevOps and DevSecOps Explained: Bridging the Gap Between Speed and Security
  • Comprehensive Manual on DevOps Methodologies and Cloud Native Engineering
  • The Master Guide to Immigration Points: Calculating Your Path to Canada, Australia, and Beyond
  • How to Skip the Activation Email and Password Reset After Google Login in Keycloak Auto-Link Existing Users in First Broker Login
  • Free SSL Certificate Generation Tutorial for Any Website Using Certbot and Apache
  • The Ultimate Guide to Certified FinOps Professional: Skills, Levels, and Career Impact
  • Certified FinOps Manager: Essential Skills for Modern Cloud Operations
  • How to Use Claude AI for Programming: Complete Guide for Developers to Boost Productivity
  • The Definitive Guide to Certified FinOps Engineer: Master Cloud Value Engineering
  • A Comprehensive Guide to the Certified FinOps Architect Certification and Training
  • Linux Server Diagnostic Commands: Complete Guide for Performance, Network & System Troubleshooting
  • The Ultimate Guide to CDOM โ€“ Certified DataOps Manager Certification
  • The Practical Path to AI Reliability: A Guide to the Certified MLOps Manager

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

  • June 2026
  • May 2026
  • 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
  • postify
  • 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
  • SSL
  • 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