1. Home
  2. /
  3. Awk
  4. /
  5. Counter and Theme Toggle

Counter and Theme Toggle - Awk Typing CST Test

Loading…

Counter and Theme Toggle — Awk Code

Demonstrates a simple counter with theme toggling using AWK variables and functions.

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();
}

Awk Language Guide

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.

Primary Use Cases

  • ▸Log processing and analysis
  • ▸CSV and text file transformations
  • ▸Inline data filtering and extraction
  • ▸Quick scripting and reports
  • ▸Automating shell workflows

Notable Features

  • ▸Pattern-action processing model
  • ▸Automatic field splitting ($1, $2, etc.)
  • ▸Associative arrays (hash maps)
  • ▸Built-in text and regex support
  • ▸One-liners for powerful command-line automation

Origin & Creator

Created in the 1970s by Alfred Aho, Peter Weinberger, and Brian Kernighan at Bell Labs (A, W, K).

Industrial Note

Still widely used in DevOps, data engineering, UNIX systems, log parsing, sysadmin automation, and ETL pipelines.

More Awk Typing Exercises

AWK Sum of ArrayAWK FactorialAWK Fibonacci SequenceAWK Prime CheckerAWK Reverse StringAWK Multiplication TableAWK Celsius to FahrenheitAWK Simple Alarm SimulationAWK Random Walk Simulation

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher