There are times when you will want to trim some information from the output of a command. This may be because you want to feed that output into another command. Whatever the reason for wanting to manipulate the output, awk is one of many tools available in GNU/Linux to perform this task.
Now awk is a very powerful command and this demo is just a tiny fragment of what awk can do.
So let's say we want to know what kernels are installed in an Ubuntu system. The /boot directory has several files that reference the installed kernels in their file names. The command
will list these files. Notice that there are several files that reference the same kernel versions, so we'll just pick one set of these files and filter them by pipelining into the grep command. I'll use the pattern "vmlinuz".
The output should look something like this:
vmlinuz-2.6.31-15-generic
vmlinuz-2.6.31-16-generic
Now I want to trim this output to remove the "vmlinuz-" portion from the front. I'll do that by pipelining into awk with "z-" as a field delimiter.
ls /boot | grep vmlinuz | awk -F'z-' '{ print $2 }'
The [-F'z-'] in the above command defines the field delimiter. The [print $2] statement that follows it tells awk to print the second field. My output looks like this.
2.6.31-16-generic
#