Basic awk Script Construction
Let’s begin with the simplest awk
script that you can imagine, which we’ll call awk_kernel1.awk
. It looks like this:
/kernel/
As you’ve likely guessed, this script will look through a specified file to search for all lines that contain the text string kernel
. You already know that {print $0}
is the default action if no action is specified. So, this script will print out every line that contains the specified text string.
In actual awk
scripts, there’s no need to preface every command with awk
, and there’s no need to surround the commands with pairs of single quotes, as you have to do when embedding awk
commands in normal shell scripts. I didn’t put a shebang line into this script, so there’s no need to set the executable permission. Instead, just invoke the script like this:
donnie@fedora:~$ sudo awk -f awk_kernel1.awk /var/log/messages
Jan 11 16:17:55 fedora kernel: audit: type=1334 audit...