Are you a Windows user who would like to take advantage of all the powerful features of Linux AWK? If so, you’ll be happy to know that it’s possible to install Linux AWK for Windows.
In this tutorial, you will learn how to do just that. You will go over the different methods for installing Linux AWK for Windows. After following the instructions in this tutorial, you can use AWK’s features from your Windows computer.
Read on to get started!
Prerequisites
This tutorial will be a hands-on demonstration. If you’d like to follow along, be sure you have a Windows computer handy. This tutorial uses Windows 10.
Installing AWK For Windows Using Installer
One way to install Linux AWK for Windows is by using an installer. Installers are usually the easiest way to get started with new software for typical users. The installer handles all of the necessary file placement and configuration for you.
Luckily, AWK for Windows comes with an installer.
Downloading the AWK for Windows Installer
1. Launch your browser and open the Gawk for Windows URL.
2. Scroll down to the Download section and click the Setup link next to Complete package, except sources.
3. Save the file to a convenient location. This example uses ~\Downloads\Programs as the download location.
Verifying the Installer File Integrity
To ensure you downloaded the correct file or that the file is intact, you must compare the MD5 sum value of the file against the one shown on the download page. This step is optional but recommended for security.
1. First, copy the MD5sum value from the download page.
2. Next, open PowerShell as admin and change the working directory to where you downloaded the AWK for Windows installer.
cd ~\Downloads\Programs
3. Run the Get-FileHash cmdlet to generate the file’s MD5 checksum and compare the hash value with the value you copied from the website. In this example, the Md5sum value from the is 1fdd86c1d73496817588f12a2a2e3a43.
(Get-FileHash -Path .\gawk-3.1.6-1-setup.exe -Algorithm MD5).Hash `
-eq '1fdd86c1d73496817588f12a2a2e3a43'
If the MD5 hash comparison result is True, the installer file is correct and not corrupted. Otherwise, you must re-download the file.
Installing AWK For Windows Using Installer
Now that you have verified that the file is safe, it’s time to install AWS for Windows on your computer.
1. Double-click the installer file from File Explorer or run the executable file in PowerShell.
.\gawk-3.1.6-1-setup.exe
2. Click Next on the Welcome to the Gawk Setup Wizard page.
3. On the License Agreement, click I accept the agreement and click Next.
4. On the next page, leave the destination path and click Next.
5. On the Select Components page, leave all options checked and click Next.
6. The next screen will ask you to select additional tasks. For this tutorial, leave the defaults and click Next.
7. Finally, click Install to begin the installation process.
8. Once the installation is complete, click Finish to close the installer. You’ve now installed AWS for Windows.
Configuring the PATH Variable
The final step is to configure your PATH environment variable so that you can run the awk command from any working directory. Otherwise, you can only run awk from its location (C:\Program Files (x86)\GnuWin32\bin\).
While still in PowerShell, follow the below instructions to add the awk
binary path to the PATH environment variable.
1. For good measure, run the below command to back up the current PATH to a text file.
[Environment]::GetEnvironmentVariable('Path') | Out-File origPath.txt
2. Get the current PATH variable value and append C:\Program Files (x86)\GnuWin32\bin\.
$newPath = [Environment]::GetEnvironmentVariable('Path')+";C:\Program Files (x86)\GnuWin32\bin\"
3. Now, save the new PATH.
[Environment]::SetEnvironmentVariable('Path',$newPath,'Machine')
4. Close and reopen PowerShell to verify the changes. You can now run awk from any directory on your computer. For example, run the below command to check the version of AWK for Windows.
awk --version
Installing AWK For Windows Using Chocolatey
Chocolatey is a command line package manager for Windows. Chocolatey helps you automate software installs. With this method, you always get the latest version of awk
.
1. Since Chocolatey does not ship with Windows out of the box, run the below command in an elevated PowerShell session 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'))
Wait for the installation to finish, which could last a few minutes.
2. Restart the PowerShell session and run the below command to install awk using Chocolatey.
choco install gawk
3. Finally, verify the installation by running the below command.
awk --version
Using AWK for Windows (Examples)
Now that you have installed awk, you can perform some of the basic things it can do. You can use awk
to extract information from a file or stream and print it to the screen.
Extracting Date Elements
For example, the date
command prints the current date to the terminal in the following format.
Suppose you need to extract the month and year. First, break down the output into fields.
Field Number | Value |
1 | 06 |
2 | September |
3 | 2022 |
4 | 23:43:38 |
In this case, you’ll extract fields 2 and 3. To do so, run the date command and pipe its output to the awk command. The ‘{print $2,$3}’ part means to print the value of fields 2 and 3. Each field starts with the $ followed by the field number.
date | awk '{print $2,$3}'
As you can see below, only the fields you specified in the awk
command return.
Specifying a Custom Field Separator
You can append the OFS (output field separator) variable to specify what character to use to concatenate the output.
date | awk '{print $2,$3}' OFS="-"
Calculating Fields
You can also use awk to perform mathematical operations on fields and print the result to the terminal. The below command prints the sum of the first and third fields.
echo "10 20 30" | awk '{print $1+$3}'
Conclusion
In this article, you learned ways to install AWK for Windows. You also learned how to use awk to print fields and perform mathematical operations on them.
This tutorial barely scratched the surface of AWK for Windows. You could do more complex operations, such as printing only the odd-numbered fields or mail merge and word counting. Read the documentation for more information about what you can do with awk.
How to Use the GitHub Actions Matrix Strategy in Deployments