How to install and use 'AWK' on Windows

avatar
Borislav Hadzhiev

Last updated: Apr 4, 2024
4 min

banner

# How to install and use 'AWK' on Windows

To install awk on Windows if you have Chocolatey installed:

  1. Click on the Search bar and type PowerShell.

  2. Right-click on the PowerShell application and click "Run as administrator".

run powershell as administrator

  1. Run the following command to install awk.
PowerShell
choco install gawk -y
If you don't have Chocolatey installed, you have to install it first.

To install Chocolatey:

  1. Open PowerShell as an administrator.

run powershell as administrator

  1. Run the following command.
PowerShell
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))

windows install chocolatey

  1. Wait for the command to complete.
  2. Type choco to make sure Chocolatey is installed.

windows verify chocolatey installed

Now that you have Chocolatey installed, run the following command to install awk.

PowerShell
choco install gawk -y

Note that your shell should still be run using elevated permissions.

chocolatey install awk

Click on the Search bar, type "cmd" and start the Command Prompt application.

You can use the awk --version command to make sure awk is installed successfully.

Note, if you use CMD, wrap the parameters when issuing an awk command in double quotes. If you use PowerShell or Git Bash, wrap them in single quotes.

Create a file called example.txt with the following contents.

example.txt
Name Salary Experience Alice 100 5 Bobby 50 3 Carl 150 8

example awk file

You can run the following command to display the file's contents.

shell
awk "{print $0}" example.txt

The $0 field variable represents the entire record.

awk display file contents

You can display the number of records in the file with the NR built-in variable.

shell
awk "{print NR,$0}" example.txt

awk display number of records

To display only the first column, use the $1 field variable.

shell
awk "{print $1}" example.txt

awk display first column

You can display multiple columns, by separating the records with a comma.

shell
awk "{print $1, $2}" example.txt

awk display multiple columns

Alternatively, you can run the awk command in Git Bash by installing git on your Windows machine.

If you already have git installed, you can search for Git Bash and use the awk command, otherwise, you have to install git first.

# Use AWK on Windows via Git Bash

To download git and be able to use Git Bash:

  1. Open the git downloads page and download the installer for Windows.
  2. Start the installer.
  3. You will be prompted to select a destination location. You can leave the default option and click Next.

git select destination location

  1. You will be prompted to select components on the next screen. Leave the default options and click Next.

git select components

  1. Click Next on the screen that prompts you to "Select Start Menu Folder".

  2. On the next screen, you can choose the default editor for Git, e.g. Notepad, Notepad++ or any other editor you prefer.

choose git default editor

  1. On the "Adjust the name of the initial branch in new repositories screen", click Next.

adjust name of initial branch

  1. On the "Adjust your PATH environment" screen, make sure you have the default option of "Git from the command line and also from 3rd-party software" option selected and click "Next".

adjust your path environment

  1. For all the remaining screens, leave the default option selected and click Next.
  2. Lastly, click on the Install button to install git.

Once you have git installed, click on the Search field, type "Git Bash" and start the application.

search for git bash

Now you can use the awk command directly in Git Bash.

Note, if you use CMD, wrap the parameters when issuing an awk command in double quotes. If you use PowerShell or Git Bash, wrap them in single quotes.

Create a file called example.txt with the following contents.

example.txt
Name Salary Experience Alice 100 5 Bobby 50 3 Carl 150 8

example awk file

You can run the following command to display the file's contents.

GitBash
awk '{print $0}' example.txt

The $0 field variable represents the entire record.

git bash awk display file contents

You can display the number of records in the file with the NR built-in variable.

GitBash
awk '{print NR,$0}' example.txt

To display only the first column, use the $1 field variable.

GitBash
awk '{print $1}' example.txt

You can display multiple columns, by separating the records with a comma.

GitBash
awk '{print $1, $2}' example.txt

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.