Fundamentals 6 min read

A One‑Minute Guide to AWK: Basics, Syntax, and Common Use Cases

This article provides a concise, one‑minute introduction to AWK, covering its origin, line‑by‑line processing principle, basic syntax of pattern‑action, built‑in variables and functions, operators, control structures, and how to interact with the shell, illustrated with practical command‑line examples.

Qunar Tech Salon
Qunar Tech Salon
Qunar Tech Salon
A One‑Minute Guide to AWK: Basics, Syntax, and Common Use Cases

1. AWK AWK is named after its three creators – Aho, Weinberger, and Kernighan – and is a line‑oriented text‑processing tool commonly used in Unix environments.

2. Basic Principle The core idea of AWK is to process input files one line at a time. Its syntax follows the pattern + {action} model, where a pattern selects lines and the action defines what to do with them.

Example to display lines 3 to 5 of hello.txt : cat hello.txt | awk 'NR==3, NR==5{print;}'

2.3 Pattern Explanation Patterns can be regular expressions (e.g., /hello/ ) or numeric conditions. If no action is provided, the default action is print .

Example to print lines matching the word “hello”: cat hello.txt | awk '/hello/'

3. Built‑in Variables Common variables include: FS – field separator (default space) NR – current record number (starting at 1) NF – number of fields in the current record $0 – the entire current line $1…$n – the n‑th field of the current line.

Example to print the first and last fields of lines 3‑5: cat hello.txt | awk 'NR==3, NR==5{print $1,$NF}'

4. Built‑in Functions gsub(r,s) – replace occurrences of r with s in $0 index(s,t) – return position of t in s length(s) – length of string s match(s,r) – test if s matches regex r split(s,a,fs) – split s into array a using separator fs substr(s,p) – substring of s starting at position p

5. Operators Arithmetic operators similar to C (+, -, *, /, %, ++, --, +=, -=, etc.) and comparison operators (==, !=, >, >=, =~ for regex matching).

6. Control Flow BEGIN and END are special patterns that run before processing starts and after it finishes, respectively. Example to count total characters: awk ' BEGIN {count=0;} {count+=length($0);} END {print count;}' Other control structures (if, while, do…while, for, break, continue) behave like in C.

7. Interaction with the Shell - Using shell variables inside AWK: simply reference them within single quotes. #!/bin/bash STR="hello" echo | awk '{ print "${STR}"; }' - Executing shell commands from AWK: use double quotes or the system() function. #!/bin/bash echo hello | awk '{ system("date > date.txt"); }' - Passing data back to the shell is typically done via temporary files. - The getline statement reads input from a file or command into AWK variables. #!/bin/bash echo | awk '{ while (getline < "date.txt") { print $0; } }'

Conclusion: Although the title promises a one‑minute read, a careful study of this guide will give you a solid foundation in using AWK for everyday text‑processing tasks.

Command-lineData ExtractionUnixtext processingshell scriptingawk
Qunar Tech Salon
Written by

Qunar Tech Salon

Qunar Tech Salon is a learning and exchange platform for Qunar engineers and industry peers. We share cutting-edge technology trends and topics, providing a free platform for mid-to-senior technical professionals to exchange and learn.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.