Last updated: Apr 4, 2024
Reading time·4 min
To install awk
on Windows if you have Chocolatey installed:
Click on the Search bar and type PowerShell.
Right-click on the PowerShell application and click "Run as administrator".
awk
.choco install gawk -y
To install Chocolatey:
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'))
choco
to make sure
Chocolatey
is installed.Now that you have Chocolatey installed, run the following command to install
awk
.
choco install gawk -y
Note that your shell should still be run using elevated permissions.
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.
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.
Name Salary Experience Alice 100 5 Bobby 50 3 Carl 150 8
You can run the following command to display the file's contents.
awk "{print $0}" example.txt
The $0
field variable represents the entire record.
You can display the number of records in the file with the NR
built-in
variable.
awk "{print NR,$0}" example.txt
To display only the first column, use the $1
field variable.
awk "{print $1}" example.txt
You can display multiple columns, by separating the records with a comma.
awk "{print $1, $2}" example.txt
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.
To download git
and be able to use Git Bash:
Next
.Next
.Click Next
on the screen that prompts you to "Select Start Menu Folder".
On the next screen, you can choose the default editor for Git
, e.g.
Notepad
, Notepad++
or any other editor you prefer.
Next
.Next
.Install
button to install git
.Once you have git
installed, click on the Search field, type "Git Bash"
and start the application.
Now you can use the awk
command directly in Git Bash.
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.
Name Salary Experience Alice 100 5 Bobby 50 3 Carl 150 8
You can run the following command to display the file's contents.
awk '{print $0}' example.txt
The $0
field variable represents the entire record.
You can display the number of records in the file with the NR
built-in
variable.
awk '{print NR,$0}' example.txt
To display only the first column, use the $1
field variable.
awk '{print $1}' example.txt
You can display multiple columns, by separating the records with a comma.
awk '{print $1, $2}' example.txt
You can learn more about the related topics by checking out the following tutorials: