Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST TOOLS

Bash Tutorial

Bash HOME Bash Intro Bash Get Started

Basic Commands

Bash Commands Bash List (ls) Bash Change Dir (cd) Bash Print Dir (pwd) Bash Echo (echo) Bash Concatenate (cat) Bash Copy (cp) Bash Move (mv) Bash Remove (rm) Bash Timestamp (touch) Bash Make Dir (mkdir) Bash Find Files (find) Bash Manual (man) Bash Alias

Pipes and Redirection

Bash Redirection Bash Pipes (|) Bash Build Commands (xargs)

Text Processing

Bash Search Text (grep) Bash Pattern Scan (awk) Bash Stream Editor (sed) Bash Remove Section (cut) Bash Sort Lines (sort) Bash View End (tail) Bash View Start (head) Bash Count (wc) Bash Unique Lines (uniq) Bash Translate (tr) Bash Compare Files (diff)

System Monitoring

Bash Process Status (ps) Bash List Processes (top) Bash Disk Space (df) Bash Directory Usage (du) Bash Memory Usage (free) Bash Terminate (kill) Bash Uptime

Networking

Bash Ping Bash URL Transfer (curl) Bash Downloader (wget) Bash Remote Connect (ssh) Bash Secure Copy (scp) Bash File Sync (rsync)

File Compression

Bash Compress (zip) Bash Extract (unzip) Bash TAR Archive

File Permissions

Bash Ownership Bash Modify (chmod) Bash Ownership (chown) Bash Group (chgrp)

Scripting

Bash Syntax Bash Script Bash Variables Bash Data Types Bash Operators Bash If...Else Bash Case Bash Loops Bash Functions Bash Script Arguments Bash Read Input (read) Bash Exit Codes Bash Arrays Bash Strings Bash Here Documents Bash Trap Signals (trap) Bash Schedule (cron)

Bash Cert

Bash Certificate

Bash Reference

Bash Glossary

Exercises and Quiz

Bash Compiler Bash Exercises Bash Challenges Bash Quiz

Bash awk - Pattern Scanning and Processing Language


Share

Using the awk Command

awk is a pattern scanning and processing language.

It's useful for handling text files and used for data extraction and reporting.


Basic Usage

The awk command is powerful for text processing.

For example, you can use it to extract specific fields from a file or perform calculations.

All examples below use the example_data.csv file:

id,Created,Amount,Currency,Description,Customer
1,2024-11-01,100,USD,Payment,John Doe
2,2024-11-02,200,EUR,Refund,Jane Smith
3,2024-11-03,150,USD,Purchase,Emily Davis
4,2024-11-04,175,GBP,Subscription,Michael Brown

To print the first column of a file, use awk -F"," '{print $1}' filename:

Example: Print First Column

awk -F"," '{print $1}' example_data.csv
id
1
2
3
4
Try it Yourself »


Options

The awk command has options to change how it works:

  • -F - Set what separates the data fields
  • -v - Set a variable to be used in the script
  • -f - Use a file as the source of the awk program

Field Separator

The -F option allows you to define the field separator for processing data.

This is useful when dealing with CSV files or data with specific delimiters.

Example: Field Separator

echo "2024-11-01" | awk -F"-" '{print $3 "/" $2 "/" $1}'
01/11/2024
Try it Yourself »

Assign Variable

The -v option lets you assign a variable to be used within the awk script.

This is helpful for passing external values into the script.

Example: Assign Variable

awk -F"," -v var="Amount:" '{print var, $3}' example_data.csv
Amount: Amount
Amount: 100
Amount: 200
Amount: 150
Amount: 175
Try it Yourself »

Using awk for Data Manipulation

Awk can perform complex data manipulation tasks.

For example, awk -F"," '{sum += $3} END {print sum}' example_data.csv calculates the sum of the Amount column.

Example: Data Manipulation

awk -F"," '{sum += $3} END {print sum}' example_data.csv
625
Try it Yourself »

Common Errors and Troubleshooting

When using awk, you might encounter errors such as:

  • "awk: syntax error" - Check your command syntax.
  • "awk: cannot open file" - Ensure the file path is correct and accessible.

Debugging tips include using print statements to check variable values and logic flow.



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.