awk Part I · Module 1 · Introduction to AWK
Why awk Exists
A great deal of the text an engineer deals with is already arranged in rows and columns — a list of files and sizes, a table of tests and results, a report of instances and areas. awk exists for exactly that shape of text. It reads one line at a time, splits each line into columns for you, and lets you say what to do with them. This chapter installs that single idea and then earns it four times over: printing a column, adding a column up, picking out the failing rows of a test report, and finding the error lines in a simulation log. Nothing here assumes you have used awk, written a script, or seen a simulator before.
Foundation12 min readawkPart IFundamentalsText ProcessingLog Analysis
Part I · Module 1 · Chapter 1.1 · Why awk Exists
1. The Problem awk Was Built For
Open almost any file a tool produces and you will find the same shape: one thing per line, and a few pieces of information on each line separated by spaces.
apple 10
banana 20
orange 15Three lines. Two pieces of information on each. Now consider the questions you might reasonably ask about that file:
- Print just the numbers.
- Add the numbers up.
- Show only the lines where the number is above ten.
None of those is hard, and all of them are tedious by hand. They are also awkward in a general-purpose programming language, where you first have to open the file, loop over it, strip the newline, split the line, and convert the text to a number — five mechanical steps before you get to the question you actually asked.
awk exists because that shape of text is so common that it deserves a tool which assumes it. In awk, "one line at a time, split into columns" is not something you write. It is already done by the time your instruction runs.
2. What awk Is
awk is a program for reading and processing text. You give it two things: a short program saying what to do, and some text to do it to. It reads the text line by line and applies your program to each line in turn.
Two words carry the whole model, and they are worth learning now because every later chapter uses them.
A record is one piece of input that awk handles at a time. Unless you tell it otherwise, a record is a line.
A field is one of the pieces a record is split into. Unless you tell it otherwise, awk splits on whitespace — a run of spaces or tabs, however long — which is why a report padded out so its columns line up on screen still splits into exactly the fields you wanted.
So in the file above, banana 20 is one record, and it has two fields: banana and 20. awk gives those fields names you use in your program — $1 is the first field, $2 is the second, and so on. $0 means the whole record.
Three more facts, then we start using it. awk can find lines, extract fields, count records and calculate values — those four verbs cover most of what it is ever asked to do. It is available on essentially every Unix-like system, including Linux and macOS, so a short awk command is usually something you can rely on being able to run. And awk is both the name of the language and the name of the command you type.
3. Printing One Column
Put those three lines in a file called fruits.txt:
apple 10
banana 20
orange 15Now the first question: how do we print only the numbers?
awk '{print $2}' fruits.txt10
20
15That is the whole answer. Reading the command from left to right:
awkis the program being run.'{print $2}'is your program — print the second field.fruits.txtis the file to read.
awk opened the file, took the first line, split it into fields, ran print $2, and then did the same for the second line and the third. You did not write a loop, because reading every line in turn is what awk already does.
Why the single quotes
The single quotes are there for the shell, not for awk.
Before awk ever runs, the shell looks at what you typed and does its own work on it — in particular, $2 means something to the shell too, and { and } are shell punctuation. Wrapping the program in single quotes tells the shell to pass the text through untouched, so awk receives the program you wrote rather than something the shell rewrote.
4. Your First Calculation
Same file. Now add the numbers up:
awk '{total += $2} END {print total}' fruits.txt45There are two instructions here, and they run at different times.
{total += $2} runs once for every line. total is a name we invented — awk does not require it to be declared. total += $2 means add this line's second field to total. So the value grows as awk works down the file:
| Line read | $2 | total after the line |
|---|---|---|
apple 10 | 10 | 10 |
banana 20 | 20 | 30 |
orange 15 | 15 | 45 |
END {print total} runs once, after the last line has been read. By then total holds 45, so that is what gets printed. Without END, the print would happen on every line and you would see the running total three times instead of the answer once.
5. A Practical Example: Which Tests Failed
The fruit file was a shape, not a use. Here is the same shape carrying something worth reading — a file of test results, results.txt:
test_a PASS
test_b FAIL
test_c PASS
test_d FAILThe question an engineer actually has is which tests failed. Not how many lines there are, and not the whole file — just the names in the first column of the lines whose second column says FAIL:
awk '$2 == "FAIL" {print $1}' results.txttest_b
test_dThis command has a new part. Everything before the braces is a condition: $2 == "FAIL" asks whether this line's second field is exactly the text FAIL. When the answer is yes, awk runs {print $1} and the test name appears. When the answer is no, the line is read and nothing happens.
So awk still visited all four lines. It just had nothing to do on two of them.
This is the shape of regression analysis. A real regression produces hundreds or thousands of results rather than four, and the answer a person wants is almost never the whole file — it is the failing subset, or a count, or a name. The command above is the same length whether the file has four lines or forty thousand, which is the practical reason this tool has survived.
6. A VLSI Example: Finding Errors in a Simulation Log
Now a simulation log. This one is sim.log:
100 UVM_INFO Test started
250 UVM_WARNING Retry detected
420 UVM_ERROR Data mismatch
500 UVM_INFO Test finishedEach line here has a time, a severity, and a message. A verification engineer reading a log is usually looking for one thing first: did anything go wrong?
awk '/UVM_ERROR/ {print}' sim.log420 UVM_ERROR Data mismatchThe command reads: for each line, check whether the text UVM_ERROR appears anywhere in it; if it does, print the line. The slashes mark a pattern to look for. print on its own prints the whole record, the same as print $0.
So out of four lines, one matched, and that one line is the answer: at time 420 there was a data mismatch.
That is a real first step toward finding simulation failures. It is worth being precise about how small a step it is.
7. When awk Is the Right Tool — and When It Is Not
awk earns its place when the input is text arranged in lines and columns and the output you want is a smaller piece of it: a column, a filtered set of rows, a count, or a total. In a semiconductor flow that covers a lot of ground — pulling a number out of a report, finding the important lines in a log, counting warnings and errors, summarising test results, and emitting simple comma-separated output for a spreadsheet.
It is the wrong tool, or at least the wrong first choice, for several other jobs.
When the input is not line-and-column shaped — deeply nested or structured formats, or a report where one logical entry spans a dozen lines in no fixed pattern — awk can be made to work but stops being the short answer. When the task involves real program structure, many steps, or data you need to hold and reshape, a language with proper data structures and libraries serves you better; Python is the usual choice in this field, and Chapter 1.5 is about where that line sits. And when you only need to know which lines contain some text, grep is the simpler tool, which Chapter 1.3 takes up directly.
8. Common Beginner Mistakes
Five mistakes account for most early frustration. Each one below is a real failure mode, not a hypothetical.
Confusing $1 and $2. The most common slip, and the one that produces output that looks fine. Asking for $1 in fruits.txt when you wanted the numbers gives you apple, banana, orange — a perfectly good answer to a question you did not ask. There is no error, because nothing went wrong; count the columns before you pick a number.
Forgetting the single quotes. This is the one worth seeing, because of how it fails. Here is the same program from section 3, in double quotes instead:
awk "{print $2}" fruits.txtapple 10
banana 20
orange 15No error. No warning. Three lines of plausible output — and all of it wrong. What happened is that the shell, which does look inside double quotes, replaced $2 with nothing at all before awk was started. awk received the program {print }, which means print the whole line, and did that faithfully. The fix is the single quotes:
awk '{print $2}' fruits.txt10
20
15Leaving the quotes off entirely tends to fail more loudly — the shell mangles the braces and either it or awk reports a syntax error — but the exact message depends on which shell you use, so it is not worth memorising. Single quotes avoid the whole category.
Getting the filename wrong. awk does tell you about this one:
awk '{print $2}' fruit.txtawk: can't open file fruit.txt
source line number 1The command exits with a non-zero status, which matters later when awk runs inside a script. The wording of that message differs between awk implementations, so match the shape of it rather than the exact words.
Expecting every line to have the same number of fields. Suppose one line lost its number:
apple 10
banana
orange 15awk '{print $2}' ragged.txt prints 10, then a blank line, then 15 — a field that is not there is empty, not an error. Worse, awk '{total += $2} END {print total}' ragged.txt prints 25, because the missing field counted as 0. The total is wrong and nothing says so. Chapters 8.3 and 8.4 are about catching this deliberately.
Assuming every report has the same format. A command written for one tool's output is written for that output. Field positions move between tools, between versions of the same tool, and sometimes between sections of one report. This is why the semiconductor chapters in Part III always start by reading the format rather than by writing the pattern.
9. Exercises
Create the three files from this chapter — fruits.txt, results.txt, and sim.log — and work on those. Predict each answer before you run it.
Exercise 1 — Print the first column
Print just the names from fruits.txt.
awk '{print $1}' fruits.txtapple
banana
orangeOnly the field number changed from section 3. If you can explain why that is the only change needed, you have the model from section 2.
Exercise 2 — Count the failing lines
Count how many lines of results.txt contain FAIL.
awk '/FAIL/ {n++} END {print n + 0}' results.txt2n++ adds one to n, so n counts the matching lines, and END prints it after the file is finished. The + 0 is there on purpose: on a file with no matches, n is never touched, and print n on its own prints an empty line rather than a zero. print n + 0 prints 0, which is the answer you wanted. Try it on a file with no FAIL lines and watch the difference.
Exercise 3 — Extract the warnings
Print the UVM_WARNING lines from sim.log.
awk '/UVM_WARNING/ {print}' sim.log250 UVM_WARNING Retry detectedOne line matched. Now the part worth thinking about rather than typing: this found the warning, and a warning is not a failure. Decide for yourself whether a line like Retry detected is something you would want surfaced in a nightly summary, and what you would need to know about the design to answer that. Chapter 9.3 takes the question up properly.
10. Summary
- awk is a tool for reading and processing text, and it is built for text arranged in lines and columns.
- A record is one line; a field is one column. awk splits every line for you before your program runs — that is the one idea the whole curriculum rests on.
$1is the first field,$2the second,$0the whole line. Count the columns before choosing a number.awk '{print $2}' fileprints a column, with no loop to write, because reading every line in turn is what awk already does.awk '{total += $2} END {print total}' filecalculates. The first part runs per line;ENDruns once after the last line.- A condition in front of the braces filters.
$2 == "FAIL"picks rows by a field's value;/UVM_ERROR/picks lines containing some text. - Always use single quotes. Double quotes let the shell eat
$2and give you wrong output with no error. - Be careful what a match proves. Many matching lines can be one bug, and no matching lines does not mean the run passed.
- awk is one tool among several. grep for finding lines, Python for real program structure, awk for the case where rows and columns make the answer one command long.
You can now read a column-shaped file, pull a column out of it, add one up, filter rows by a field, and find lines containing some text — which is already enough to be useful on a real report.
What that leaves open is where this gets used. A chip flow produces logs and reports at every stage, and they are not equally worth parsing. Chapter 1.2 — awk in Semiconductor Workflows maps the places awk actually earns its keep, from compile and simulation logs through timing, area and coverage reports, so the rest of Part I has somewhere concrete to point.
Where this fits
Part of the awk curriculum.
