Learn Awk - 10 Code Examples & CST Typing Practice Test
AWK is a text-processing and pattern-scanning language designed for data extraction, reporting, and quick scripting on structured text streams. It excels at line-based parsing, field manipulation, and automating command-line data workflows.
Learn AWK with Real Code Examples
Updated Nov 21, 2025
Code Sample Descriptions
AWK Counter and Theme Toggle
BEGIN {
count = 0;
isDark = 0;
function updateUI() {
print "Counter: " count;
if (isDark) print "Theme: Dark";
else print "Theme: Light";
}
function increment() {
count += 1;
updateUI();
}
function decrement() {
count -= 1;
updateUI();
}
function reset() {
count = 0;
updateUI();
}
function toggleTheme() {
isDark = !isDark;
updateUI();
}
# Simulate actions
updateUI();
increment();
increment();
toggleTheme();
decrement();
reset();
}
Demonstrates a simple counter with theme toggling using AWK variables and functions.
AWK Sum of Array
BEGIN {
sum = 0;
for (i=1; i<=5; i++) sum += i;
print "Sum:", sum;
}
Calculates sum of numbers from 1 to 5.
AWK Factorial
BEGIN {
fact = 1;
for(i=1;i<=5;i++) fact *= i;
print "Factorial: ", fact;
}
Calculates factorial of 5 using a loop.
AWK Fibonacci Sequence
BEGIN {
a=0; b=1;
print a;
print b;
for(i=3;i<=10;i++) {
sum=a+b;
print sum;
a=b;
b=sum;
}
}
Prints first 10 Fibonacci numbers.
AWK Prime Checker
BEGIN {
n=13;
isPrime=1;
for(i=2;i<=sqrt(n);i++) if(n%i==0) isPrime=0;
print "Is prime:", isPrime;
}
Checks if a number is prime.
AWK Reverse String
BEGIN {
str="HELLO";
rev="";
for(i=length(str);i>0;i--) rev=rev substr(str,i,1);
print rev;
}
Reverses a string.
AWK Multiplication Table
BEGIN {
n=5;
for(i=1;i<=10;i++) print n,"x",i,"=",n*i;
}
Generates multiplication table of 5.
AWK Celsius to Fahrenheit
BEGIN {
c=25;
f=c*9/5+32;
print "Fahrenheit:", f;
}
Converts Celsius to Fahrenheit.
AWK Simple Alarm Simulation
BEGIN {
temp=80; thresh=75;
if(temp>thresh) print "Alarm: Temp Too High!";
else print "Temp Normal";
}
Prints an alarm if temperature exceeds threshold.
AWK Random Walk Simulation
BEGIN {
pos=0;
for(i=1;i<=10;i++) {
pos += (rand()<0.5?-1:1);
print pos;
}
}
Simulates a 1D random walk for 10 steps.
Frequently Asked Questions about Awk
What is Awk?
AWK is a text-processing and pattern-scanning language designed for data extraction, reporting, and quick scripting on structured text streams. It excels at line-based parsing, field manipulation, and automating command-line data workflows.
What are the primary use cases for Awk?
Log processing and analysis. CSV and text file transformations. Inline data filtering and extraction. Quick scripting and reports. Automating shell workflows
What are the strengths of Awk?
Extremely fast for text processing. Built-in regex and field handling. Ideal for command-line automation. Readable one-liners. Zero dependencies on Unix-like systems
What are the limitations of Awk?
Not suited for large-scale or complex applications. Limited data structures beyond associative arrays. Hard to debug very long one-liners. Not ideal for binary data. Lacks modern libraries compared to Python
How can I practice Awk typing speed?
CodeSpeedTest offers 10+ real Awk code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.