Using Conditional Statements
You’ve already been using if
constructs without even knowing it. That’s because you don’t have to explicitly declare them as such. The simple /kernel/
command that you just saw in the awk_kernel1.awk
script means that if the kernel string is found on a line, then print that line. However, awk
also offers the whole array of programming constructs that you would expect to see in other languages. For example, let’s create the awk_kernel2.awk
script, which will look like this:
#!/usr/bin/awk -f
{
if (/kernel/) {
print $0
}
}
This is somewhat different from what you’re used to seeing in bash
scripts, because in awk
there’s no need to use then
or fi
statements. This is because awk
uses C language syntax for its programming constructs. So, if you’re used to programming in C, rejoice!
Also, note how you need to surround the pattern with a pair of parentheses, and how...