• Skip to main content
  • Skip to primary sidebar
  • Home
  • WordPress
  • web Hosting
  • linux
  • mysql
  • nginx
  • apache2
  • devops

Raju Ginni

wordpress tutorials seo hosting etc

You are here: Home / Linux sysadmin tutorials linux system administrator / awk commands with practical examples

awk commands with practical examples

AWK is a powerful text-processing tool in Linux and Unix. It is used for pattern scanning and processing. Below are some commonly used awk commands with practical examples.

Table of Contents

Toggle
  • 1. What is AWK?
  • 2. Basic Syntax
  • 3. Print a File’s Content
  • 4. Print Specific Columns
  • 5. Print Lines Matching a Pattern
  • 6. Print Lines Based on a Condition
  • 7. Count the Number of Lines
  • 8. Calculate Sum of a Column
  • 9. Use a Custom Field Separator
  • 10. Print Only Even Rows
  • 11. Find Maximum Salary
  • 12. AWK with Piping
  • 13. Replace a Column Value
  • 1. Print Specific Columns from a File
  • 2. Print Lines Matching a Pattern
  • 3. Print Lines with a Condition (Salary > 5000)
  • 4. Print the Sum of a Column
  • 5. Count the Number of Lines
  • 6. Use Field Separator (CSV File)
  • 7. Replace a Column Value
  • 8. Print Only Even Rows
  • 9. Find the Maximum Salary
  • 10. AWK in a Pipeline

1. What is AWK?

  • AWK is a scripting language designed for text processing and pattern matching.
  • It processes structured data such as CSV, logs, and space/tab-separated files.
  • AWK reads a file line by line, splits each line into fields, and applies specified operations.

2. Basic Syntax

The basic syntax of an awk command is:

bash
awk 'pattern { action }' filename
  • pattern – Specifies when to apply the action.
  • action – Specifies what to do (print, calculate, modify, etc.).
  • filename – The input file.

3. Print a File’s Content

bash
awk '{print}' employees.txt
  • This prints every line of employees.txt, similar to cat employees.txt.

4. Print Specific Columns

bash
awk '{print $1, $3}' employees.txt
  • $1, $2, $3, etc., represent columns (fields).
  • Example (employees.txt file):
    yaml
    1 John 5000
    2 Alice 6000
    3 Bob 7000
  • Output:
    yaml
    1 5000
    2 6000
    3 7000

5. Print Lines Matching a Pattern

bash
awk '/Alice/ {print}' employees.txt
  • Finds and prints lines containing “Alice”.

6. Print Lines Based on a Condition

bash
awk '$3 > 5000 {print $1, $2}' employees.txt
  • Prints employees with a salary greater than 5000.
  • Output:
    2 Alice
    3 Bob

7. Count the Number of Lines

bash
awk 'END {print NR}' employees.txt
  • NR (Number of Records) holds the count of processed lines.
  • Output:
    3

8. Calculate Sum of a Column

bash
awk '{sum += $3} END {print "Total Salary:", sum}' employees.txt
  • Adds up all values in the third column.
  • Output:
    yaml
    Total Salary: 18000

9. Use a Custom Field Separator

For a CSV file (employees.csv):

1,John,5000
2,Alice,6000
3,Bob,7000

Use -F to define the field separator:

bash
awk -F ',' '{print $1, $3}' employees.csv
  • Output:
    yaml
    1 5000
    2 6000
    3 7000

10. Print Only Even Rows

bash
awk 'NR%2==0' employees.txt
  • Prints only even-numbered lines.

11. Find Maximum Salary

bash
awk 'max < $3 {max=$3} END {print "Max Salary:", max}' employees.txt
  • Finds the highest salary.

12. AWK with Piping

bash
ls -l | awk '{print $9, $5}'
  • Displays filenames and their sizes.

13. Replace a Column Value

bash
awk '{$2="David"; print}' employees.txt
  • Replaces the second column with “David”.


1. Print Specific Columns from a File

Command:

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

Explanation:
Prints the first and third columns of file.txt.
Example:

yaml
$ cat employees.txt
1 John 5000
2 Alice 6000
3 Bob 7000

$ awk '{print $1, $3}' employees.txt
1 5000
2 6000
3 7000


2. Print Lines Matching a Pattern

Command:

bash
awk '/Alice/ {print}' employees.txt

Explanation:
Prints lines containing the word “Alice”.
Example Output:

yaml
2 Alice 6000

3. Print Lines with a Condition (Salary > 5000)

Command:

bash
awk '$3 > 5000 {print $1, $2}' employees.txt

Explanation:
Prints employees with salaries greater than 5000. Example Output:

2 Alice
3 Bob

4. Print the Sum of a Column

Command:

bash
awk '{sum += $3} END {print "Total Salary:", sum}' employees.txt

Explanation:
Calculates and prints the sum of salaries.
Example Output:

yaml
Total Salary: 18000

5. Count the Number of Lines

Command:

bash
awk 'END {print NR}' employees.txt

Explanation:
Prints the total number of lines in the file.
Example Output:

3

6. Use Field Separator (CSV File)

Command:

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

Explanation:
Uses a comma , as the field separator.

Example (employees.csv):

1,John,5000
2,Alice,6000
3,Bob,7000

Output:

yaml
1 5000
2 6000
3 7000

7. Replace a Column Value

Command:

bash
awk '{$2="David"; print}' employees.txt

Explanation:
Replaces the second column with “David”.


8. Print Only Even Rows

Command:

bash
awk 'NR%2==0' employees.txt

Explanation:
Prints only even-numbered lines.


9. Find the Maximum Salary

Command:

bash
awk 'max < $3 {max=$3} END {print "Max Salary:", max}' employees.txt

Explanation:
Finds the highest salary in the third column.


10. AWK in a Pipeline

Command:

bash
ls -l | awk '{print $9, $5}'

Explanation:
Lists file names and their sizes.


These are just some basic examples. AWK is very powerful for text processing, and you can use it to manipulate, filter, and analyze text files efficiently.

Primary Sidebar

Linux sysadmin tutorials linux system administrator

  • top 10 apt & apt-get commands (most used) apt vs apt-get
  • If-Else Statements in Shell Scripting
  • linux commands pdf (files & Directories, zip & unzip process, search etc)
  • Find Files with Specific Text on Linux grep find command
  • linux performance tuning inode limit file descriptors tco, kernel etc
  • Variables and Data Types in Shell Scripting
  • Top 10 most used Cat commands with examples (create, view, append files)
  • Ip tables / ufw / firewall d commands for block port ip rate limiting
  • Top 10 zip / tar commands to compress & extract files in linux
  • TOP 10 mv & cp commands in linux to move & copy files in Linux
  • Top 10 GREP Commands in linux to search files directory words strings
  • lsof netstat commands to know listening ports in linux 3 ways
  • Upgrade Ubuntu from 18.04 (19.10) to 20.04 LTS command line or gui server | desktop
  • 3 Ways (SCP, rsync, Sftp) linux server migration between two remote server apache nginx
  • linux system specs commands (CPU, Memory, Disk )speed, type. manufacture
  • linux sysctl command tweaks & hardening
  • linux security limits.conf deciding user limits process limits for nginx server
  • ulimit linux unlimited command unlimto set & know user limits open files file descriptor max user process etc.
  • red hat linux certification cost jobs salary syllabus courses fees
  • ufw firewall commads allow port enable disable ubuntu 20.04
  • ddos attack prevention
  • change ssh port in linux - avoid sshd ddos attacks
  • ping command
  • memcached install ubuntu wordpress
  • check linux version (lsb_release -a) ubuntu debian 32 or 64 bit
  • rsync command linux with examples comparison to scp
  • how to uninstall package in linux ubuntu rpm, yum apt-get
  • increase open file limit linux File descriptor ft nginx , mysql, lemp
  • remove repository ubuntu
  • htop commad memory details virtual vs shard vs resident
  • chown command in Linux with Examples
  • Kill PHP process
  • VIrtual Memory vs RSS Memory vs Shared memory in Linux
  • oom killer fixing it by configuration linux ubuntu
  • Install Lemp nginx mysql php fpm Stack on Debian 11 with repository
  • connect two remote servers linux command line
  • auto start after oom killer Mysql & php fpm nginx etc ubuntu wth systemd or cron job
  • load average Linux 1, 5, 15 min 2,4,8 cores explained
  • Control Structures in Shell Scripting
  • Shell Scripting Roadmap for Beginners to Advanced
  • awk commands with practical examples
  • Shell Scripting Tutorial for Beginners 🚀
  • find Command in Linux with Examples
  • sed Command in Linux with Examples (Beginner to Advanced)
  • Linux Text processing commands in with Examples
  • linux disk management commands
  • fdisk command in linux with examples
  • how to add a new disk in linux
  • Linux mount Command with Examples
  • fstab options with examples
  • Top 50 Shell Scripting Interview Questions and Answers
  • Linux Networking Interview Questions and Answers
  • Linux Networking Commands Cheat Sheet with Examples pdf
  • Netstat & SS Commands cheat sheet with examples Interview Questions
  • Nmap Cheat Sheet – Network Scanning & Security
  • Bash Brackets ([], (), {}, $( ), $(( ))) – Types, Uses & Examples

hi i am raju ginni, primalry i manage wordpress websites on GCP cloud platform as a cloud engineer, and create content on passionate things.
you can follow me on youtbe

© 2025 - All Rights Reserved Disclaimer & Privacy Policy